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.
Related
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?
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.
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
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.
I need to have my symfony app installed on the same domain as other webapps so I wanted it to sit in /dev/symfony_app path
I tried to use NginX Friendly PHP Framework but solutions from there do not work.
I have such nginx config and it does not work at all too. there is some problem with paths, nor root neither alias directive work for me.
location /dev/symfony_app/ {
root /home/.../public_html/web;
}
location ~ ^/dev/symfony_app/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param HTTPS off;
}
nginx error log:
request http://domain.com/dev/symfony_app/
2013/06/23 11:25:31 [error] 22549#0: *668
"/home/.../public_html/web/dev/symfony_app/index.php"
is not found (2: No such file or directory), client: *,
server: domain.com, request: "GET /dev/symfony_app/ HTTP/1.1", host: "domain.com"
request https://domain.com/dev/symfony_app
2013/06/23 11:25:37 [error] 22549#0: *668
FastCGI sent in stderr: "Primary script unknown" while
reading response header from upstream, client: *, server: domain.com,
request: "GET /dev/symfony_app HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php-fpm.sock:", host: "domain.com"
request https://domain.com/dev/symfony_app/app_dev.php
2013/06/23 11:27:06 [error] 22549#0: *797
FastCGI sent in stderr: "Primary script unknown" while
reading response header from upstream, client: *, server: domain.com,
request: "GET /dev/symfony_app/app_dev.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php-fpm.sock:", host: "domain.com"
Well, what are the dots doing there in your path? You can’t have a directory with three dots as name (at least this would be new to me). The error message from nginx is very specific in that regard. That path doesn’t exist.
server {
listen 80;
server_name _;
root /home/public_html/web;
location / {
location ~* ^/dev/symfony_app/(app|app_dev|config)\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php-fpm.sock;
}
}
}
That should do the trick.
The absolute local path to the index file of your Symfony installation has to be /home/public_html/web/dev/symfony_app/index.php. A request to http://example.com/dev/symfony_app will map the above location.
I hope this helps, otherwise please leave a comment and describe what else is going wrong.