gpt4 book ai didi

Nginx 代理 : rewrite rule for root request

转载 作者:行者123 更新时间:2023-12-04 18:28:40 24 4
gpt4 key购买 nike

我在 80 端口上安装了 nginx,在 nginx 后面的 2368 端口上安装了一个节点应用程序

nginx 配置是这样的

server {
server_name domain.com www.domain.com;
listen 80;
location / {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}

此配置完全按预期工作。比如/请求变成http://localhost:2368//post/mypost变成http://localhost:1234/post/mypost

我想要的是只有 / 请求变成了 http://localhost:2368/latestpost/。所有其他请求的处理方式与上面的示例相同。谢谢!

最佳答案

你可以使用 rewrite 指令:

server {
server_name domain.com www.domain.com;
listen 80;
location / {
rewrite ^/$ /latestpost/ break;
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}

或在单独的位置:

server {
server_name domain.com www.domain.com;
listen 80;
location = / {
rewrite ^.* /latestpost/;
}
location / {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}

第二种变体的效率稍高一些,因为它不会尝试重写每个请求。但我猜差异将不明显。

关于Nginx 代理 : rewrite rule for root request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27373166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com