nginxcurl html body,nginx location中uri的截取的实现方法

说明:

location 中的 root 和 alias

root 指令只是将搜索的根设置为 root 设定的目录,即不会截断 uri,而是使用原始 uri 跳转该目录下查找文件

aias 指令则会截断匹配的 uri,然后使用 alias 设定的路径加上剩余的 uri 作为子路径进行查找

location 中的 proxy_pass 的 uri

如果 proxy_pass 的 url 不带 uri

如果尾部是"/",则会截断匹配的uri

如果尾部不是"/",则不会截断匹配的uri

如果proxy_pass的url带uri,则会截断匹配的uri

examples

location中的 root

root@pts/1 $ ls -ld /data/web/lctest*|awk '{print $nf}'

/data/web/lctest

/data/web/lctest2

/data/web/lctest3

/data/web/lctest4

location /lctest {

root /data/web/;

}

location /lctest2/ {

root /data/web/;

}

location /lctest3 {

root /data/web;

}

location /lctest4/ {

root /data/web;

}

curl 测试结果如下

备注: 浏览器输入的时候最后面不添加 / , 会自动补上,但是curl 不行

root@pts/1 $ curl http://tapi.xxxx.com/lctest/

hello world

root@pts/1 $ curl http://tapi.xxxx.com/lctest2/

hello world

2

root@pts/1 $ curl http://tapi.xxxx.com/lctest3/

3

hello world

root@pts/1 $ curl http://tapi.xxxx.com/lctest4/

hello world

4

location alias

location /lctest5 {

alias /data/web/;

}

location /lctest6/ {

alias /data/web/;

}

location /lctest7 {

alias /data/web;

}

## 403 /data/web forbidden

location /lctest8/ {

alias /data/web;

}

curl 测试结果如下

curl 'http://tapi.kaishustory.com/lctest5/'

curl 'http://tapi.kaishustory.com/lctest6/'

curl 'http://tapi.kaishustory.com/lctest7/'

结果都是 /data/web/index.html的输出

root@pts/1 $ curl 'http://tapi.kaishustory.com/lctest8/'

403 forbidden

403 forbidden


nginx

location proxy_pass

#--------proxy_pass配置---------------------

