一、rewrite
在/usr/share/nginx/html/下创建abc.html文件
[root@localhost html]# cat /usr/share/nginx/html/81/abc.html abc
nginx配置如下
server { listen 81; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location /test { root /usr/share/nginx/html; index index.html index.htm; rewrite /test/(.*) /$1 permanent; } location /abc { root /usr/share/nginx/html/81; index index.html index.htm; } }
在浏览器访问http://192.168.10.66:81/test/abc.html,结果
在浏览器访问http://192.168.10.66:81/test/abc/abc.html,结果
- 结论:/abc.html和/abc是匹配的
二、proxy_pass
配置模拟81端口是代理,82端口是后端服务器
81端口配置如下
server { listen 81; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location /test { root /usr/share/nginx/html; index index.html index.htm; rewrite /test/(.*) /$1 permanent; } location /abc { root /usr/share/nginx/html/81; index index.html index.htm; } location /p1 { proxy_pass http://127.0.0.1:82; } }
82端口配置如下
server { listen 82; server_name localhost; access_log /var/log/nginx/82.access.log main; location / { root /usr/share/nginx/html/82; index index.html index.htm; } location /p1 { root /usr/share/nginx/; index index.html index.htm; } location /p2 { root /usr/share/nginx/html/82/p2; index index.html index.htm; } }
1、创建2个p.html
[root@localhost p1]# cat /usr/share/nginx/html/82/p1/p.html ppppp
[root@localhost p1]# cat /usr/share/nginx/p1/p.html nginx_p1
2、在浏览器访问192.168.10.66:81/p1/p.html,结果如下。根据日志看,访问的是192.168.10.66:82/p1/p.html
3、把81的proxy_pass http://127.0.0.1:82;改成proxy_pass http://127.0.0.1:82/;。
- 创建文件p.html
[root@localhost 82]# cat /usr/share/nginx/html/82/p.html test_p
- 在浏览器访问192.168.10.66:81/p1/p.html,结果如下。根据日志,访问的路径是192.168.10.66:82//p.html,即192.168.10.66:82/p.html。
4、把81的proxy_pass http://127.0.0.1:82;改成proxy_pass http://127.0.0.1:82/;后,。
- 访问http://192.168.10.66:81/p1/p2/p.html,实际访问的是http://192.168.10.66:82//p2/p.html
- 访问http://192.168.10.66:81/p1/p2/p3/p.html,实际访问的是http://192.168.10.66:82//p2/p3/p.html
- 结论:把81的proxy_pass http://127.0.0.1:82;改成proxy_pass http://127.0.0.1:82/;,在后面加了/。不加/就是代理服务器用同一url访问后端,加了/后,代理服务器访问后端的url去掉/p1。
标签:index,常用,nginx,配置,share,Nginx,html,82,81 From: https://www.cnblogs.com/lfxx/p/17315553.html