nginx动静分离

nginx动静分离,第1张

nginx动静分离

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

1.配置动静分离 1.1 环境准备 主机名IP服务nginx192.168.237.170nginxlnmp192.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访问测试

访问静态资源

访问动态资源

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4993007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存