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

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

Related

Nginx sites-available configuration

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!

Getting 500 internal error on redirect rule of alias in nginx

I am newbie to nginx server. I am getting stuck in URL redirection. I have following lines to default file.
server {
listen 80;
root /home/ubuntu/web/server/current/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
error_log /home/ubuntu/web/error.log info;
location / {
# try_files $uri $uri/ /index.php?$query_string;
# try_files $uri $uri/ =404;
}
rewrite ^/web/(.*) /web/;
location /web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
what I expect from above rewrite rule is - all URLs like - http://example.com/web/login, http://exmpale.com/web/dashboard will be redirected to /home/ubuntu/web/client/web/ and the default page to hit is index.html file.
When I open the error log file then i found error like -
rewrite or internal redirection cycle while internally redirecting to "/web/index.php", client: ip_address, server: _, request: "GET /web/ HTTP/1.1", host: "ipaddress"
What i am doing wrong here.
Answer credit goes to #RichardSmith, he provided possible error which is in right direction. I figure out my mistake in nginx rewriting rule.
rewrite ^/web/(.*) /web/;
location /web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
Instead of above I should have following-
rewrite ^/web/(.*) web/;
location web/ {
alias /home/ubuntu/web/client/web/;
# try_files $uri $uri/ index.html;
}
Server consider path as absolute whenever / placed before web. Thus rewrite statement tries to redirect to fallback file which is not existed in absolute path. Eventually, rewrite statement makes never ending loop.

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)

Location not working for files but only for path

I have a nginx.conf that looks like this:
server {
...
root /var/opt/data/web;
...
location ~* \.(?:eot|woff|woff2|ttf|js)$ {
expires 1M;
}
...
location /one {
root /var/opt/data/alternatives;
try_files $uri $uri/ =404;
}
location /two {
root /var/opt/data/alternatives;
try_files $uri $uri/ =404;
}
}
when I curl http://localhost/one/ I get the content of index.html stored in /other. But when I curl .../localhost/one/foo.js the file is not found and I get this in the error.log:
open() "/default/foo.js" failed (2: No such file or directory)
I tried other variants like location ~ (one|two), location /one/ or even location ~ /(one|two) but all of them didn't work.
The complete config consists of a lot more locations, but I guess the cause of my problem is the location where I set up .js resources to expire -1 because this prevents changing the root to what I need.
If this matters: I use nginx 1.15.2. In case you are wondering why I have this strange alternatives directory: the web directory is created by a CMS software while alternatives is git pulled.
nginx chooses a one location to process a request. Your location ~* \.(?:eot|woff|woff2|ttf|js)$ block processes any URI that ends with .js, and its root value is inherited from the outer block as /var/opt/data/web.
Where you have multiple roots, you need to ensure that those location blocks take precedence, by using the ^~ modifier. See this document for details.
For example:
server {
...
root /var/opt/data/web;
...
location ~* \.(?:eot|woff|woff2|ttf|js)$ {
expires 1M;
}
...
location ^~ /one {
root /var/opt/data/alternatives;
try_files $uri $uri/ =404;
location ~* \.(?:eot|woff|woff2|ttf|js)$ {
expires 1M;
}
}
...
}
If you need your expires rule to apply to the other roots, you will need to repeat the location within that scope, as shown above.
As an alternative, the expires directive can be used in conjunction with a map. See this document for details.
For example:
map $request_uri $expires {
default off;
~*\.(eot|woff|woff2|ttf|js)(\?|$) 1M;
}
server {
...
root /var/opt/data/web;
expires $expires;
...
location ^~ /one {
root /var/opt/data/alternatives;
try_files $uri $uri/ =404;
}
...
}

Multiple Location For Static Location in Nginx Configuration

I have a two locations where my app will serve static files, one is /my/path/project/static and the other is /my/path/project/jsutils/static.
I'm having a hard time getting the webserver to look in both directories for static content. Here is my entry for static location in the nginx configuration file for my app.
location ^~ /static {
root /my/path/project/static;
alias /my/path/project/jsutils/static;
index index.html index.htm;
}
I get an error that says : "alias" directive is duplicate, "root" directive was specified earlier.
I'm not sure how to go about having nginx look in both these paths for static content.
Thank you in advance for any help.
location ^~ /static {
root /my/path/project/static;
index index.html index.htm;
try_files $uri $uri/ #secondStatic;
}
location #secondStatic {
root /my/path/project/jsutils/static;
}
So first the file will be searched in /my/path/project/static and if that could not be found there, the secondStatic location will be triggered where the root is changed to /my/path/project/jsutils/static.
You may use try_files (http://wiki.nginx.org/HttpCoreModule#try_files). Assuming that you static files are in /my/path/project/static and /my/path/project/jsutils/static. you can try this:
location ^~ /static {
root /my/path/project;
index index.html index.htm;
try_files $uri $uri/ /jsutils$uri /jsutils$uri/ =404;
}
Let me know if it works. Thanks!
Just implement your configuration in nginx language:
location /my/path/project/static {
try_files $uri =404;
}
location /my/path/project/jsutils/static {
try_files $uri =404;
}
I had the exact same problem and it looks like nginx doesn't like when root is overwritten by an alias. I fixed it by firstly removing the root declaration that was inside the server section and instead declared the root and alias appropriately directly in the location sections (note the commented out lines):
server {
# root /usr/share/nginx/html;
location /logs/ {
root /home/user/develop/app_test;
autoindex on;
}
location /logs2/ {
# root /home/user/branches/app_test;
alias /home/user/branches/app_test/logs/;
autoindex on;
}
}

Resources