咨询热线:4006-75-4006
售前:9:00-23:30 备案:9:00-18:00 技术:7*24h
之前我们说过在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都可以打开。