nginx

react-router-dome nginx routing problem for dynamic string at the end of URL


example url reactjs appp route: https://domain.de/home/%7Brandomstring%7D

https://domain.de/home/123uwb2j3u2

https://domain.de/home/128283283s

randomstring is actually an id that i use.

Question: How can i use nginx to point to this https://domain.de/home/128283283s route.

reactjs build files are under

/var/www/html/

I tried following nginx config:

location \~\* (.\*/home/)(.+)$
{
return 301 https://domain.de/anotherRoute( ; #not really what i want
}

No success with the following too

location \~\* (.\*/home/)(.+)$
{
alias   /var/www/html/;
index  index.html index.js;
try_files $uri $uri/ /index.html?$args;

}

Following worked:

     location /home/ {
         alias   /var/www/html/;
         index  index.html index.js;
         try_files $uri $uri/ /index.html?$args;
     }

Solution

  • if you are not using nginx as a proxy then, the below should work.

    server {
        listen 80;
        server_name domain.de;
        
        root /var/www/html;
        index index.html;
        
        location / {
            try_files $uri $uri/ /index.html;
        }
        
        # specifically for /home/{id} routes
        location ~ ^/home/ {
            try_files $uri $uri/ /index.html;
        }
    }