Nginx not matching the location - nginx

Can anyone tell me why is this ngnix config doesn't match all URL that starts with /admin :
location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
It always fall back to the default content of location / . However, I hardcoded all the possible URL in the Nginx config, it works and only matches the hard coded URL, something like :
location /admin {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
location /admin/news/ {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
location /admin/another-url/ {
alias {{path_to_static_page}}/admin/build/;
try_files $uri $uri/ /index.html;
}
Thanks for your help.

The final term of the try_files statement is a URI. The URI of the index.html file at /path/to/admin/build/index.html is /admin/index.html.
Using alias and try_files in the same location block can be problematic.
You may want to use a more reliable solution:
location ^~ /admin {
alias /path/to/admin/build;
if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}
The location and alias values should both end with / or neither end with /. The ^~ operator will prevent other regular expression location blocks from matching any URI that begins with /admin. See this caution on the use of if.

Related

try_files directive multiple options not working

I have the following code:
server {
.....
root /user/share/nginx/html;
rewrite ^(/.*)\.html(\?.*)?$ $1$2 redirect;
location / {
try_files $uri/index.html $uri.html $uri index.html =404;
}
In the rewrite I remove the .html extension from file
In location I uri corresponding file is not found I offer a set of other options.
The last one before the error code is index.html; index.html exist but always I get the 404;
All Nginx URIs begin with a leading /. Use: /index.html.
For example:
location / {
try_files $uri/index.html $uri.html $uri /index.html;
}

Wordpress Multisite with nginx. Match subdirectories within location

I have multiple WordPress sites running in subdirectories.
Everything works great, but I'm looking to simplify my nginx configuration.
At the moment, when I add a location, I need to add an entry to my server {} configuration for the specified directory.
location / {
try_files $uri $uri/ /index.php?q=$args;
}
location /site1 {
try_files $uri $uri/ /site1/index.php?q=$args;
}
location /site2 {
try_files $uri $uri/ /site2/index.php?q=$args;
}
location /site2 {
try_files $uri $uri/ /site3/index.php?q=$args;
}
location /site4 {
try_files $uri $uri/ /site4/index.php?q=$args;
}
I tried adding a regex to match the subdirectory, but seem to have a problem with it.
location /([_-0-9a-zA-Z]/?) {
try_files $uri $uri/ /$1index.php?q=$args;
}
does not appear to do the trick. In theory that should match a subdirectory, or nothing, and be able to let me add new directories without having to touch the nginx configuration.
Any pointers would be appreciated.
#jedifans pointed out how to get the regex to work
Thanks. That did the trick on any pages, but when I go to / it just tries to download the index.php.
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass php70;
fastcgi_keep_conn on;
}
location ~ ^/([_\-0-9a-zA-Z]*/?) {
try_files /$1$uri /$1$uri/ /$1index.php?q=$args;
}
However when I go to domain.com/site1/ i get a download of the index.php not the homepage. What's missing?
I think you are missing the following from your new location block, the ~ operator that says to nginx that it's a regular expression. Try:
location ~ ^/([_-0-9a-zA-Z]*/?) {
A few regex tweaks too, ^ to say it must begin with / and * to match more than one of those characters within [ and ]
Edit: after your question update, try the following try_files directive:
try_files $uri $uri/ /$1/index.php?q=$args;
$uri should work without the matched string prepended and I have added a / in between the matched query and the index.php;
Also make sure to have index index.php; at server{} level.

Nginx - sub-url and separated service

It's possible in nginx to do something like:
location /backend {
try_files $uri #proxy_to_app;
}
location /{
try_files $uri #proxy_to_frontend;
}
It works when I go to /backend but if I go to /backend/something_more I'm redirected to /something_more

nginx try_files without any files - internal redirect

Given:
location ~ /foo/ {
try_files $uri $uri/ /foohandler.py;
}
try_files: "If none of the files were found, an internal redirect to the uri specified in the last parameter is made" 1.
If I know $uri and $uri/ will never exist, how do I always do an internal redirect to /foohandler.py without using try_files?
Using try_files /foohandler.py is invalid syntax. What is the proper equivalent? return? rewrite?
location ~ /foo/ {
rewrite ^ /foohandler.py break;
}
You can still use try_files. It requires more than one parameter, so just add a 404 code to avoid the syntax error:
location ~ /foo/ {
try_files /foohandler.py =404;
}
On a side note, if your uri always begins with /foo/ then a prefix location would be better:
location /foo/ {
try_files /foohandler.py =404;
}

why does ^~ not stop processing routing rules in nginx?

So I've got 2 routes, and the first one doesn't stop the route matching, as the docs say it should:
location ^~ /p/ {
root /www/domain.com/;
try_files $uri $uri/ /path/index.html;
}
location ^~ /v/ {
root /www/domain.com/;
try_files $uri $uri/ /path/index.html;
}
location ^~ / {
root /www/domain.com/php_www/;
try_files $uri $uri/ /index.php;
location ~* \.(?:php|html)$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
auth_basic "Staging";
auth_basic_user_file /www/.htpasswd;
}
So if I have a url like this:
http://domain.com/p/1234567890
It matches the last route and not the first route. The problem surfaced because one of our guys added a page to the application:
http://domain.com/privacy
This was picked up by the FIRST route?? Which is where the problem is coming from.
The problem I'm having is with ^~. In the docs, it says that once this matches, it will stop matching, however the last route is always the one that loads.
Any ideas?
Upgraded to latest nginx, and re-ordered some of the directives and everything is working now.

Resources