9
nginx做URI转发
source link: https://www.wencst.com/archives/1944
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
nginx做URI转发
新建default.conf,作为调试打印使用
server {
listen 80;
server_name localhost;
location / {
default_type text/plain;
return 200 "转发后的URI:$uri";
}
}
新建test.conf,用作测试转发的配置
server {
listen 80;
server_name www.test.com;
#--------proxy_pass配置---------------------
location /t1/ { proxy_pass http://127.0.0.1; }
location /t2/ { proxy_pass http://127.0.0.1/; }
location /t3 { proxy_pass http://127.0.0.1; }
location /t4 { proxy_pass http://127.0.0.1/; }
location /t5/ { proxy_pass http://127.0.0.1/test/; }
location /t6/ { proxy_pass http://127.0.0.1/test; }
location /t7 { proxy_pass http://127.0.0.1/test/; }
location /t8 { proxy_pass http://127.0.0.1/test; }
}
注意此处必须要用两个不同的conf文件,否则会报错。
创建一个shell脚本:
#!/bin/bash
for i in $(seq 8)
do
url=http://www.test.com/t$i/doc/index.html
echo ""
echo "执行时的URI:$url"
curl $url
echo ""
done
修改shell脚本所在的系统hosts文件,把www.test.com指向nginx服务器。
执行test脚本结果如下:
执行时的URI:http://www.test.com/t1/doc/index.html
转发后的URI:/t1/doc/index.html
执行时的URI:http://www.test.com/t2/doc/index.html
转发后的URI:/doc/index.html
执行时的URI:http://www.test.com/t3/doc/index.html
转发后的URI:/t3/doc/index.html
执行时的URI:http://www.test.com/t4/doc/index.html
转发后的URI:/doc/index.html
执行时的URI:http://www.test.com/t5/doc/index.html
转发后的URI:/test/doc/index.html
执行时的URI:http://www.test.com/t6/doc/index.html
转发后的URI:/testdoc/index.html
执行时的URI:http://www.test.com/t7/doc/index.html
转发后的URI:/test/doc/index.html
执行时的URI:http://www.test.com/t8/doc/index.html
转发后的URI:/test/doc/index.html
如果文章对您有用,扫一下支付宝的红包,不胜感激!
欢迎加入QQ群进行技术交流:656897351(各种技术、招聘、兼职、培训欢迎加入)
Leave a Reply Cancel reply
You must be logged in to post a comment.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK