Nginx sites-available configuration - nginx

So, I emigrate from Apache to Nginx, and it is a terrible experience for me.
Right now I need your help with understanding of Nginx file of configuration:
server {
....
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$args;
}
location ~ "^/([-0-9a-zA-Z_\s]+)$" {
try_files $uri $uri/ /user/user?username=$1;
}
location ~ "^/trends/([.*]+)$" {
try_files $uri $uri/ /trends/trend?name=$1;
}
}
When I type to the URL bar site https://example.com/someuser ,
it successfully redirect me (show to me the folder) to the folder example.com/user/user?username=someuser .
But when I trying to access to https://example.com/trends/sometrend
Nginx load me the page from the fist block.
Why it do this?
Thanks for your help.

Sorry, for my late reply, but I suppose that it will be helpful for others, who will face with similar problem:
server {
...
# enable utf8
charset UTF-8;
# pretty urls
# user folder
location ~ "^/([-0-9a-zA-Z_\s]+)$" {
try_files $uri $uri/ /user/user?username=$1;
}
# trends
location ~ "^/trends/([\x00-\xff]+)$" {
try_files $uri $uri/ /trnds/trend?name=$1;
}
...
}
Let me explain it in details.
\x00-\xff - it allow all symbols in Unicode format, don't forget to enable UTF-8 with charset UTF-8; in the server block.
/trnds/ - on official documentation to Nginx (cant find it now to post the link) they have a little explain that this folder shouldn't exist in the server if we have the same address in the URL bar.
So, I change try_files from /trends/ to /trnds/ and do refactor of folder on my server and everything work like a charm!

Related

nginx rewrite specific folder to php and arg

