Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx proxy and remove proxy_pass prefix

Tags:

nginx

proxy

i want use nginx location proxy 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 use nginx proxy access 404

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 this:

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

thanks for everybody.

like image 337
itchenyi Avatar asked Sep 13 '15 12:09

itchenyi


1 Answers

The trailing slash does this magic, take it out from 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;
    }
  }

Let's see through the docs:

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; 
}
like image 61
Anatoly Avatar answered Sep 17 '22 22:09

Anatoly