帮助中心 >  技术知识库 >  网站相关 >  建站知识 >  如何使用nginx下的proxy_pass指令反向代理

如何使用nginx下的proxy_pass指令反向代理

2015-11-11 17:04:23 7574

之前我们说过在nginx.conf下的http块中使用upstream来添加反向代理的服务器(参考https://www.landui.com/Help/Show-1266.html),你会发现在nginx主服务器中建立的站点也打不开的,那么怎样为单个站点建立反向代理呢?

1.在nginx目录中建立vhost.d目录

2.在vhost.d中建立proxy.conf(可以随便取名)配置文件并输入如下代码:

proxy_connect_timeout 300s;

proxy_send_timeout 900;

proxy_read_timeout 500;

proxy_buffer_size 32k;

proxy_buffers  4 32k;

proxy_busy_buffers_size 64k;

proxy_redirect off;

proxy_hide_header Vary;

proxy_set_header Accept-Encoding ;

proxy_set_header host $host;

proxy_set_header Referer $http_referer;

proxy_set_header Cookie $http_cookie;

proxy_set_header X-Real-Ip $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

expires 10d;

3.在vhost.d文件中建立要代理的站点如:site2.conf 并且输入:

server

        {

                listen 80;

                #listen [::]:80;

                server_name site2.com www.landui.com;

                index index.html index.htm index.php default.html default.htm default.php index.asp default.asp;

                location / {

                        #ModSecurityEnabled off;

             #ModSecurityConfig modsecurity.conf;

                        }

                location ~ .*.(php)(.*){

                 proxy_pass http://www.landui.com;

                 include /etc/nginx/vhost.d/proxy.conf;

                }

                location ~ .*.(asp)(.*){

                 proxy_pass http://www.landui.com;

                 include /etc/nginx/vhost.d/proxy.conf;

                }

location ~ .*.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {

    #include cache.conf;

    proxy_pass http://www.landui.com ;

    include /etc/nginx/vhost.d/proxy.conf;

}

 

 

4.在nginx.conf配置文件中引入vhost.d目录下的.conf文件

include /etc/nginx/vhost.d/*.conf;

 

5.重启nginx(service nginx restart),把域名解析到nginx反向代理主服务器,测试结果在nginx本地建立的站点www.landui.com和做反向代理的站点www.landui.com都可以打开。


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: