天天看點

Nginx如何反向代理網站和設定虛拟主機

反向代理的應用舉例:

反向代理伺服器對于使用者而言,使用者并不清楚自己的通路的伺服器(網站)是否是真的要通路的網站還是代理的網站,隻有反向代理出錯或者使用者熟悉該網站的一些标簽性質的資訊時使用者才可能意識到自己實際是在通路一個代理伺服器。    

反向代理伺服器往往用于提高内網伺服器的安全性,但不局限于此。通過代理不僅隐藏了真實伺服器的位置和系統資訊,而且往往代理伺服器本身的安全性被管理者配置得比較高,是以很難被攻入。比如反向代理伺服器位于經過安全加強了的linux伺服器上,去代理一個windows伺服器上的網站,那實際上就降低了那台windows伺服器的安全風險。    

# 反向代理其他網站伺服器

# proxy reverse setting.

server {    

       listen       port;    

       server_name  domainname(fqdn);    

       location / {    

           proxy_pass http://{ipaddress | domainname(fqdn)};    

           proxy_set_header host $host;    

           proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;    

       }    

}    

# 虛拟主機設定

# vhost setting

server    

       {    

               listen       port;    

               server_name {ipaddress | domainname(fqdn)};    

               index index.html index.htm index.php default.html default.htm default.php; # 根據需要選擇順序    

               root  /web contents path;    

               include none.conf;    

               location ~ .*\.(php|php5)?$    

                       {    

                               try_files $uri =404;    

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

                               fastcgi_index index.php;    

                               include fcgi.conf;    

                       }    

               location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$    

                               expires      30d;    

               location ~ .*\.(js|css)?$    

                               expires      12h;    

               access_log off;    

strictly limiting access is essential if you are using a forward proxy (using the proxyrequests directive). otherwise, your server can be used by any client to access arbitrary hosts while hiding his or her true identity. this is dangerous both for your network and for the internet at large. when using a reverse proxy (using the proxypass directive with proxyrequests off), access control is less critical because clients can only contact the hosts that you have specifically configured.    

嚴格限制出入站是必不可少的,如果你使用的是正向代理(使用proxyrequests指令)。則你的伺服器可以讓使用任何用戶端的使用者通路任意主機,而他或她的真實身份将被隐藏(用戶端都被認為是代理伺服器在通路任意主機)。這對于為大型的網絡和網際網路應用來說是很危險的。    

而當使用反向代理(使用的proxypass指令proxyrequests關閉),通路控制是至關重要的,因為用戶端隻能通路專門配置的反向代理伺服器主機。

繼續閱讀