使用nginx获取带有react路由器app的404

时间:2017-10-31 00:01:29

标签: reactjs nginx react-router

我有一个反应前端应用程序,它使用react-router创建不同的路由。在开发服务器上它的工作正常,但是当我构建项目时,它给了我404直接访问它的不同路由。网站完美打开xyz.net。当我尝试使用xyz.net/login访问它时,它会给出404。

这是我的nginx conf

server {

listen 80;
server_name  xyz.net www.xyz.net;
root /root/frontend/react/build/;
index index.html;


location /api/ {

    include proxy_params;
    proxy_pass http://localhost:8000/;
}

}

1 个答案:

答案 0 :(得分:1)

当我遇到这个问题时,这对我有用:

location / {
                #...
                try_files $uri $uri/ /index.html$is_args$args;
                #...  
      }

但是,我在使用*.ejs模板进行开发构建的其他项目中遇到了错误,而*.html插件用于使用webpack进行生产构建。我在这里找到答案:

React-router issue when refreshing URLs with SSL Enabled

希望有所帮助。