Search code examples
nginxurl-rewritingreverse-proxyproxypass

One out of three Nginx routes ignores upstream server


I have configured three locations for a nginx reverse proxy:

    location / {
            root /var/www/html;
            index index.html;
    }


    location /login {
            proxy_pass http://127.0.0.1:9080;

            proxy_set_header        Host                    $host;
            proxy_set_header        X-Real-IP               $remote_addr;
    }


    location /app {
            rewrite ^/app/(.*)$ /$1 last;
            proxy_pass https://10.11.12.13/1020/;

            proxy_set_header        Host                    $host;
            proxy_set_header        X-Real-IP               $remote_addr;
    }

The server listening on port 9080 redirects to the route /app/{generated subpath}. The server on IP 10.11.12.13 processes the request on {generated subpath}. Therefore I remove the prefix path /app with a corresponding rewrite rule, then proxying the request to that server's /1020 endpoint.

For some reason, the nginx reverse proxy doesn't take the 10.11.12.13 upstream server but tries to find the path locally:

8888#8888: *470 open() "/var/www/html/html/createCustomer" failed (2: No such file or directory), client: x.x.x.x, server: 10.10.10.10, request: "GET /app/html/createCustomer?tokenId=0xC00FF3 HTTP/1.1", host: "10.10.10.10"

Solution

  • Instead of last I believe you are looking for break. From the rewrite documentation

    last

    stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;

    The starts a search for a new location matching the changed URi is what is happening as you are removing the /app/ part which then matches the / location.

    break

    stops processing the current set