nginx how to proxy_pass all requests expect one directory?

1.1k views Asked by At

What I'm trying to do is that all incoming requests shall be handled by my Angular application. But one directory is for a REST-api that have to be excluded.

My current nginx config locks like this:

location /rest {
    try_files $uri $uri/ /rest/index.php$is_args$args;
}

location ~ / {
    proxy_pass http://127.0.0.1:4000;
}

The above config always responds with the http://127.0.0.1:4000. The REST api itself seems to work, because when I disable the location ~ / the REST is called and returns content. So what I would like to achieve is:

https://SERVER       -> http://127.0.0.1:4000
https://SERVER/xyz   -> http://127.0.0.1:4000
...
https://SERVER/rest  -> http://127.0.0.1/rest

How can do a proxy forward off all requests but not if a specific directory is given?

1

There are 1 answers

3
g_bor On

What I would first try is to modify the first rule as:

location ^~ /rest {...

According to the info here: https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms

This should ensure that the sercond block is not encoutered if the first matches:

If the longest matching prefix location has the ^~ modifier, then Nginx will immediately end its search and select this location to serve the request.