Using Nginx to serve a static files in a subdirectory

10,994

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

Share:
10,994
sgeza
Author by

sgeza

Updated on June 04, 2022

Comments

  • sgeza
    sgeza almost 2 years

    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.

    • Alex C
      Alex C almost 6 years
      Check your access.log and you will see where your request is going in file system
    • sgeza
      sgeza almost 6 years
      59.189.202.117 - - [07/Jun/2018:13:39:06 +0800] "GET /RGBGame HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" Yeah I got that, not sure where to start with that though unfortunately
  • sgeza
    sgeza almost 6 years
    Have tried that but still returns error 404 : | Do u know if there are any other instructions i need to perform to set up a subdirectory? Like using Express to set a route or something? Completely new to this sorry for the newbie questions!
  • sgeza
    sgeza almost 6 years
    thanks! have tried both alias and root variants and still 404; I Also cant remove index circles.html; in the 3rd line, otherwise khairulslt.me will not load in the browser
  • nbari
    nbari almost 6 years
    @sgeza do you have files (images) on the defined paths?
  • sgeza
    sgeza almost 6 years
    yeap i do, css/js/html files on the defined paths, i never had to define "running them on my main khairulslt.me to get that one working though