Nginx configuration for single page app and nested directories - nginx

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.

Related

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)

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;
}

Defaulting to 404 when file doesn't exist in 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.

Nginx multiple locations with different roots

I have really simple nginx configuration with 3 locations inside. Each of them have it's own root directory + I should be able to add another in the future easily.
What I want:
Request /admin => location ^/admin(/|$)
Request /admin/ => location ^/admin(/|$)
Request /admin/blabla => location ^/admin(/|$)
Request /client => location ^/client(/|$)
Request /client/ => location ^/client(/|$)
Request /client/blabla => location ^/client(/|$)
Request /blabla => location /
Request /admin-blabla => location /
Request /client-blabla => location /
Actual result:
All requests goes to location /.
I tried many different suggestions from docs, stackoverflow and other sources using different combinations of aliases, try_files, roots and regexes, but nothing worked for me.
Only when I tried to use just return 200 'admin'; and return 200 'front' it worked as intended.
Minimal config:
server {
listen 80;
index index.html;
location / {
root /var/www/html/www_new/front;
try_files $uri $uri/ /index.html;
}
location ~ ^/admin(/|$) {
root /var/www/html/www_new/admin;
try_files $uri $uri/ /index.html;
}
location ~ ^/client(/|$) {
root /var/www/html/www_new/client;
try_files $uri $uri/ /index.html;
}
}
Directory structure:
/admin
/client
/front
Thank you
When you change the root it'll still include the directory name, so what you want to do is only set the root for location /. You also don't need any additional regex on /admin as the location modifier ~ already tells nginx 'anything starting with'.
This works for your use case:
server {
listen 80;
index index.html;
location / {
root /var/www/html/www_new/front;
try_files $uri $uri/ /index.html;
}
location ~ ^/admin {
root /var/www/html/www_new; # the directory (/admin) will be appended to this, so don't include it in the root otherwise it'll look for /var/www/html/www_new/admin/admin
try_files $uri $uri/ /admin/index.html; # try_files will need to be relative to root
}
}

NGINX try_files Fallback

I have this folder structure:
/document/root/
|-- main
`-- wishlist
I want to get my nginx to work like this: If I point my browser to example.com/wishlist it will display the index.html on the wishlist folder. If I point my browser to example.com, I want it to fallback to main/index.html (and, of course, related main/style.css and other files that are in the main directory).
I don't want to write a location rule for every folder I have under my root, so I want this to be as generic as possible. I have found this questtion and it has helped me to get most of the job done, but there's something not working: if I point the browser to wishlist/index.html it works perfectly. But if I remove the index.html and point it just to example.com/wishlist the browser will return a 404. My current Nginx config is below. Can someone point me in the right direction? Thanks.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /document/root/main;
location ~ ^/([^/]+)(/.+)?$ {
if (!-d "$document_root/$1") {
return 404;
}
try_files /$1$2 /main$2 =404;
}
}
All you need to do for the index file is:
index index.html
location / {
try_files $uri.html $uri/index.html =404;
}
location /wishlist {
try_files $uri.html $uri/index.html =404;
}
Turns out I found out a way that worked for me: using a custom #location on nginx. My final piece of code turned out something like this:
location / {
root /document/root/main;
index index.html;
try_files $uri $uri/ index.html;
}
location ~ ^/(.+)$ {
root /document/root;
index index.html;
try_files $uri $uri/ index.html #main;
}
location #main {
try_files /main/$uri /main/$uri/;
}
Now example.com uses /document/root/main as it's root and example.com/wishlist uses /document/root/wishlist :) Hope this helps someone else.
Keep it simple:
server {
root /document/root/main/;
index index.html;
location /wishlist {
root /document/root/;
}
}

Resources