try_files directive multiple options not working - nginx

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

Related

Nginx location regex not working for sub pages

I have two react applications, one at '/home/user/www' and one at '/home/user/builds/checkout'. I want any url starting with '/checkout' eg. '/checkout/complete', '/checkout/error' to use that application. I have the below setup in my Nginx config file:
root /home/user/www;
index index.html index.htm;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
location ~ ^/checkout?(.*)$ {
allow all;
root /home/user/builds;
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
try_files $uri $uri/index.html =404;
}
location ~ ^.+\..+$ {
try_files $uri =404;
}
going to the url '/checkout' is working correctly but any other url begining with '/checkout' like '/checkout/complete' and 'checkout/error' are just returning a 404 page.
Your configuration looks too complicated. Can you try this one?
root /home/user/www;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
location /checkout/ {
root /home/user/builds;
try_files $uri $uri/ /checkout/index.html;
}
If the /checkout URI won't redirect you to /checkout/, add this:
location = /checkout {
return 301 /checkout/;
}

Nginx not matching the location

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.

nginx match location after prefix

The problem is that nginx is matching paths correctly on www.example.com/en/
or www.example.com/pl/ but not www.example.com/en/something/. If I go to www.example.com/en/something/ then I get "Welcome to nginx!" page.
When I visit www.example.com/en/ then do action that redirects to www.example.com/en/something/ - this scenario works.
I tried locations: '/en', '^~ /en'.
What's going on?
my nginx.conf is looking like this:
server {
index index.html index.htm index.nginx-debian.html;
server_name xxx; # managed by Certbot
location / {
root /usr/share/nginx/html/en;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /en/ {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /pl/ {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
With your current configuration, you use try_files $uri $uri/ /index.html =404;.
Ignoring the redundant =404 on the end, if the file is not found, the file located at /usr/share/nginx/html/index.html will be returned. And that file probably contains "Welcome to nginx!".
All parameters of the try_files directive are like URIs, and the correct URI for the /en/ index page is /en/index.html.
For example:
index index.html index.htm;
root /usr/share/nginx/html;
location /en/ {
try_files $uri $uri/ /en/index.html;
}
location /pl/ {
try_files $uri $uri/ /pl/index.html;
}
See this document for details.

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 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.

Resources