【问题标题】:Nginx 404 error for react route deployed to subdirectory部署到子目录的反应路由的 Nginx 404 错误
【发布时间】:2020-04-15 06:58:54
【问题描述】:

我是 Nginx 的新手。

我构建了 react 项目,以便可以将其部署到子目录。

它在本地 xampp 服务器上运行良好,所有路由都运行良好。

localhost/mysite.com/public

localhost/mysite.com/public/signup

localhost/mysite.com/public/admin

为了托管这个网站,我在云计算机(ubunbu 18.04)上安装了 nginx。

我更新了/etc/nginx/sites-available/default 来设置这个项目的服务器配置。

我的配置如下

server {
    listen 80;
    server_name mySite;
    root /var/www/html/mysite.com/public;

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

一旦我尝试连接站点 (http://{ip address}/mysite.com/public),它就会加载第一页。

但是当我去子路由时,它返回“404 not found”。

所以我更新了服务器配置

server {
    listen 80;
    server_name mySite;
    root /var/www/html/mysite.com/public;

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

    location ~ /mysite.com/public {
        root /var/www/html/mysite.com/public;
        try_files $uri $uri/ /index.html;
    }
}

更新此配置后,由于错误,我无法重新启动 nginx。

我认为是因为配置错误。

但由于对nginx的了解很少,我无法知道原因。

【问题讨论】:

  • root 值似乎有误。文件路径是root 值与URI 的连接,所以如果你想要/mysite.com/public/foo 的URI,请尝试root /var/www/html;。不知道为什么你想要 /mysite.com/public 位在 URI 中?

标签: nginx hosting web-hosting nginx-config


【解决方案1】:

我通过这样做使它与https://www.resonance.how/resonate-peace/resonate 一起工作:

location /resonate-peace/ {
  try_files $uri $uri/ /resonate-peace/index.html =404;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2021-11-15
    • 2019-07-08
    • 2023-01-21
    相关资源
    最近更新 更多