1

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.

1
  • I posted a possible solution, but is there any reason you can't have the root be /var/www/html/ or /var/www/html which sounds more normal for static html files? And it works with your testing. Commented Sep 3, 2022 at 4:00

1 Answer 1

1

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"; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks works. I still don't know why. The official NGINX site shows the configuration both with and without trailing slash.
@arek121281 I believe it will work with the trailing slash on root (mine does). I couldn't get it working without leading slash for foo/ though. When you typed in */foo/test.html, it just looked for it in the root, not even matching it with the foo/ location block. Because http://localhost/foo/test.html has a leading slash. I think! please like or select answer if it helped thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.