admin管理员组

文章数量:1410705

example url reactjs appp route: /%7Brandomstring%7D




randomstring is actually an id that i use.

Question: How can i use nginx to point to this route.

reactjs build files are under

/var/www/html/

I tried following nginx config:

location \~\* (.\*/home/)(.+)$
{
return 301 ( ; #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;
     }

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;
     }
Share edited Mar 5 at 4:33 Faisal Mahmood asked Mar 4 at 12:13 Faisal MahmoodFaisal Mahmood 133 bronze badges 2
  • What is the physical path to your index.html file? Is it /var/www/html/index.html or /var/www/html/home/index.html (or neither first nor second)? – Ivan Shatsky Commented Mar 4 at 13:22
  • /var/www/html/index.html – Faisal Mahmood Commented Mar 4 at 20:34
Add a comment  | 

1 Answer 1

Reset to default 1

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;
    }
}

本文标签: reactrouterdome nginx routing problem for dynamic string at the end of URLStack Overflow