gpt4 book ai didi

Nginx 代理并删除 proxy_pass 前缀

转载 作者:行者123 更新时间:2023-12-03 21:39:46 27 4
gpt4 key购买 nike

我想使用 nginx 位置代理我的应用程序

nginx(ip address) : 10.255.1.10
php(10.255.1.20)

ip访问:
10.255.1.20/            "access ok(200)"
10.255.1.20/api "access ok(200)"
10.255.1.20/project "access ok(200)"

但我使用 nginx 代理访问 404
example.com/work      "access ok(200)"
example.com/work/api "access not found(404)"
example.com/work/project "access not found(404)"

Nginx 配置文件:
server {
listen 80;
server_name example.com;

location /work/ {
proxy_pass http://10.255.8.77:8065/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header HOST $host/work;
proxy_read_timeout 90;
}
}

我要这个:
"curl http://example.com/work          200"
"curl http://example.com/work/api 200"
"curl http://example.com/work/project 200"

谢谢大家。

最佳答案

尾部斜杠有这个魔法,从 proxy_pass 中取出它应该有帮助:

server {
listen 80;
server_name example.com;

location /work/ {
proxy_pass http://10.255.8.77:8065;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header HOST $host/work;
proxy_read_timeout 90;
}
}

让我们看一下文档:

A request URI is passed to the server as follows:

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:


location /name/ {
proxy_pass http://127.0.0.1/remote/;
}

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:


location /some/path/ {
proxy_pass http://127.0.0.1;
}

关于Nginx 代理并删除 proxy_pass 前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32549684/

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