Search code examples
restnginxstaticcreate-react-app

Apply path to statically served content


I'm statically serving a PWA builded with Create-React-App through Nginx.

This is my Nginx location configuration:

location / {
    root /etc/my_app/latest;
    index index.html;
    add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
    expires off;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept' always;
    allow all;
  }

So :

  • When querying https://my_domain_name_app/ it logically find the index.html file in /etc/my_app/latest/index.html
  • When querying https://my_domain_name_app/assets/image.png it logically find the image.png file in /etc/my_app/latest/assets/image.png

But when my PWA's URL is, for example, https://my_domain_name_app/login, Nginx will try to find a file login in /etc/my_app/latest/ and throw a 404 error instead of accessing the /login page of my application

How can I tell Nginx to differenciate the page of my application from the files I serve ?


Solution

  • The answer of Richard Smith is correct, I had to add try_files $uri $uri/ /index.html;