0

currently I'm using the nginx to serve the static files of my API docs (mywebsites.com), I want to add another website : mywebsites.com/subwebsite.
so I edited the nginx config file to serve both of them :

server {
listen      0.0.0.0:80;
server_name mywebsite.com;

client_max_body_size 1m;
access_log            /var/log/nginx/error.log;
error_log             /var/log/nginx/static.log;

#location ~ /\.git {
 #  deny all;
#}

location /subwebsite {
    root  /home/api/portal/build;
    index index.html index.htm;

    try_files $uri $uri/ =404;
}



 location / {
    root  /home/api/application/public;
    index index.html index.htm;

   try_files $uri $uri/ =404;
 }




#sendfile off;
}

The problem is when I try to access the new website : mywebsite.com/subwebsite .. I got 404 not found.
And when I try to change the current server to forward to the new subwebsite (instead of adding location /subwebsite, I change the root for location /) it works.
the original file:

server {
listen      0.0.0.0:80;
server_name mywebsite.com;

client_max_body_size 1m;
access_log            /var/log/nginx/error.log;
error_log             /var/log/nginx/static.log;

location ~ /\.git {
    deny all;
}

location ~ {
    root  /home/api/application/public;
    index index.html index.htm;

    try_files $uri $uri/ =404;
}

sendfile off;
}


What I'm missing here ? Thanks in advance

Rawhi
  • 6,155
  • 8
  • 36
  • 57
  • Can you explain `And when I try to change the current server to forward to the new subwebsite` more? – Shawn C. May 07 '19 at 13:31
  • instead of adding: location /subwebsite { .... etc, I change the root value of the current location /{ ... to be the index file of the subwebsite, I edited the question – Rawhi May 07 '19 at 13:34
  • Nginx expects to find the file at `/home/api/portal/build/subwebsite/index.html`. If this is the wrong path, you may need to use [`alias`](http://nginx.org/en/docs/http/ngx_http_core_module.html#alias) instead of `root`. – Richard Smith May 07 '19 at 14:31

1 Answers1

0

I think, this variant of location, could work for you:

location /subwebsite {
    root  /home/api/portal/build;
    try_files $uri /index.html;
}

It seems that such config file could be more readable:

server {
    listen      0.0.0.0:80;
    server_name mywebsite.com;

    root  /home/api/application/public;
    index index.html index.htm;

    client_max_body_size 1m;
    access_log            /var/log/nginx/error.log;
    error_log             /var/log/nginx/static.log;

    #location ~ /\.git {
     #  deny all;
    #}

    location /subwebsite {
        root  /home/api/portal/build;
        try_files $uri /index.html;
    }

    #sendfile off;
}
metallic
  • 361
  • 2
  • 4