Nginx proxy and remove the proxy_pass prefix

I want to use nginx proxy for my applications

nginx(ip address) : 10.255.1.10
php(10.255.1.20)

IP Access:

10.255.1.20/            "access ok(200)"
10.255.1.20/api        "access ok(200)"
10.255.1.20/project    "access ok(200)"

but i am using nginx 404 proxy access

example.com/work      "access ok(200)"
example.com/work/api  "access not found(404)"
example.com/work/project  "access not found(404)"

Nginx ConfigFile:

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;
    }
  }

I want:

"curl http://example.com/work          200"
"curl http://example.com/work/api      200"
"curl http://example.com/work/project  200" 

Thanks to everyone.

+4
source share
1 answer

The trailing slash does this magic, takes it out of proxy_pass, and it should help:

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;
    }
  }

View documents:

The request URI is passed to the server as follows:

If the proxy_pass directive is specified using a URI, then when the request is sent to the server, part of the normalized URI of the location mapping request is replaced by the URI specified in the directive:

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

proxy_pass URI, URI , , URI URI:

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

Source: https://habr.com/ru/post/1607154/


All Articles