1

I am having hard time finding a solution. I have a Ubuntu 14.04 machine running NGINX. I have below 2 folders I would like to host.

/var/www/apphost.comp.ill.com/app1/home/index.html /var/www/apphost.comp.ill.com/app2/index.html

I would like to open app1's index file when I go to "apphost.comp.ill.com/app1" and open app2's index file when I go to "apphost.comp.ill.com/app2".

I believe I need to edit "/etc/nginx/sites-available/apphost.comp.ill.com" to make it happen but I can't seem to figure out how. I tried multiple things, searched online but could not find any solution. Here is how my file looks like currently:

server {
    listen 80;
    listen [::]:80;

    root /var/www/apphost.comp.ill.com/app1/home;     
    index index.html index.htm home home.html;

    # Make site accessible from http://localhost/
    server_name apphost.comp.ill.com;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

It works for app1 when I go to apphost.comp.ill.com. How can I make it so it works when I go to "apphost.comp.ill.com/app1" and also add app2 to work when I go to "apphost.comp.ill.com/app2".

Please help. Thanks

2
  • I don't know much about Nginx but is the mix of slashes / and backslashes \ intentional?
    – dreua
    Commented Dec 6, 2017 at 22:50
  • Not intentional .. fixed
    – Neo
    Commented Dec 6, 2017 at 22:57

2 Answers 2

0

I tried this and it works:

server {
    listen 80;
    listen [::]:80;

    index index.html index.htm home home.html;

    # Make site accessible from http://localhost/
    server_name apphost.comp.ill.com;

    location /app1 {
            # we need to use alias here to strip the "app1" from 
            # the requested path
            alias /var/www/apphost.comp.ill.com/app1/home;

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }    
    location /app2 {
            # we don't need an alias here because app2 matches the
            # directory structure
            root /var/www/apphost.comp.ill.com/;

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }
}

I would recommend /app1 and /app2 without a leading slash because in my test browsing to .../app1 didn't work otherwise.

Documentation of the alias command: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

0
0

Edit: This answer is wrong, because the part after "location" will be appended to the document root. (i.e. nginx tries to open the file /var/www/apphost.comp.ill.com/app1/home/app1/index.html) I should not have posted it without trying first, sorry.

Have you tried:

server {
    listen 80;
    listen [::]:80;

    index index.html index.htm home home.html;

    # Make site accessible from http://localhost/
    server_name apphost.comp.ill.com;

    location /app1/ {
            root /var/www/apphost.comp.ill.com/app1/home;
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }    
    location /app2/ {
            root /var/www/apphost.comp.ill.com/app2;
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }
}
1
  • That did not work
    – Neo
    Commented Dec 7, 2017 at 20:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .