Nginx 代理并移除 proxy_pass 前缀

Nginx proxy and remove proxy_pass prefix

我想使用 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; 
}