Search code examples
nginxwebserverhttpserverwinginx

stuck on nginx location directive


I am working on windows and using Winginx, modifying the nginx.conf file under conf folder. I have changed up the server block listening to port as follows:

server{
 listen 127.0.0.1:80;
 log_not_found off;
 charset utf-8;

 access_log logs/access.log main;

 location /images/ {
  root home/localhost/public_html;
  index index.php index.html;
 }
}

The images folder is under the home folder in winginx and is added as a domain through the hostseditor. However, when I use the URL http://images, I get the 404 error.

If the location directive is changed to:

location / {
}

Everything works fine then. Neither do any regular expressions work except if I match the '/'. I have read up on location directive but could not find any relevant reason. Would be great help if anyone could point out the error. Thanks.


Solution

  • The reason is that the location directive matches URI (absolute path of the Request-URI to be more precise), not Host.

    When you use http://images, your browser sends request like this:

    GET / HTTP/1.1
    Host: images
    

    Notice that request URI is /.