Nginx returns 404 after changing root path - nginx

I've installed Nginx on my Digital Ocean droplet and I've done a build for my react application, now I want to serve the build index.html file to Nginx,
/etc/nginx/sites-enabled/default:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /root/project-name/dist;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
When I visit the server in the browser I get an 404 message.
The path /root/project-name/dist is valid:
If I change the root path to:
root /var/www/html;
Then I get the default Nginx page displayed:
The root and var folder are both in the same path:
So why can't I point my root to a different folder?

Resolved the error.
Step 1. Read the logs (!!)
Nginx: stat() failed (13: permission denied)
My nginx logs are located in /var/log/nginx/. The error.log showed:
2021/10/04 18:18:41 [crit] 1565#1565: *3 stat() "/root/project-name/dist/" failed (13: Permission denied), client: 82.73.195.213, server: _, request: "GET / HTTP/1.1", host: "104.248.82.123"
Step 2. Change access to folders:
chmod +x /root/
chmod +x /root/project-name
chmod +x /root/project-name/dist

Related

nginx proxy_pass working but static content doesn't

I can set up nginx as a reverse proxy with no major issues, but if I do a simple static page test like this, the server doesn't serve pages:
server {
server_name localhost;
listen 12345;
location / {
root /Volumes/E/static/;
index index.html index.htm;
}
}
error.log says:
2023/02/09 22:39:10 [crit] 53512#0: *18710 open() "/Volumes/E/static/index.html" failed (1: Operation not permitted), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:12345"
...and I get a "500 Internal Server Error" in the browser when entering http://localhost:12345/.
I've tried chmod -R 755 /Volumes/E/static, no effect.
Why is that?

Try_files directive keeps looking for files in /usr/local/nginx/html

I can't get my head round why my nginx config keeps looking for files in the wrong directory.
I'm trying to map the /files location to /home/user/toto. For that I'm using a regex capture group to remove the /files prefix and search for the files in the directory specified in the root directive.
server {
listen 80;
server_name localhost;
location ~^\/files\/(.*)$ {
root /home/user/toto;
try_files /$1/ /$1 ;
autoindex off;
}
}
The problem is I only get 404s. And that's pretty normal because here is what we can find in error.log :
2022/07/17 11:27:49 [error] 40358#0: *40 open() "/usr/local/nginx/html/test.txt" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /files/test.txt HTTP/1.1", host: "localhost"
As we can see the root directive seems to be completely ignored.
I tried an alternative configuration such as :
server {
listen 80;
server_name localhost;
location ~^\/files\/(.*)$ {
# root /home/user/toto;
try_files /home/user/toto/$1/ /home/user/toto/$1 ;
autoindex off;
}
}
But the problem is still the same. The only change is that the path now appears in the logs as a prefix for the file name :
2022/07/17 11:12:02 [error] 40288#0: *36 open() "/usr/local/nginx/html/home/user/toto/test.txt" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /files/test.txt HTTP/1.1", host: "localhost"
What am I missing ?
Your missing the last parameter from the try_files directive, so that the /$1 is being interpreted as an internal redirect instead of a file term.
Use:
try_files /$1/ /$1 =404;
See this document for details.

Nginx - Setting root location with spaces in path

