使用Nginx的React Web App无法正确重定向

时间:2018-08-31 13:38:25

标签: nginx

我正在尝试使用nginx在数字海洋上部署React Web App。

我的根目录指向我的git repo中的build文件夹。这样,每当我进行更改时,我都可以通过git pull更新我的应用。

起初,一切正常,但后来我进行了git pull,现在出现重定向错误或500个错误。我不确定发生了什么。

如果我在浏览器中打开网站到主页,它只会显示空白屏幕。如果我尝试访问子域,则会显示500内部错误。

我的网站可用/默认文件如下:

server {
    server_name portal.productive.city www.portal.productive.city;
    root /www/Productive-Website/my-app/build;
    index index.html index.htm;
    rewrite ^/(.*)/$ $1 permanent;
    location / {
       try_files $uri $uri/ /index.php?$args;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/portal.productive.city/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/portal.productive.city/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

当我检查错误日志时,我不断看到

  

2018/08/31 13:32:26 [错误] 29118#29118:* 13重写或内部   内部重定向到“ /index.php”时进行重定向循环,   客户端:74.105.149.67,服务器:portal.productive.city,请求:“获取   / signin / signin HTTP / 1.1”,主机:“ www.portal.productive.city”,   引荐来源网址:“ https://www.portal.productive.city/service-worker.js

如果我运行nginx -t,则表明我的配置正常并且成功。

1 个答案:

答案 0 :(得分:1)

看起来index.php上不存在/www/Productive-Website/my-app/build,也许它应该是.html.htm文件?这使您的网络服务器尝试呈现index.php,并且由于它不存在,您将被重定向到index.php。如果是这样,它将在任何路径上发生。

编辑该文件,以便您的位置文件如下所示:

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

这意味着,“在收到请求时,搜索与$uri OR $uri/ OR $uri.html相匹配的文件,如果没有一个起作用,则渲染/index.html?$args”。这意味着您的React应用程序应管理/index.html上的所有错误,因此请充分满足您的应用程序要求。