Defaulting to 404 when file doesn't exist in nginx - nginx

Here is my nginx setup:
location / {
root /var/www/web-app/public;
index index.html index.htm;
try_files $uri $uri/ /index.html;
default_type "text/html";
}
location /profile_images {
try_files $uri $uri/ =404;
}
The question is on that second block. It is a directory full of images. When I look up an image based on a user id, I may or may not have the image. If not, I want a 404 error. Based on the above I am getting a 404 on all images now. I have tried both 404 and =404.
The first location is my api which works fine.
I look up the images (in html) with src='/profiles_images/***.png'
For what it is worth, I am using reactjs.

You are missing a root directive for the second location block. Where several location blocks share the same value for root, it is usual practice to place the root statement in the enclosing server block so that all location blocks inherit the same value. For example:
server {
...
index index.html index.htm;
root /var/www/web-app/public;
location / {
try_files $uri $uri/ /index.html;
}
location /profile_images {
try_files $uri $uri/ =404;
}
}
See this document for more.

Related

NGINX, How to use same location path for different target folder?

i am trying to use same location path in nginx.
location = / {
root /usr/share/nginx/html/main;
try_files $uri /index.html;
}
location / {
root /usr/share/nginx/html/app;
try_files $uri /index.html;
}
but it is rendering app folder.
i am trying to achieve rendering 'main' folder html file in case of '/' path and 'app' folder html file in case of any other path.
You can't use the same location, you need to use different location for different folder. Even if you see "try_files" which use multiples points, "location" is different.
However, you can use multiple location like media in "/media" and "/" for html content or other.
Example
location /media {
root /usr/share/nginx/html/app;
try_files $uri /index.html;
}
location / {
root /usr/share/nginx/html/main;
try_files $uri /index.html;
}

How nginx process =404 fallback in try_files

I have a example web server with only one index.html file in a www directory. I can setup a nginx with following configuration:
location /subfolder {
alias /data/www;
try_files $uri $uri/ /index.html;
}
In browser I can see correct response on my local domain test.local/subfolder, also test.local/subfolder/something returns a default nginx page (it is normal because root is not set)
if I change a configuration to
location /subfolder {
alias /data/www;
try_files $uri $uri/ /index.html =404;
}
response to test.local/subfolder is still correct, but test.local/subfolder/something and all URI with /subfolder prefix return a index.html of correct response also status is 200 not 404. If I remove /index from try_files I get the same result
I wonder how nginx process request with =404 fallback, but cant find any information, not even in a official docs.
UDAPTE:
I found out that a alias directive should end with an / but still dont get a =404 functionality and purpose because a status is still 200ok
The try_files directive only supports these syntaxes:
try_files file ... uri;
try_files file ... =code;
It doesn't support:
try_files file ... uri =code;
The difference between file and uri here, is that for file arguments, NGINX will check their existence before moving on to next argument; for uri, it won't.
If the last argument has form of a =code, then all prior arguments to it are files (checked for existence).
From this, you can get a conclusion that with request URI /foo/bar and this config:
root /var/www;
location /foo/ {
try_files $uri $uri/ =404;
}
... Will not trigger 404 error if any of the files exist:
/var/www/foo/bar
/var/www/foo/bar/ directory (if you have autoindex enabled)
/var/www/foo/bar/index.html (or index.php, etc.) (due to value of index)
Only when none of the above exist, NGINX will trigger 404 error.
You should define the root of your server, then the default indexes and then add the =404 to try_files:
server {
server_name example.com;
root /var/www/html/example.com;
index index.html index.htm index.php;
# This is optional - if you want a customized 404 error page
error_page 404 /404.html;
location /subfolder {
try_files $uri $uri/ =404;
}
}
The difference between root and alias is that root appends location to get the absolute path in the filesystem while alias excludes the location. So for example when you try to fetch http://example.com/subfolder/filename.txt
server_name example.com;
root /var/www/html/example;
location /subfolder {
try_files $uri $uri/ =404;
}
will return the contents of /var/www/html/example/subfolder/filename.txt (if it exists) while
server_name example.com;
location /subfolder {
alias /var/log;
try_files $uri $uri/ =404;
}
will return the contents of /var/log/filename.txt (if it exists)

Nginx configuration for single page app and nested directories

I have a directory/files structure such as:
root/
a/
utils.js
b/
assets/
styles.css
app.js
index.html
And I want to configure nginx to serve files from a directory directly if exist and have single page app in directory b (if file in path exists the it wil be served directly, nd if not the fallback will end up at index.htm file.
For example:
myapp.com/a/utils.js will return that file.
myapp.com/b/ or myapp.com/b/foo will display index.html
myapp.com/b/assets/style.css will return directly css file
I tries multiple different configurations and non had worke so far. For exampe the simplest:
server {
listen 80;
root /root;
index index.html;
location / {
try_files $uri $uri/ /index.html =404;
}
}
I also tries something to serve different directories:
server {
listen 80;
root /root;
index index.html;
location /a {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /index.html =404;
}
}
I tried to define different roots as well:
server {
listen 80;
index index.html;
location /a {
root /root/a;
try_files $uri $uri/ =404;
}
location /b {
root /root/b;
try_files $uri $uri/ /index.html =404;
}
}
Nginx seems to ignore existing files and ends up returning 404 page at all times. When I try to access soe existing file directly it gets redirected to / (root) url regardless.
The last parameter of a try_files statement is the default action. There can only be one. Many of your examples have two. See this document for details.
The correct URI for your index.html file is /b/index.html which is what you need to use for the default action of the try_files statement.
This should meet your requirements:
location / {
try_files $uri $uri/ /b/index.html;
}
You do not state what should happen with the URI /a/foo. In the above case, it would also return index.html. If you need it to return a 404 response, you would use:
location / {
try_files $uri $uri/ =404;
}
location /b {
try_files $uri $uri/ /b/index.html;
}
See this document for more.

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.

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