nginx部署react项目

280 阅读1分钟

1.build项目

build注意要配置webpack的webpack.config.prod.js文件,生产环境的配置(我是使用的less,所以加了个less的loader)

yarn build

2.配置nginx

由于使用的是browserRouter,所以要用nginx把所有路由指向index.html 所以配置以下(我页面放html/build里的)

        location / {
            root   html/build;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html =404;
        }

由于后台接口跨域,所以要用nginx配置跨域

        location /api/console {
            proxy_pass http://127.0.0.1:8080/console;
        }

        location /api {
            proxy_pass http://127.0.0.1:8080;
        }

3.参考

www.cnblogs.com/souvenir/p/… blog.csdn.net/zhangliangz… blog.csdn.net/boonya/arti…