nginxnginx-confignginx-location

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


Solution

  • 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;
    
    }
    

    And carefully check that links to style files (css) and resources(jpeg...) inside the /blog folder are relative

    // Absolute paths for /homepage resources 
    <link rel="stylesheet" href="/styles.css" type="text/css"/>
    
    // Relative paths for /blog resources 
    <link rel="stylesheet" href="styles.css" type="text/css"/>