栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx动静分离

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx动静分离

nginx动静分离
    • 1.配置动静分离
      • 1.1 环境准备
      • 1.2 安装服务
      • 1.3 修改nginx主机配置文件
      • 1.4 测试

1.配置动静分离 1.1 环境准备
主机名IP服务
nginx192.168.237.170nginx
lnmp192.168.237.167lnmp架构
httpd192.168.237.168httpd
1.2 安装服务

安装nginx

//编译安装nginx
[root@nginx scripts]# ./nginx.sh 
[root@nginx scripts]# nginx 
[root@nginx ~]# ss -anltu
Netid       State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       
tcp         LISTEN       0            128                      0.0.0.0:80                     0.0.0.0:*             
tcp         LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*          
tcp         LISTEN       0            128                         [::]:22                        [::]:*          

访问

搭建lnmp

[root@lnmp scripts]# ./lnmp.sh 
[root@lnmp scripts]# ss -anltu
Netid       State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       
tcp         LISTEN       0            128                    127.0.0.1:9000                   0.0.0.0:*          
tcp         LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*          
tcp         LISTEN       0            80                             *:3306                         *:*          
tcp         LISTEN       0            128                            *:80                           *:*          
tcp         LISTEN       0            128                         [::]:22                        [::]:*          

访问

安装apache

//编译安装apache
[root@httpd scripts]# ./apache.sh 
[root@httpd scripts]# ss -anltu
Netid       State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       
tcp         LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*          
tcp         LISTEN       0            128                         [::]:22                        [::]:*          
tcp         LISTEN       0            128                            *:80                           *:*          

访问

1.3 修改nginx主机配置文件
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
 33     #gzip  on;
 ##添加以下6行
 34     upstream static{
 35         server 192.168.237.168;	//httpd主机的IP
 36     }
 37 
 38     upstream dynamic{
 39         server 192.168.237.167;	//lnmp主机的IP
 40     }
 41 
 42     server {
 43         listen       80;
 44         server_name  localhost;
 45 
 46         #charset koi8-r;
 47 
 48         #access_log  logs/host.access.log  main;
 49 
 50         location / {
 51             #root   html;	//注释或删掉
 52             #index  index.html index.htm;		//注释或删掉
 53             proxy_pass http://static;	//访问静态资源会自动跳转到进行访问
 54         }
 55 
 56         #error_page  404              /404.html;
 57 
 58         # redirect server error pages to the static page /50x.html
 59         #
 60         error_page   500 502 503 504  /50x.html;
 61         location = /50x.html {
 62             root   html;
 63         }
 64 
 65         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 66         #
 ## 取消以下三行注释,并修改
 67         location ~ .php$ {
 68             proxy_pass http://dynamic;	//访问动态资源会自动跳转到进行访问
 69         }

[root@localhost conf]# nginx -t	//检查配置文件是否正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# nginx -s reload
[root@localhost conf]# ss -anltu
Netid       State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       
tcp         LISTEN       0            128                      0.0.0.0:80                     0.0.0.0:*          
tcp         LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*          
tcp         LISTEN       0            128                         [::]:22                        [::]:*          
1.4 测试

使用nginx主机IP访问测试

访问静态资源

访问动态资源

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/395740.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号