location /t1/ { proxy_pass http://servers; } #正常,不截断

location /t2/ { proxy_pass http://servers/; } #正常,截断

location /t3 { proxy_pass http://servers; } #正常,不截断

location /t4 { proxy_pass http://servers/; } #正常,截断

location /t5/ { proxy_pass http://servers/test/; } #正常,截断

location /t6/ { proxy_pass http://servers/test; } #缺"/",截断

location /t7 { proxy_pass http://servers/test/; } #含"//",截断

location /t8 { proxy_pass http://servers/test; } #正常,截断

测试脚本

for i in $(seq 8)

do

url=http://tapi.xxxx.com/t$i/doc/index.html

echo "-----------$url-----------"

curl url

done

测试结果

----------http://tapi.xxxx.com/t1/doc/index.html------------

/t1/doc/index.html

----------http://tapi.xxxx.com/t2/doc/index.html------------

/doc/index.html

----------http://tapi.xxxx.com/t3/doc/index.html------------

/t3/doc/index.html

----------http://tapi.xxxx.com/t4/doc/index.html------------

/doc/index.html

----------http://tapi.xxxx.com/t5/doc/index.html------------

/test/doc/index.html

----------http://tapi.xxxx.com/t6/doc/index.html------------

/testdoc/index.html

----------http://tapi.xxxx.com/t7/doc/index.html------------

/test//doc/index.html

----------http://tapi.xxxx.com/t8/doc/index.html------------

/test/doc/index.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

<think>嗯,用户想配置Nginx反向代理,把不同的路径转发到不同的后端服务,同时去掉前缀。我需要先理清他的需求。他有两个路径:/edu_admin和/edu_h5,分别要转发到223.108.25.106的8083和8084端口,并且去掉前缀。那去掉前缀应该是指访问/edu_admin/xxx的时候,实际后端接收到的是/xxx,对吧? 首先,我记得Nginx的proxy_pass配置里,如果proxy_pass后面加了URI部分,比如http://backend/,那么请求的URI会被替换。比如location /edu_admin/ { proxy_pass http://backend/; } 这样,请求中的/edu_admin/xxx会被转发为/xxx。这就是去掉前缀的方法。可能需要用rewrite或者直接调整proxy_pass的路径。 然后,用户提供的引用中有几个例子,比如引用[3]里的rewrite规则,rewrite ^/test-api/(.*)$ /$1 break; 然后proxy_pass到某个服务。这可能是在重写URI,把/test-api/后面的部分截取出来,然后转发。或者,如果proxy_pass后面带/的话,可能自动去掉前缀。比如location /edu_admin/ { proxy_pass http://223.108.25.106:8083/; } 这样,当请求是/edu_admin/abc时,proxy_pass后面的/会把location匹配的部分去掉,转发到http://223.108.25.106:8083/abc。 那这样的话,用户的问题可以通过配置两个location块,每个里面用proxy_pass加上目标地址的根路径,这样就能自动去掉前缀。比如: location /edu_admin/ { proxy_pass http://223.108.25.106:8083/; ...其他配置... } 同样地,对于/edu_h5的路径也是如此处理。这样请求中的/edu_admin/xxx会被转发到8083端口的/xxx,而/edu_h5/yyy则转发到8084的/yyy。 还需要注意proxy_set_header的设置,比如Host头,X-Real-IP等,可能用户需要这些配置,如引用[2]中的例子,设置了几个proxy_set_header。所以需要把这些也包含进去。 另外,用户提供的引用[1]中提到,如果proxy_pass后面是绝对路径,会消除/user/这样的前缀,所以这里应该适用同样的逻辑。因此,正确的配置应该是在两个location块里分别使用proxy_pass到对应的地址,并在末尾添加斜杠,这样Nginx会自动处理URI的替换。 可能还需要考虑是否需要使用rewrite指令,比如用break标志来重写URI。但根据引用中的例子,使用proxy_pass后面加斜杠可能已经足够,不需要额外的rewrite。比如,引用[3]中的配置用了rewrite和proxy_pass的组合,但用户的需求可能更简单,可以不用rewrite。 总结下来,配置步骤应该是: 1. 在Nginx的配置文件中,添加两个location块,分别匹配/edu_admin和/edu_h5。 2. 每个location块中使用proxy_pass指向对应的IP和端口,并在末尾添加斜杠,以去掉前缀。 3. 添加必要的代理头设置,如Host、X-Real-IP等。 4. 确保配置语法正确,并重新加载Nginx。 然后测试一下,访问/edu_admin/test是否转发到8083的/test,同理/edu_h5/test到8084的/test。如果没问题,就解决了用户的问题。 可能还需要提醒用户注意location的匹配规则,比如是否以斜杠结尾,或者是否需要精确匹配。另外,是否需要处理静态文件或者上传大小限制,比如client_max_body_size,但用户的问题中没有提到,所以可能不需要,但可以作为一个考虑点。</think>### 配置Nginx反向代理规则 以下是针对路径 `/edu_admin` 和 `/edu_h5` 的反向代理配置方案,实现路径前缀去除并转发到指定IP和端口: ```nginx server { listen 80; server_name your_domain.com; # 处理/edu_admin路径请求 location /edu_admin/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 关键配置:通过结尾的斜杠自动去除前缀 proxy_pass http://223.108.25.106:8083/; } # 处理/edu_h5路径请求 location /edu_h5/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 关键配置:通过结尾的斜杠自动去除前缀 proxy_pass http://223.108.25.106:8084/; } } ``` ### 核心原理说明 1. **路径重写**:`proxy_pass` 结尾的斜杠 `/` 会将匹配到的 `/edu_admin/` 或 `/edu_h5/` 前缀替换为空字符串[^3] - 示例:请求 `http://your_domain.com/edu_admin/api/test` 会被转发到 `http://223.108.25.106:8083/api/test` 2. **请求头设置**:`proxy_set_header` 确保后端服务器能获取真实客户端信息[^2] 3. **无重写规则**:相比使用 `rewrite` 指令,直接通过 `proxy_pass` 结尾斜杠实现更高效的前缀去除[^3] ### 验证步骤 1. 保存配置文件后执行 `nginx -t` 检查语法 2. 使用 `nginx -s reload` 重新加载配置 3. 通过curl测试路径转发: ```bash curl -I http://your_domain.com/edu_admin/test curl -I http://your_domain.com/edu_h5/test ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值