下面舉個小執行個體說明下:
centos7系統庫中預設是沒有nginx的rpm包的,是以我們自己需要先更新下rpm依賴庫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<code>1)使用yum安裝nginx需要包括Nginx的庫,安裝Nginx的庫</code>
<code>[root@localhost ~]</code><code># rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm</code>
<code> </code>
<code>2)使用下面指令安裝nginx</code>
<code>[root@localhost ~]</code><code># yum install nginx</code>
<code>3)nginx配置</code>
<code>[root@localhost ~]</code><code># cd /etc/nginx/conf.d/</code>
<code>[root@localhost conf.d]</code><code># cat test.conf</code>
<code>server {</code>
<code>listen 80;</code>
<code>server_name localhost;</code>
<code>location / {</code>
<code>root </code><code>/var/www/html</code><code>;</code>
<code>index index.html;</code>
<code>}</code>
<code>[root@localhost conf.d]</code><code># cat /var/www/html/index.html</code>
<code>this is page of </code><code>test</code><code>!!!!</code>
<code>4)啟動Nginx</code>
<code>[root@localhost ~]</code><code># service nginx start //或者使用 systemctl start nginx.service</code>
<code>5)測試通路(103.110.186.23是192.168.1.23機器的外網ip)</code>
<code>[root@localhost conf.d]</code><code># curl http://192.168.1.23</code>

--------------------------看看下面幾種情況:分别用http://192.168.1.23/proxy/index.html進行通路測試-----------------
為了友善測試,先在另一台機器192.168.1.5上部署一個8090端口的nginx,配置如下:
<code>[root@bastion-IDC ~]</code><code># cat /usr/local/nginx/conf/vhosts/haha.conf</code>
<code>listen 8090;</code>
<code>[root@bastion-IDC ~]</code><code># cat /var/www/html/index.html</code>
<code>this is 192.168.1.5</code>
<code>[root@bastion-IDC ~]</code><code># /usr/local/nginx/sbin/nginx -s reload</code>
<code>測試通路(103.110.186.5是192.168.1.5的外網ip):</code>
<code>[root@bastion-IDC ~]</code><code># curl http://192.168.1.5:8090</code>
192.168.1.23作為nginx反向代理機器,nginx配置如下:
1)第一種情況:
<code>location </code><code>/proxy/</code> <code>{</code>
<code> </code><code>proxy_pass http:</code><code>//192</code><code>.168.1.5:8090/;</code>
這樣,通路http://192.168.1.23/proxy/就會被代理到http://192.168.1.5:8090/。p比對的proxy目錄不需要存在根目錄/var/www/html裡面
注意,終端裡如果通路http://192.168.1.23/proxy(即後面不帶"/"),則會通路失敗!因為proxy_pass配置的url後面加了"/"
<code>[root@localhost conf.d]</code><code># curl http://192.168.1.23/proxy/</code>
<code>[root@localhost conf.d]</code><code># curl http://192.168.1.23/proxy</code>
<code><html></code>
<code><</code><code>head</code><code>><title>301 Moved Permanently<</code><code>/title</code><code>><</code><code>/head</code><code>></code>
<code><body bgcolor=</code><code>"white"</code><code>></code>
<code><center><h1>301 Moved Permanently<</code><code>/h1</code><code>><</code><code>/center</code><code>></code>
<code><hr><center>nginx</code><code>/1</code><code>.10.3<</code><code>/center</code><code>></code>
<code><</code><code>/body</code><code>></code>
<code><</code><code>/html</code><code>></code>
頁面通路http://103.110.186.23/proxy的時候,會自動加上"/”(同理是由于proxy_pass配置的url後面加了"/"),并反代到http://103.110.186.5:8090的結果
2)第二種情況,proxy_pass配置的url後面不加"/"
<code> </code><code>proxy_pass http:</code><code>//192</code><code>.168.1.5:8090;</code>
<code>[root@localhost conf.d]</code><code># service nginx restart</code>
<code>Redirecting to </code><code>/bin/systemctl</code> <code>restart nginx.service</code>
<code>那麼通路http:</code><code>//192</code><code>.168.1.23</code><code>/proxy</code><code>或http:</code><code>//192</code><code>.168.1.23</code><code>/proxy/</code><code>,都會失敗!</code>
<code>這樣配置後,通路http:</code><code>//192</code><code>.168.1.23</code><code>/proxy/</code><code>就會被反向代理到http:</code><code>//192</code><code>.168.1.5:8090</code><code>/proxy/</code>
3)第三種情況
<code> </code><code>proxy_pass http:</code><code>//192</code><code>.168.1.5:8090</code><code>/haha/</code><code>;</code>
<code>192.168.1.5 haha-index.html</code>
這樣配置的話,通路http://103.110.186.23/proxy代理到http://192.168.1.5:8090/haha/
4)第四種情況:相對于第三種配置的url不加"/"
<code> </code><code>proxy_pass http:</code><code>//192</code><code>.168.1.5:8090</code><code>/haha</code><code>;</code>
<code>[root@localhost conf.d]</code><code># curl http://192.168.1.23/proxy/index.html</code>
<code>192.168.1.5 hahaindex.html</code>
<code>上面配置後,通路http:</code><code>//192</code><code>.168.1.23</code><code>/proxy/index</code><code>.html就會被代理到http:</code><code>//192</code><code>.168.1.5:8090</code><code>/hahaindex</code><code>.html</code>
<code>同理,通路http:</code><code>//192</code><code>.168.1.23</code><code>/proxy/test</code><code>.html就會被代理到http:</code><code>//192</code><code>.168.1.5:8090</code><code>/hahatest</code><code>.html</code>
<code>注意,這種情況下,不能直接通路http:</code><code>//192</code><code>.168.1.23</code><code>/proxy/</code><code>,後面就算是預設的index.html檔案也要跟上,否則通路失敗!</code>
-------------------------------------------------------------------------------------
上面四種方式都是比對的path路徑後面加"/",下面說下path路徑後面不帶"/"的情況:
1)第一種情況,proxy_pass後面url帶"/":
<code>location </code><code>/proxy</code> <code>{</code>
2)第二種情況,proxy_pass後面url不帶"/"
<code>[root@localhost conf.d]</code><code>#</code>
這樣配置的話,通路http://103.110.186.23/proxy會自動加上"/”(即變成http://103.110.186.23/proxy/),代理到192.168.1.5:8090/proxy/
這樣配置的話,通路http://103.110.186.23/proxy會自動加上"/”(即變成http://103.110.186.23/proxy/),代理到http://192.168.1.5:8090/haha/
這樣配置的話,通路http://103.110.186.23/proxy,和第三種結果一樣,同樣被代理到http://192.168.1.5:8090/haha/
***************當你發現自己的才華撐不起野心時,就請安靜下來學習吧***************
本文轉自散盡浮華部落格園部落格,原文連結:http://www.cnblogs.com/kevingrace/p/6566119.html,如需轉載請自行聯系原作者