Nginx不同域名反向代理到另一台服务器 proxy_pass和$host

发布于 2013-08-12  89 次阅读


转自http://www.congci.com/item/nginx-duo-yuming-duo-server-fanxiang-daili-peizhi

 

想让一个VPS专门做另一个VPS的前端,后端VPS每添加一个域名,前端VPS就要同时添加一个域名来反向代理,作为前端的VPS如果一个一个的添加后端VPS的域名,那么这个事情特别麻烦,能不能让其自动反向代理后端VPS呢,用到proxy_pass和$host就可以轻松实现。

 

以下例子为了省事,以lnmp为安装环境进行设置

 

修改前端VPS的nginx.conf文件,修改成以下内容:

 

server {

listen 80;

server_name $host;

location / {

proxy_pass http://www.31.gd/;

proxy_set_header Host $host;

proxy_redirect off;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 60;

proxy_read_timeout 600;

proxy_send_timeout 600;

}

 

下面的一并修改吧

 

location /.(php|php5)?$

{

fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_index index.php;

include fcgi.conf;

}

 

location /status {

stub_status on;

access_log off;

}

 

location /.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

 

location /.(js|css)?$

{

expires 12h;

}

 

这样就可以实现了前端VPS可以反向代理任意域名到后端VPS,只要将域名解析到前端VPS,后端VPS进行域名绑定,那么就可以直接访问到了