500个内部服务器错误Nginx运行React应用

时间:2018-08-31 14:48:55

标签: nginx

在回答this之后,我像这样设置了我的Nginx服务器:

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?$args $uri/ $uri.html?$args /index.html?$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


}

我的其他服务器(在同一文件中)(由let-encrypt创建)是:

server {
    if ($host = www.portal.productive.city) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = portal.productive.city) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name portal.productive.city www.portal.productive.city;
    return 404; # managed by Certbot
}

当我尝试转到:www.portal.productive.citywww.portal.productive.city/signin时,出现 500内部服务器错误

我的错误日志文件如下:

  

2018/08/31 14:43:08 [错误] 29581#29581:* 25重写或内部   内部重定向到“ /index.html”时进行重定向循环,   客户端:74.105.149.67,服务器:portal.productive.city,请求:“获取/   HTTP / 1.1”,托管:“ www.portal.productive.city”

     

2018/08/31 14:43:08 [错误] 29581#29581:* 26重写或内部重定向周期   内部重定向到“ /index.html”,客户端:74.105.149.67,   服务器:portal.productive.city,请求:“ GET /favicon.ico HTTP / 1.1”,   主持人:“ www.portal.productive.city”,引荐来源:   “ https://www.portal.productive.city/

favicon.ico位于path/to/repo/build

编辑:我清除了缓存并按如下所示重组了服务器:

server {
    server_name portal.productive.city www.portal.productive.city;
    root /www/Productive-Website/my-app/build;
    index index.html index.htm;
    location / {
        try_files $uri?$args $uri/ $uri.html?$args /index.html?$args;
    }

    listen 80;
    if ($scheme != "https") {
        return 301 https://$host$request_uri?$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 15:17:54 [错误] 29789#29789:* 17重写或内部   内部重定向到“ /index.html”时进行重定向循环,   客户端:74.105.149.67,服务器:portal.productive.city,请求:“获取   /? HTTP / 1.1”,托管:“ www.portal.productive.city”

     

2018/08/31 15:17:54   [错误] 29789#29789:* 18重写或内部重定向周期   内部重定向到“ /index.html”,客户端:74.105.149.67,   服务器:portal.productive.city,请求:“ GET /favicon.ico HTTP / 1.1”,   主持人:“ www.portal.productive.city”,引荐来源:   “ https://www.portal.productive.city/?”

1 个答案:

答案 0 :(得分:0)

您不应有2个具有相同server_name的服务器文件,这会使您的配置更难找到。而且,如果您偶然将它们都配置为侦听相同的端口,则会使NGINX呈现在哪个端口上感到困惑。首先,将它们移到同一位置,然后根据$scheme而不是$host进行重定向。但这不是真正的问题。您有一个正在循环的重定向,因为它匹配以'/'

结尾的每个请求
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; Your REAL problem is here. You've got an redirect that is looping because it matches every request ending with '/'
    location / {
        try_files $uri?$args $uri/ $uri.html?$args /index.html?$args;
    }
    listen 80;
    if ($scheme != "https") {
        return 301 https://$host$request_uri?$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
}

https重定向是这样的:“如果$scheme不是'https',则重定向到https://$host$request_uri?$args”。表示为:“ {https://”位于同一主机上的地址,位于同一路径上,并且访问使用相同的URL参数”。