Home > Mobile >  nginx execute incorrect path with alias
nginx execute incorrect path with alias

Time:09-03

I don't understand my nginx not serve file from '/var/www/html/foo/' when I execute 'w3m http://localhost/foo/test.html'? I get 404 error, despite of existing test.html in '/var/www/html/foo/'. When I checked log I see that it looking for page in '/var/www/html/nginx/foo/test.html'.

user nobody nogroup;
worker_processes 2; 
events {
        worker_connections 512;
}
http {
        server {
                listen *:80;
                listen *:1026; 
                server_name "test";
                root /var/www/html/nginx/ ;
                location foo/ {
                        alias /var/www/html/foo/ ; 
                }
        }
}


arek@127:~$ ls /var/www/html/nginx
test.html
arek@127:~$ ls /var/www/html/foo
index.html  test_bigger.html  test.html  text.html
arek@127:~$ 

When I checked log I see that it looking for page in '/var/www/html/nginx/foo/test.html'

2022/09/03 02:36:05 [error] 139475#139475: *2 open() "/var/www/html/nginx/foo/test.html" failed (2: No such file or directory), client: 127.0.0.1, server: test, request: "GET /foo/test.html HTTP/1.0", host: "localhost"

when I change my root path on '/var/www/html/' its works.

CodePudding user response:

Try it with the server configuration block like this instead, which is working on my server. Removed the trailing slash from the root, and added a slash before foo/. Also added a default_type for the location. If you still get an error, comment with the log showing the query path.

 server {
        listen 80;
        listen 1026; 
        server_name "test";
        root /var/www/html/nginx;
 location /foo/ {
        alias /var/www/html/foo/;
        default_type "text/html";
            }
             
        }
  • Related