冰豆网

分享网络精彩
bingdou.com.cn

nginx服务器反向代理目录代理、页面代理配置规则

时间:2020-02-02加入收藏

nginx服务器下的反向代理的配置步骤比apache服务器稍简单,ngingx只需在需要代理站点配置代理规则就可以了。
(www.test1.com  代理 www.test2.com)
1、全站代理配置(首页也代理)

server {
        listen       80;
        server_name  www.test1.com ;
        root   "D:\PHP\WWW\nginx.com";
         
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            #反向代理配置
            proxy_pass http://www.test2.com
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

2、全站代理配置(首页不代理)

server {
        listen       80;
        server_name  www.test1.com ;
        root   "D:\PHP\WWW\nginx.com";
         
        #配置首页不代理
        location = / {
        index index.html index.htm index.php
        }
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            #反向代理配置
            proxy_pass http://www.test2.com
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

3、目录代理配置

server {
        listen       80;
        server_name  www.test1.com ;
        root   "D:\PHP\WWW\nginx.com";
         
        #目录代理 news目录代理 www.test2.com
        location /news/ {
        proxy_pass http://www.test2.com
        }
         
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

4、指定类型文件代理配置

server {
        listen       80;
        server_name  www.test1.com ;
        root   "D:\PHP\WWW\nginx.com";
        #指定文件类型代理配置(.shtml、.htm 类型文件代理 www.test2.com)
        location ~ .*\.(shtml|htm)$ {
        proxy_pass http://www.test2.com
        }
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

nginx服务器的指定文件类型反向代理配置时,只配置一种文件类型时,重启服务器的时候报错,导致服务器重启失败,不知道是配置的时候出了点小差错还是这里必须两种以上的类型。

5、首页和html页面使用本地文件代理配置

location  ~* \.(html)$ {  
    root /www/wwwroot/;  
    index index.html; 
}
location = / {
   proxy_pass http://127.0.0.1/index.html;
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

打 赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

TGA: 技巧

分享到:


官方微信二维码冰豆网官方微信公众号