Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

Nginx - config to route to single page app

I'm using nginx with the deployment of my website on my local server.

The website is a single page create react app running on the server. I have a domain, www.test.com for example, and i want the single page app to be found on www.test.com/first-website. From reading online I'm supposed to use the rewrite directive.

This is my current config:

http {
        upstream sensory-showcase {
                server 127.0.0.1:5000;
        }

        server {
                listen 80;

                location /sensory-solution-for-firefighters {
                        rewrite ^/sensory-solution-for-firefighters $1 break;
                        proxy_pass http://sensory-showcase/;
                }
        }
}

events { }

From this the url resolves without an nginx 500 error however it just shows a blank white page. And i have to have a trailing /, eg www.test.com/first-website/ . Without the trailing / it errors.

I just note, when I didnt have the rewrite directive in, and left the location at just / the site loaded fine.

question from:https://stackoverflow.com/questions/65850526/nginx-config-to-route-to-single-page-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this, it will fallback to the index.html of your single page app, which will do the routing:

       location /first-website {
            try_files $uri $uri.html $uri/ /first-website/index.html;
       }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...