wicccked
wicccked

Reputation: 389

nginx: location of static content within a subfolder

I'm trying to set up an nginx server to serve a React app at the address http://mydomain/memorygame

Currently I have the following nginx routing config:

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  root /var/www/mydomain.com;
  index index.html;
  server_name mydomain.com www.mydomain.com;

  location /memorygame {
    root /var/www/mydomain.com/memorygame;
    try_files $uri /$uri $uri/ /index.html $uri/index.html  =404;

    location ~* \.(css|js)$ {
      try_files $uri /$uri  =404;
    }
  }

The css files are stored under /var/www/mydomain.com/memorygame, in the index.html the link is /static/css/main.d5dd0bd5.css.

The index.html load fine, but the css requests aren't routed to where I want them. If I take out "/index.html" from try_files, the css loads, but index.html doesn't. How can I make them both work at the same time?

(My guess is that part of the problem is that css request issued goes to mydomain.com/static/css/style.css instead of mydomain.com/memorygame/static/css/style.css, but I might be wrong. If I'm correct, is there a way to auto-prepend the /static/css/style.css to be relative to the mydomain.com/memorygame folder?)

Thanks in advance!

Upvotes: 2

Views: 2598

Answers (1)

wicccked
wicccked

Reputation: 389

I figured it out myself. Deleting everything out of the location /memorygame block did the trick. Nginx does what I was trying to achieve by default.

Upvotes: 1

Related Questions