下面举个小实例说明下:
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,如需转载请自行联系原作者