【问题标题】:500 internal server error Nginx running react app500 内部服务器错误 Nginx 运行反应应用程序
【发布时间】:2019-02-06 15:10:36
【问题描述】:

按照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


}

我的另一台服务器(在同一个文件中)(由lets-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,请求:“GET / HTTP/1.1”,主机:“www.portal.productive.city”

2018/08/31 14:43:08 [错误] 29581#29581: *26 重写或内部重定向周期 while 内部重定向到“/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,请求:“GET /? HTTP/1.1”,主机:“www.portal.productive.city”

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

【问题讨论】:

    标签: nginx


    【解决方案1】:

    您不应该有 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 参数”。

    【讨论】:

    • 服务器在同一个文件中(默认)。只是想澄清一下。我编辑了我的问题以添加该位
    • 现在它重定向到portal.productive.city?我仍然收到 500 个内部错误。检查我的编辑以查看我的服务器的新设置以及错误文件
    • 为什么返回301有$host?那应该是 $scheme 吗?
    • 如何监控?
    • 是同样的错误。我尝试注释掉 if($scheme..) 并完全删除它。然后我重新启动了nginx并清除了缓存。同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2017-08-15
    • 2022-01-12
    • 2015-08-05
    相关资源
    最近更新 更多