Home > Mobile >  nginx with multiple locations directives with subdomains
nginx with multiple locations directives with subdomains

Time:11-09

I am trying to implement something like that in the nginx conf:

subdomain

  • sub.domain.com -> Serve html
  • sub.domain.com/api -> proxy to port 3001
  • sub.domain.com/viewer -> serve another html

subdomain2

  • sub2.domain.com -> proxy to port 3000

The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.

I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.

I use CentOS7. This is the configurations currently in the server:

events {
}
http {
    server {
            listen 80;
            listen [::]:80;
            server_name www.sub.domain.com subdomain.com;

            location /viewer {
                    root /opt/viewer/;
                    try_files $uri /index.html;
                    index index.html;
            }

            location / {
                    root /opt/client-bo/;
                    try_files $uri /index.html;
                    index index.html;
            }

            location /api {
                    proxy_pass "http://localhost:3001";
            }
    }
    server {
            listen 80;
            server_name www.sub2.domain.com sub2.domain.com;
            listen [::]:80;
            location / {
                    proxy_pass "http://localhost:3000";
            }
    }
}

Thanks!

CodePudding user response:

If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.

Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to

try_files $uri /viewer/index.html;
  • Related