location rewrite以及proxy_pass 的规则

本文介绍如何使用Nginx配置反向代理,将特定子目录下的请求代理到本地不同端口的服务上。例如,将http://domain.com/couchdb下的请求代理到http://localhost:5984,通过Nginx的rewrite和proxy_pass指令实现路径转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Reverse proxy for a subdirectory

Here's an excerpt of a basic nginx configuration that proxies the URL "http://domain.com/couchdb" to "http://localhost:5984" so that requests appended to the subdirectory, such as "http://domain.com/couchdb/db1/doc1" are proxied to "http://localhost:5984/db1/doc1".

location /couchdb {
    rewrite /couchdb/(.*) /$1 break;
    proxy_pass http://localhost:5984;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
 
rewrite放在location里 break标示就是匹配不再另匹配其他location和rewrite,但是改变了url,然后继续执行prox_pass。但是last标示就会停止匹配同一个location里的下一个rewrite但是它会继续
需找匹配其他location。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值