【问题标题】:Getting 404 with react router app with nginx [duplicate]使用 nginx 使用反应路由器应用程序获取 404 [重复]
【发布时间】:2025-12-02 03:55:01
【问题描述】:

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

这是我的 nginx 配置文件

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

}

【问题讨论】:

标签: reactjs nginx react-router


【解决方案1】:

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

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

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

React-router issue when refreshing URLs with SSL Enabled

希望对您有所帮助。

【讨论】:

  • 感谢您的提醒。但是,我没有看到与链接到内部 S/O 链接有关的任何不良做法(在您提供的链接中)。实际上这似乎很常见,你能指出一个明确说明这是禁止的参考吗?
  • 没问题!尽管如此,我还是很欣赏这种洞察力。干杯!
  • 谢谢,它成功了。