I have two locations being served by nginx. I'd like all the /api/* paths to be server by uwsgi, and have all other / paths served by index.html. I'm using Vue Router, so I also need this try_files $uri $uri/ /index.html;
Here's my full config which works for /, but does not properly exclude /api
server {
listen 8080;
location ^~ /api {
include uwsgi_params;
uwsgi_pass localhost:9000;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
location ~ ^(?!/api).+ { { # this doesn't work. i'm trying to ignore /api/* for the rule below.
try_files $uri $uri/ /index.html;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
You will need to use a different approach to exclude the /api paths from the try_files directive.
One option could be to use a regular expression in the location directive to match all paths except those starting with /api. You can use the following directive:
location ~ ^(?!/api).+ { try_files $uri $uri/ /index.html; }
This will match any path that does not start with /api. The try_files directive will then be applied to all of these paths.
Alternatively, you could use an if statement inside of the location / block to check if the request URI starts with /api. If it does, you can return a 404 response. If it does not, you can use the try_files directive as before.
location / { root /usr/share/nginx/html; index index.html index.htm; if ($uri ~ "^/api") { return 404; } try_files $uri $uri/ /index.html; }