想讓一個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進行域名綁定,那么就可以直接訪問到了