Ubuntu下如何配置Nginx做反向代理
Ubuntu是一個以桌面應(yīng)用為主的Linux操作系統(tǒng),Debian和Ubuntu都自帶了Nginx,用他們來配置Nginx的反向代理,非常方便。
安裝Nginx
運行如下命令安裝并運行Nginx
- apt-getinstallnginx
- /etc/init.d/nginxstart
然后在瀏覽器里面訪問該IP的80端口,就會看到"WelcometoNginx!"的信息,這說明Nginx安裝完成了!
配置Nginx做反向代理
Nginx的缺省站點的配置文件是/etc/nginx/sites-available/default,修改這個文件中的如下部分:
- location/{
- root/var/www/nginx-default;
- indexindex.htmlindex.htm;
- }
修改為:
- location/{
- proxy_passhttp://www.6688.cc/;
- proxy_redirectoff;
- proxy_set_headerX-Real-IP$remote_addr;
- proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
- }
然后重啟Nginx:
- /etc/init.d/nginxrestart
然后在瀏覽器里面重新訪問該IP上面的80端口,應(yīng)該就看到google的主頁了,反向代理配置成功了
多域名反向代理配置實例
在一個VPS上配置多個域名的反向代理,比如我們有兩個域名test1.idcfree.com和test2.idcfree.com,我們希望客戶在訪問test1.idcfree.com的時候出現(xiàn)www.linuxidc.com的內(nèi)容,希望客戶在訪問test2.idcfree.com的時候出現(xiàn)www.baidu.org.tw的內(nèi)容,客戶只知道test1.idcfree.com和test2.idcfree.com的存在,而不知道www.linuxidc.com和www.baidu.org.tw的存在。
首先需要把域名test1.idcfree.com和test2.idcfree.com指向VPS的IP地址。
然后在/etc/nginx/sites-available目錄下增加兩個文件,文件名分別是test1.idcfree.com和test2.idcfree.com
test1.idcfree.com的文件的內(nèi)容如下:
- server{
- listen80;
- server_nametest1.idcfree.com;
- location/{
- proxy_passhttp://www.linuxidc.com/;
- proxy_redirectoff;
- proxy_set_headerX-Real-IP$remote_addr;
- proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
- }
- }
test2.idcfree.com的文件的內(nèi)容如下:
- server{
- listen80;
- server_nametest2.idcfree.com;
- location/{
- proxy_passhttp://www.baidu.org.tw/;
- proxy_redirectoff;
- proxy_set_headerX-Real-IP$remote_addr;
- proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
- }
- }
然后運行命令:
- cd/etc/nginx/sites-enabled
- ln-sf/etc/nginx/sites-available/test1.idcfree.com.ln-sf/etc/nginx/sites-available/test2.idcfree.com.
- /etc/init.d/nginxrestart
這時候在瀏覽器里面訪問test1.idcfree.com將會出現(xiàn)www.linuxidc.com的內(nèi)容,訪問test2.idcfree.com將會出現(xiàn)www.baidu.org.tw的內(nèi)容。
反向代理的高級配置
關(guān)于Nginx反向代理的一些高級配置,我們會不斷寫博客介紹,敬請關(guān)注。
總結(jié):
希望本文介紹的在Ubuntu下配置Nginx做反向代理的方法能夠?qū)ψx者有所幫助,更多有關(guān)linux系統(tǒng)的知識還有待于讀者去探索和學(xué)習(xí)。
【編輯推薦】