Setup NGINX to serve to static site on the same domain

When i use this nginx configuration separately – sites open normally

server {

    server_name mysite.com;
    root /myblog;
    index index.html;


    location / {
        try_files $uri $uri/ =404;
    }

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

    server_name mysite.com;
    root /homepage;
    index index.html;


    location / {
        try_files $uri $uri/ =404;
    }

    listen 80;
    listen [::]:80;
}

How to set up the homepage by mysite.com in /homepage folder, and the mysite.com/blog in /myblog folder?

folders for static sites are in different paths.

I tryed alias, proxypass to http.server, try_files $uri – and nothing helped

  • You can make the directory blog as sub-directory of the homepage folder.

    – 

  • @Luuk this option does not suit me, the sites are in different repositories. And one of them is generated by a script.

    – 

  • 1

    you can create a link?: cd homepage; ln -s someplaceToMyblog myblog. This will virtually move the directory, and make it accessible as a subdirectory under homepage.

    – 

  • @Luuk I tried it, only the index.html is loaded, but styles and other files that are in the same folder do not open. Read/write permissions are ok.

    – 

  • Try to add an extra line with the location to your myblog, something like: location /myblog directoryToMyblobg. (more info Serving Static Content)When that does not work more info is needed about that blog…

    – 

Thanks to @Luuk for the idea of linking folder:

cd /homepage
ln -s /myblog blog

After a lot of trying, these are the settings I came up with:

server {

    server_name mysite.com;
    root /homepage;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location /blog {
         index index.html;
         try_files $uri $uri/ =404;
    }

    listen 80;
    listen [::]:80;

}

Leave a Comment