Am trying to redirect
domain.tld/blog/read.php?article=first-article to domain.tld/blog/first-article
What I tried and didn't work resulting in redirect to domain.tld/first-article
location "^blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}
location /blog {
rewrite ^/blog/?$ /blog/read.php?article=? last;
rewrite ^/blog/([-a-zA-Z0-9_]+)/$ /blog/read.php?article=$1? last;
}
location ~ "^/blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}
Thinking the issue comes from my other parts in the config and mainly second location from below
server {
...
...
location ~ "^/([^/]+)/?$" {
try_files $uri $uri/ /device.php?name=$1;
}
location ~ "^/([^/]+)/([^/]+)/?$" {
try_files $uri $uri/ /device.php?name=$1&crversion=$2;
}
...
Any pointers would help a lot
Cheers
So the fast answer is actually the fact that my config was fine first time, yet nginx config is read top bottom with first match being the one that is used.
So in the end the "fix" was adding the blog part upper in the site config
location ~ "^/blog/([^/]+)/?$" {
try_files /$uri /$uri/ /blog/read.php?article=$1;
}

Nginx Configuration wildcard first folder

I am struggling to get nginx conf to work the way we need it.
Basically on the same domain we have many apps, each one in root folder. As the user installs apps it is not possible to know the name of the folders.
location / {
try_files $uri $uri/ /index.php?$args /index.php?q=$uri&$args;
}
location /myfiles {
try_files $uri $uri/ /myfiles/index.php?$args /myfiles index.php?q=$uri&$args;
}
If I specify the second folder, it makes app in myfiles work, URLs are resolving properly. If I do not then the main app tries to resolve the URL and it fails.
So I would like to have something like:
location /* {
try_files $uri $uri/ /$folderrequested/index.php?$args /$folderrequested/index.php?q=$uri&$args;
}
where * would be any root folder, for example myfiles, mycrm, myaccount, which would route the trafic to that folder.
Any suggestions and ideas welcome!
Put all your app root directories in a parent directory.
server {
listen .....;
server_name ....;
root /path/to/apps;
index index.php index.html;
location / {
}
location ~ \.php {
fastcgi_pass localhost:8000;
}
}
Bingo.

nginx redirects - how can I optimise my code?

I've got the following config (which works) in nginx:
location /de-de/sitemap.xml {
try_files $uri /sitemaps/de-de/sitemap.xml;
}
location /en-ae/sitemap.xml {
try_files $uri /sitemaps/en-ae/sitemap.xml;
}
location /en-de/sitemap.xml {
try_files $uri /sitemaps/en-de/sitemap.xml;
}
location /en-gb/sitemap.xml {
try_files $uri /sitemaps/en-gb/sitemap.xml;
}
location /en-us/sitemap.xml {
try_files $uri /sitemaps/en-us/sitemap.xml;
}
This is because requests from the outside will be looking for (say) /en-gb/sitemap.xml, but the actual file for said sitemap actually exists in /sitemaps/en-gb/sitemap.xml.
Now, like I said before, this works, but I can't help but think it could be optimised (i.e using some type of matching rule, rather than listing each of the store codes individually??) but I've not had much luck making it work.
Plus I'm not sure if the try_files command is the best for this purpose.
Thank you.
PS, it is only the sitemap.xml files that I want to target, all other files that exist within that path should be served as usual.
You could try using a back reference to the language element of the path:
location ~ "^/([a-z]{2}-[a-z]{2})/sitemap.xml$" {
try_files $uri /sitemaps/$1/sitemap.xml;
}

Nginx - multiple/nested IF statements

What i want to do:
Check if request comes from Facebook
Check if URL is like domain.com/2
If above conditions are true - show content from /api/content/item/$1?social=1
If above conditions are false - show "normal page"
It is a single page app. Before my changes configuration looked like this (and it worked):
location / {
root /home/eshlox/projects/XXX/project/project/assets/dist;
try_files $uri $uri/ /index.html =404;
}
I've tried to use if statements:
location / {
set $social 1;
if ($http_user_agent ~* "facebookexternalhit") {
set $social UA;
}
if ($uri ~* "^/(\d+)$") {
set $social "${social}URL";
}
if ($social = UAURL) {
rewrite ^/(\d+)$ /api/content/item/$1?social=1;
}
root /home/eshlox/projects/XXX/project/project/assets/dist;
try_files $uri $uri/ /index.html =404;
}
With this configuration everything works as i expected only if both conditions are true or false.
If one of conditions is true and the second is false (or vice versa) then nginx always returns status 404.
I have found "IfIsEvil" on nginx site, i've tried to use mapping (can i use mapping in this case?) but still i can't resolve this problem.
Any ideas?
Best regards.
There is good article about common pitfalls in nignx wiki.
First, I've moved root directive to server level. Second, location is the best way to check urls. So I rethink your requirements as
if location consist of digits
and request from facebook
we have to rewrite url, and the result is:
root /home/eshlox/projects/XXX/project/project/assets/dist;
location / {
try_files $uri $uri/ /index.html;
}
location ~ "^/\d+$" {
if ($http_user_agent ~* "facebookexternalhit") {
rewrite (.+) /api/content/item$1?social=1;
}
try_files $uri $uri/ /index.html;
}
Also, there is almost no reason to have =404 after /index.html in try_files directive.

Nginx virtual host error duplicate location "/" in /etc/nginx/vhosts/

After I added add this Wordpress W3 Total configuration to my Apache + Nginx VPS vhosts (which is located at /etc/nginx/vhosts/mysite.com), it gives me following error. (In Nginx Admin when the server rebooting)
2014/01/25 17:08:03 [emerg] 640#0: duplicate location "/" in
/etc/nginx/vhosts/mysite.com:54
my nginx main configuration file:- http://pastebin.com/jHtG1Hax
my vhost default configuration file :- http://pastebin.com/pQMZutL0
W3 total configuration:- http://pastebin.com/xB8DnPAN
How do I fix this issue? Any help really appreciated.
The problem is that you have the same location defined twice, just like the error says, you need to remove this block
location / {
try_files /wp-content/cache/page_enhanced/${host}${cache_uri}_index.html $uri $uri/ /index.php?$args ;
}
and then delete this in the other file
try_files $uri #backend;
and put this instead
try_files /wp-content/cache/page_enhanced/${host}${cache_uri}_index.html $uri #backend;
here's the #backend location
location #backend {
proxy_pass http://127.0.0.1:8081;
}
Any other location should just use this same proxy location, and for the ~\.php I think it can be replaced with something like this
location ~ \.php$ {
try_files #backend =404;
}

Resources