Nginx动静分离

Nginx动静分离

环境

代理 192.168.133.161

静态 192.168.133.160

动态 192.168.133.162

客户端

[root@proxy-nginx conf.d]# cat /etc/hosts
192.168.133.161 www.up-proxy.com

代理端


[root@proxy-nginx conf.d]# cat d-static.conf
upstream static {
        server 192.168.133.160:8080;
}
upstream dynamic {
        server 192.168.133.162:8080;
}

server {
        listen 80;
        server_name www.d-static.com;
        root /usr/share/nginx/html;
        # Load configuration files for the default server block.
        # include /etc/nginx/default.d/*.conf;
        location ~ .(php|jsp)$ {
                proxy_pass http://dynamic;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        location ~ .*.(html|gif|jpg|png|bmp|swf|css|js)$ {
                proxy_pass http://static;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

静态

[root@web-nginx conf.d]# cat d-static.conf
server {
        listen 192.168.133.160:8080;
        server_name www.d-static.com;
        location ~ .(html|jpg|png|js|css|gif|bmp|jpeg) {
                root /var/www/d-static;
                index index.html index.htm;
        }
}
[root@web-nginx conf.d]# mkdir /var/www/d-static
[root@web-nginx conf.d]# echo "hello static-nginx" > /var/www/d-static/index.html

动态

[root@web-nginx2 conf.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@web-nginx2 conf.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@web-nginx2 conf.d]# yum install php71w-xsl php71w php71w-ldap php71w-cli
php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring
php71w-bcmath php71w-mcrypt -y
[root@web-nginx2 conf.d]# yum install -y php71w-fpm
[root@web-nginx2 conf.d]# systemctl start php-fpm
[root@web-nginx2 conf.d]# systemctl enable php-fpm
[root@web-nginx2 conf.d]# cat dynamic.conf
server {
        listen 192.168.133.162:8080;
        server_name www.d-static.com;
        location ~ .php$ {
                root /var/www/dynamic-s;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
[root@web-nginx2 conf.d]# mkdir /var/www/dynamic-s
[root@web-nginx2 dynamic-s]# echo "hello 192.168.133.162 dynamic" > index.php

测试:

静态:http://www.up-proxy.com/index.html

动态:http://www.up-proxy.com/index.php

配置若有遗漏或错误,请评论留言。
原文地址:https://www.cnblogs.com/BrokenEaves/p/15153575.html