Search code examples
nginxstaticsubdirectory

Using Nginx to serve a static files in a subdirectory


I currently have a bunch of working static files at the domain name khairulslt.me (from NameCheap). Recently, I've tried setting up a subdomain (khairulslt.me/RGBGame) as seen in the code below; However, I keep getting 404 errors. What am i missing out?

server {
  listen 80;

  index circles.html;
  server_name khairulslt.me www.khairulslt.me;

  location / {
  root /var/www/khairulslt.me;
  add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy- 
  revalidate, max-age=0';
  expires off;
  }

  location /RGBGame {
  alias /var/www/RGBGame/colorGame.html;
  index colorGame.html;
  }
}

PS: I want to serve the new files as a working web app under the same Digital Ocean droplet that I'm using for the circles app.


Solution

  • Solved it. Needed to change this block of code here:

    location /RGBGame {
    root /var/www/khairulslt.me;
    index colorGame.html;
    try_files $uri $uri/ /var/www/RGBGame/colorGame.html?q=$uri&$args;
    autoindex off;
    }
    

    Will leave my final configuration here in case it helps anyone:

    server {
    
      server_name khairulslt.me www.khairulslt.me;
    
      autoindex off;
      location / {
        root /var/www/khairulslt.me;
        index circles.html;
      }
    
    
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/khairulslt.me/fullchain.pem; # managed by 
    Certbot
    ssl_certificate_key /etc/letsencrypt/live/khairulslt.me/privkey.pem; # managed 
    by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    location /RGBGame {
        root /var/www/khairulslt.me/RGBGame;
        index colorGame.html;
        try_files $uri $uri/ /var/www/RGBGame/colorGame.html?q=$uri&$args;
        autoindex off;
      }
    
    location /robots.txt { return 200 "User-agent: *\nDisallow: /\n"; 
      }
    
    }
    
     server {
    if ($host = www.khairulslt.me) {
        return 301 https://$host$request_uri;
      } # managed by Certbot
    
    
    if ($host = khairulslt.me) {
        return 301 https://$host$request_uri;
      } # managed by Certbot
    
    
    listen 80;
    server_name khairulslt.me www.khairulslt.me;
    return 404; # managed by Certbot
    }
    

    What this config does:

    1) Serve static files, Web App #1, aka bunch of html/css/js files) at the URL khairulslt.me

    2) Serve second set of static files, Web App #2, aka bunch of html/css/js files) at the URL khairulslt.me/RGBGame