Nginx try_files not working for default index.html - nginx

I am using $geoip_country_code module of nginx to redirect user based on IP. My config code is as given below-
server {
listen 80;
gzip on;
server_name example.com;
root html/example;
location / {
index index.html;
try_files /$geoip_country_code/index.html /index.html;
}
location ~* \.(gif|jpg|jpeg|png|js|css)$ { }
}
Idea is to redirect the user based on country for which I have localised otherwise the user goes to default index i.e generic for everyone else. It works perfectly when opened in browser from my country as I have localised for it. But when opened from a different country it shows internal server error. Its not going to default index page.
Can someone point me what am I doing wrong?

The last element of the try_files statement is the default action, which is either a URI or a response code. The /index.html starts a search for a new location to process the request, and ends up back at the start, so you have a redirection loop.
You can fix the problem by making /index.html a file term instead. For example:
try_files /$geoip_country_code/index.html /index.html =404;
The =404 is never actioned, because /index.html always exists.
Personally, I would use a generic solution:
try_files /$geoip_country_code$uri $uri /index.html;
See this document for more.

Related

NGINX Only path equals to without trailing slash and starts with path with trailing slash

I'm encountering an annoying error when it comes to my current NGINX app configuration.
I have a static web app which I am indexing on the path /admin/*. I want the index to be available on /admin, with and without a trailing slash, as well as available on a wildcard /admin/* (anything after the trailing slash).
The issue I am facing is that the index is accessable when appending anything after the admin path, for example /adminA/example.
The original NGINX configuration was as follows:
location /admin {
alias /home/user/app/static;
index index.html;
try_files $uri $uri/ /index.html;
}
The best I've been able to implement to stop this at the moment is as follows, however i'm sure it can be done more efficiently:
location = /admin {
alias /home/user/app/static;
index index.html;
try_files $uri $uri/ /index.html;
}
location /admin/ {
alias /home/user/app/static/;
index index.html;
try_files $uri $uri/ /admin/index.html;
}
The two location blocks are already efficient, but you could eliminate the redundant code in the first block by redirecting to the second.
Using an internal redirect will be invisible to the browser. For example:
location = /admin {
rewrite ^ /admin/ last;
}
location /admin/ {
...
}
Or use permanent instead of last for an external redirect, which will change the browser's address bar from /admin to /admin/. See the rewrite documentation.

How do I correctly use try_files when looking in two different directories for files to serve?

I'm quite new to Nginx so I might be misunderstanding of what try_files can do.
For my local development set up I have multiple installations that will each be accesible via their own subdomain. These installations are being migrated into a new folder structure but I still want to have the ability to support both at the same time. When pulled via git the new full path looks like this :
/home/tom/git/project/v3/[installation]/public/
The old structure goes 1 directory deeper namely as follows:
/home/tom/git/project/v3/[installation]/workspace/public
Where installation is variable according to the installation name and the /public folder will be the root for nginx to work from.
The root is determined by the subdomain and is extracted via regex like so:
server_name ~^(?<subdomain>[^.]+)\.local\.project\.test;
So far I've managed to get all this working for one of the folder structures but not both at the same time. My Nginx configuration for this local domain looks like this. Below is what I've tried but just can't seem to get working. As soon as I pass the #workspace named location as fallback for try_files it always defaults to 404.
index index.html index.htm index.nginx-debian.html index.php;
server_name ~^(?<subdomain>[^.]+)\.local\.project\.test;
root /home/tom/git/project/v3/$subdomain/public/;
location / {
try_files $uri #workspace =404;
}
location #workspace {
root /home/tom/git/project/v3/$subdomain/workspace/public/;
try_files $uri =404;
}
I have also tried shortening the root and passing the following parameters to try_files
root /home/tom/git/project/v3/$subdomain;
location / {
try_files /public/$uri /workspace/public/$uri =404;
}
But this still defaults to a 404, with a $uri/ as a third parameter there it will emit a 403 forbidden trying to list the directory index of the root.
I hope someone can provide some advice or an alternative as to how to approach this issue I am facing. If I need to provide additional data let me know,
Thanks in advance.
The named location must be the last element of a try_files statement.
For example:
location / {
try_files $uri #workspace;
}
location #workspace {
...
}
See this document for details.
The $uri variable includes a leading /, so your constructed pathnames contain a // which may be why they fail.
For example:
location / {
root /home/tom/git/project/v3/$subdomain;
try_files /public$uri /workspace/public$uri =404;
}

Regular Expression to fix broken URL's in Nginx

On my previous server that ran apache I had some htaccess rules that helped forward a certain pattern of URL's which were giving 404's to the fixed pattern.
Long time ago my URLS for my site were http://domainname/articlename and then I changed it to be http://domainname/category/articlename
Now the problem is the older links that google has are returning 404's and I want to intercept any URL that doesn't have a category and insert a fake category and then my wordpress installation can resolve the URL.
So I'm looking for a nginx solution to this problem which I presume will be in the config file somewhere that will take this URL
http://www.criticalhit.net/prey/ (which gives a 404)
and change it to
http://www.criticalhit.net/fixed/prey/
which then resolves properly.
Use a named location to perform the rewrite, although this simple rewrite can be accomplished efficiently using a return 301.
Place a regular expression location (after the PHP location block) to bypass excluded URLs. This does not need to include the static files which are served by the try_files statement.
For example:
root /path/to/root;
index index.php;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
return 301 /category$request_uri;
}
location ~ \.php$ {
try_files $uri =404;
...
}
location ~ ^/(category|tags|feeds) {
try_files $uri $uri/ /index.php;
}
See this document for more.

nginx config with spa and subdirectory root

I always seem to have problems with nginx configurations. My SPA is located at /mnt/q/app (pushstate is enabled) and the frontend root is located at client/public. Everything should be mapped to index.html, where the app picks up the route and decides what to do.
Full path to the index is /mnt/q/app/client/public/index.html.
I think I ran out of options by now. No matter what I do, I just get a 404 back from nginx, I think the configuration is simple enought and have no clue what's wrong.
server {
listen 80;
server_name app.dev;
root /mnt/q/app;
location / {
root /client/public;
try_files $uri #rewrites =404;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
}
Any help is appreciated.
If nginx views the file system from the root, then the root should be set to /mnt/q/app/client/public, and not either of the two values you are using.
The last element of the try_files directive can be a default action (e.g. /index.html), a named location or a response code. You have a named location in the penultimate element - which will be ignored.
Your named location should work, but is unnecessary, as try_files is capable of implementing it more simply. See this document for more.
For example:
root /mnt/q/app;
location / {
root /mnt/q/app/client/public;
try_files $uri $uri/ /index.html;
}
location /api {
}
location /auth {
}
The $uri/ element will add a trailing / to directories, so that the index directive can work - you do not have to add it if you do not need it.

Nginx rewriting pushstate static resources wont load

I am trying to get nginx to work with a pushstate based uri handled by react-router.
Everything works fine until I try to F5 on a second level uri example.com/MyApp/users.
My static resources are in example.com/MyApp/resources.
The problem is that nginx is trying to load my resources in example.com/MyApp/users/resources whenever I try to access directly (or F5) the users's view.
Here is my nginx conf :
location ~ ^/MyApp/ {
try_files $uri /MyApp/index.html last;
}
I am new to nginx so I don't really know how everything works...
EDIT :
I changed my conf to this:
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /MyApp/index.html break;
}
}
Now accessing to example.com/MyApp/users works but example.com/MyApp/users/ doesn't.
With client side app paths:
/
/foo
/foo/bar
/foo/bar/baz
/foo/bar/baz/123
/tacos
/tacos/123
Use nginx configuration:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
gzip_static on;
location / {
try_files $uri $uri/ /index.html;
}
# Attempt to load static files, if not found route to #rootfiles
location ~ (.+)\.(html|json|txt|js|css|jpg|jpeg|gif|png|svg|ico|eot|otf|woff|woff2|ttf)$ {
try_files $uri #rootfiles;
}
# Check for app route "directories" in the request uri and strip "directories"
# from request, loading paths relative to root.
location #rootfiles {
rewrite ^/(?:foo/bar/baz|foo/bar|foo|tacos)/(.*) /$1 redirect;
}
}
This configuration will work within a pushState "directory" such as example.com/foo/bar/baz/213123 and resolve static files at relative paths like js/app.js to example.com/js/app.js instead of example.com/foo/bar/baz/js/app.js.
For cases with directory depth beyond the first level such as /foo/bar/baz, note the order of the directories declared in the #rootfiles directive: the longest possible paths need to go first, followed by the next shallower path /foo/bar and finally /foo.
See this related answer to a similar question regarding Backbone.
I think you will have to do something like this:
location ~ ^/MyApp/ {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html =404;
}
location ~ ^/MyApp/resources {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /resources/index.html =404;
}

Resources