【问题标题】:NGINX 404 error when accessing nested React App routes访问嵌套的 React App 路由时出现 NGINX 404 错误
【发布时间】:2020-06-30 05:12:52
【问题描述】:

背景/目标:

大家好,第一次发帖!

我目前正在尝试配置 NGINX,以便它 -

  1. 在特定路由被命中时提供 React 应用程序,例如/auth/
  2. 将 /api 路由上的所有流量代理到 Node 后端
  3. 在所有其他路线上为 Wordpress 网站提供服务

问题: 我在我的 NGINX 配置文件中定义了一个 /auth 位置块,它指向文件夹 /home/ubuntu/app/client/build 中包含的 React 应用程序。

如果我访问 /auth 路由,NGINX 会正确地为 React 应用程序提供服务。

但是,当我向 嵌套路由(例如 /auth/login)发出 HTTP 请求时,我收到 404 错误。

我已尝试剥离 NGINX 配置文件以仅包含 /auth 位置块,但问题仍然存在。因此,我认为这与文件中的其他设置无关。

指定确切的路线不是一个可行的解决方案,因为我尚未添加的许多其他路线都具有动态嵌套值,例如/users/someuniqueid。

配置文件

希望以下内容能阐明我做错了什么。请注意,出于隐私原因,我已删除服务器名称,因此请不要假设我使用了该实际名称:)

server {
  listen 80;
  server_name myipaddress;
  root /home/ubuntu/wordpress;
  index index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location /css {
    alias /home/ubuntu/app/client/build/css;
  }

  location /media {
    alias /home/ubuntu/app/client/build/media;
  }

  location /static {
    alias /home/ubuntu/app/client/build/static;
  }

  location /assets {
    alias /home/ubuntu/app/client/build/assets;
  }

  location /auth {
    alias /home/ubuntu/app/client/build;
  }

  location ~ \.php$ {
    fastcgi_split_path_info  ^(.+\.php)(/.+)$;
    fastcgi_index            index.php;
    fastcgi_pass             unix:/var/run/php/php7.3-fpm.sock; #Ubuntu 17.10
    include                  fastcgi_params;
    fastcgi_param   PATH_INFO       $fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

  location /api {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  Host       $http_host;
    proxy_pass        http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/myurl.io/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myurl.io/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhusername.pem; # managed by Certbot
}

【问题讨论】:

  • 它是否使用 docker 运行,ngix 在 docker 上运行,节点应用程序也在 docker 中运行?
  • 我没有使用容器。目前它都在一个 Ubuntu EC2 实例上运行。

标签: node.js reactjs wordpress nginx


【解决方案1】:

这个问题here已经提到了。

尝试用这个替换 location /auth 配置。

location /auth/ {
     alias  /home/ubuntu/app/client/build;
     index  index.html;

     try_files $uri $uri/ /index.html?$args;
}
location / {
    alias  WORD_PRESS_PATH_HERE;
    index index.php;

    location ~ \.php$ {
      fastcgi_split_path_info  ^(.+\.php)(/.+)$;
      fastcgi_index            index.php;
      fastcgi_pass             unix:/var/run/php/php7.3-fpm.sock; #Ubuntu 17.10
      include                  fastcgi_params;
      fastcgi_param   PATH_INFO       $fastcgi_path_info;
      fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

感谢阅读。

【讨论】:

  • 感谢您的回复!我尝试了您的建议,不幸的是 NGINX 尝试为 Wordpress 站点提供服务,该站点设置为在默认的“/”路由上运行。 Wordpress 找不到 URI,因此它返回了自己的 404 页面。
  • 请尝试使用alias 而不是root
  • 不幸的是同样的问题。
  • 您可以尝试使用location / { } 包装wordpress 路由配置吗?
  • 我激动了一会,然后意识到我已经隐式指定了 /auth/login 路由。它仍然无法正常工作。现在“/”也返回 403 Forbidden
猜你喜欢
  • 2017-02-14
  • 2018-01-19
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 2017-02-03
相关资源
最近更新 更多