1

I currently have a .confin my etc/nginx/sites-available with a bunch of location entries. Some of those location entries are setup as reverse proxy's to specific ports. However, I'm having trouble adding a location entry that just points at a directory.

server {
listen 443 ssl;
server_name sub.domain.com www.sub.domain.com;
root /var/www/html;
charset utf-8;

access_log /var/log/nginx/sub.domain.com-access.log combined;
error_log /var/log/nginx/sub.domain.com.log error;

ssl_certificate /etc/letsencrypt/live/sub.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/sub.domain.com/privkey.pem;

location /site1 {
    proxy_pass        http://127.0.0.1:7777;
    proxy_set_header  Host             $http_host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
}

location /site2 {
    proxy_pass        http://127.0.0.1:8888;
    proxy_set_header  Host             $http_host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
}

location /site3 {
            root /opt/site3;
            index index.html;
            allow all;
}

}

Right now, I'm having trouble getting sub.domain.com/site3 to serve the content of /opt/site3.

Any help on how to correctly use location {} entries side by side with proxy reverse would be greatly appreciated! Thanks.

  • What happens when you try and visit that domain? Do you see any error messages? Does it do nothing? We need more info. – Thomas Ward Jun 13 '18 at 18:38
  • I get 404 Not Found when I try accessing the /site3 slug. I've also double checked that index.html is sitting in /var/www/html/site3. – clovisd Jun 13 '18 at 20:20
  • Have you confirmed what path is being tried on disk by checking the NGINX error logs themselves? – Thomas Ward Jun 13 '18 at 21:31
  • It was pointing to /opt/site3/site3 looking for an index.html file there instead of /opt/site3/. Fixed it by pointing the root to /opt/ on its own. – clovisd Jun 13 '18 at 22:02
2

Attempting to use root with a sublocation will mean that it's going to try $root$uri, which in your case becomes /opt/site3/site3.

You can do what you did and use root so that the root directory is a folder before the folder you are trying to access. However, you don't need to do this.

Try using alias /opt/site3; instead; this should work and access the correct location provided you set the index field, and if necessary have a try_files in that location block as well.

  • Also realized I could use alias with location {} instead of root and it'll point to /opt/site3/ instead of /opt/site3/site3/. – clovisd Jun 13 '18 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.