I'm trying to configure nginx for my project. Step by steps:
Create config file myproject
--/etc/nginx/sites-available
----myproject
Content of myproject file
server {
listen 80;
listen [::]:80;
root "/path/to/html/My Project/company/myproject";
index index.php index.html index.htm index.nginx-debian.html;
server_name myproject.localhost;
location / {
try_files $uri $uri/ /index.php?$request_uri;
location = /index.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME "/media/path/to/html/My Project/company/myproject/index.php";
}
}
}
Restart service nginx and try to load myproject.locahost in browser, I got "File not found."
View latest line in nginx error log, I got below error
9055#9055: *4 stat() "/media/path/to/html/My Project/company/myproject/" failed (13: Permission denied), client: 127.0.0.1, server: myproject.localhost, request: "GET / HTTP/1.1", host: "myproject.localhost"
9055#9055: *4 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: myproject.localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "myproject.localhost"
If I move it to a directory path without space, It seems to work.
How can I define this path to make it work?
You should not use path with spaces in file or directory names. But in general Unix file or directory names can contain any character other than / (which is always a directory separator) and null bytes (which you can't use =D). Try to not use spaces, $, ;, |, <, >, etc. in file or directory names, or you should escape them using backslash symbol \ behind escaping symbol.

nginx location directive doesn't look for specified file first

I have a simple nginx configuration file -
server {
listen 80 default_server;
root /var/www/example.com;
#
# Routing
#
location / { index index.html; }
location /foo { index foo.html }
#
# Logging
#
access_log /var/log/nginx/{{ www_domain }}.log;
error_log /var/log/nginx/{{ www_domain }}-error.log error;
server_name example.com;
charset utf-8;
}
As you can see, there's only 2 routes - the / and /foo paths.
When I go to www.example.com/ all works correctly. I can see the index.html page being served.
When I go to www.example.com/foo, I get a 404 error when I should be getting the foo.html page.
Looking at the logs, I see this error:
2018/08/13 21:51:42 [error] 14594#14594: *6 open() "/var/www/example.com/foo" failed (2: No such file or directory), client: XX.XX.XX.XX, server: example.com, request: "GET /foo HTTP/1.1", host: "example.com"
The error implies that it's looking for a file named /var/www/example.com/foo and not /var/www/example.com/foo.html like I would expect.
Why does this happen in general, and specifically why does it not happen on my root path / ?
Thanks!
Edit: It does work if I visit www.example.com/foo.html directly
The index directive will append the filename, when you give the URI of a directory.
So / points to the root directory (/var/www/example.com/), and the index index.html; statement causes nginx to return the file /var/www/example.com/index.html.
The /foo URI does not point to a directory. If the directory /var/www/example.com/foo/) did in fact exist, the index foo.html; statement would cause nginx to return the file /var/www/example.com/foo/foo.html. Which is not /var/www/example.com/foo.html.
What you are attempting to achieve is some kind of extension-less scheme, which is nothing to do with the index directive.
See this document for details of the index directive.
There are many solutions that will work, for example, using try_files instead of index:
location /foo { try_files /foo.html =404; }
See this document for details.

Nginx working with apache-like locations

I am trying to migrate my personal sterver from apache to nginx, but i cant get the location to work.
My server have several apps, avaliable in some /something from root, like url.com/git url.com/mediaWiki, url.com/vnstat url.com/redmine
In apache a have a bunch of config files for each app:
redmine.conf
<Location "/redmine">
Options none
Require all granted
PassengerEnabled On
RailsBaseURI /redmine
RailsEnv production
</Location>
vnstat_php.conf
Alias /vnstat /var/www/vnstat_php
<Directory /var/www/vnstat_php>
DirectoryIndex index.php
Require all granted
</Directory>
I am trying to replicate this on nginx, but my best try so far end up with a weird url. I only manage the thing to work writing on the main ngix config file:
server {
listen 8123 default_server;
listen [::]:8123 default_server;
server_name _;
root /var/www/html;
location / {
}
location /vnstat/ {
root /var/www/vnstat_php/;
index index.php;
}
}
The root page is working ok, but the /vnstat link send me to
2017/02/20 11:37:19 [error] 27538#0: *1 "/var/www/vnstat_php/vnstat/index.php" is not found (2: No such file or directory), client: 177.92.59.216, server: _, request: "GET /vnstat/ HTTP/1.1", host: "url.com:8123"
It is looking for a vnstat directory inside /var/www/vnstat_php/ instead using it as root. Can anyone point me in the right direction?
As described at http://nginx.org/en/docs/http/ngx_http_core_module.html#root
A path to the file is constructed by merely adding a URI to the value of the root directive. If a URI has to be modified, the alias directive should be used.
you should use alias for this case:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
location /vnstat/ {
alias /var/www/vnstat_php/;
}
But using PHP with nginx you should consider to work with FastCGI: https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/

Resources