简体   繁体   中英

How configure nginx for prerender React App?

I've got React app.

It works well on local machine (app + prerender-spa-plugin). I run it with command http-server into ./build package

However thing go wrong on server - it acts like if I launch it with serve-s command.

There is docker with nginx image on server.

I tried to reconfigure nginx the way that it uses different index.html for different URLs, but fail again

Do the problem with routing to directories that keeps static images?

How it could be resolved? or where I could find information about it?

You have to create virtual host on nginx server and point it to build folder of the app. Don't forget to run npm run build .

Simple nginx config

server {

    listen 80;
    listen [::]:80;

    root /var/www/reactjsapp/build;
    index index.html index.htm;

    server_name reactjsapp.com;

    location / {
        try_files $uri /index.html;
    }

        location ~ /\.ht {
            deny all;
        }

}

I decided it. My config

location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

From official documentation:"It is possible to check directory's existence by specifying a slash at the end of a name, eg “$uri/”."

https://i.stack.imgur.com/PusBE.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM