Linux VPS上配置Nginx反向代理
Nginx是一款高性能的HTTP和反向代理服務器。VPS偵探以前已經多次介紹過Nginx的HTTP應用,比如lnmp一鍵安裝包。下面要說的是Nginx的反向代理功能。
反向代理是什么?
反向代理指以代理服務器來接受Internet上的連接請求,然后將請求轉發給內部(或其他)網絡上的服務器,并將從服務器上得到的結果返回給Internet上請求連接的客戶端。
一句話說明就是訪問的域名是a.com,實際訪問的內容是b.com的。
實現方法:
比如我想在VPS上建一個t.vpser.net的域名用來反向代理訪問http://127.0.0.1:3000,首先在域名注冊商那里的域名管理上為域名t.vpser.net添加A記錄到VPS的IP上,再在VPS上修改Nginx的配置文件,添加如下:
server
{
listen 80;
server_name t.vpser.net;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
}
添加好后,先執行:/usr/local/nginx/sbin/nginx -t 檢查配置是否正常,如果顯示:the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok????? configuration file /usr/local/nginx/conf/nginx.conf test is successful 則正常,否則按錯誤提示修改配置。
這樣只是簡單的反向代理,如果nginx上已經安裝 --with-http_sub_module 模塊,可以在location / { 中添加上 sub_filter 替換內容 '替換后內容'; 進行簡單的內容替換,默認只替換html內容(如更改html源碼里面的網址、文章等信息),其他文件類型,需要添加 sub_filter_types 參數指定更多類型,多次替換還需要加上sub_filter_once off; ?。
如果網站較大可能會有很多其他的資源需要替代,需要在nginx上添加第三方模塊ngx_http_substitutions_filter_module來實現更多功能。
再執行 /usr/local/nginx/sbin/nginx -s reload 使配置生效,域名解析生效后就可以通過t.vpser.net 訪問txxx了。
>>轉載請注明出處:VPS偵探 本文鏈接地址:http://www.0794baidu.com/manage/linux-vps-nginx-reverse-proxy.html










@John Smith, 可能你nginx配置上只有這一個虛擬主機