500内部服务器错误Nginx运行React App [英] 500 internal server error Nginx running react app

查看:100
本文介绍了500内部服务器错误Nginx运行React App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

答案之后,我已经将我的nginx服务器设置为:

Following this answer, I have my nginx server set up like so:

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创建)是:

My other server (in the same file) (created by lets-encrypt) is:

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.city www.portal.productive.city/signin 我收到 500内部服务器错误

When I try to go to: www.portal.productive.city or www.portal.productive.city/signin I get a 500 Internal Server Error

我的错误日志文件如下:

My error-log file looks like:

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 [error] 29581#29581: *25 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 74.105.149.67, server: portal.productive.city, request: "GET / HTTP/1.1", host: "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/"

2018/08/31 14:43:08 [error] 29581#29581: *26 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 74.105.149.67, server: portal.productive.city, request: "GET /favicon.ico HTTP/1.1", host: "www.portal.productive.city", referrer: "https://www.portal.productive.city/"

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

The favicon.ico exists under path/to/repo/build

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

I cleared cache and restructured the server as follows:

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 [error] 29789#29789: *17 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 74.105.149.67, server: portal.productive.city, request: "GET /? HTTP/1.1", host: "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/?"

2018/08/31 15:17:54 [error] 29789#29789: *18 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 74.105.149.67, server: portal.productive.city, request: "GET /favicon.ico HTTP/1.1", host: "www.portal.productive.city", referrer: "https://www.portal.productive.city/?"

推荐答案

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

You shouldn't have 2 server files with the same server_name, this makes your configuration harder to find. And, if by chance you configure both of them to listen to the same ports, it will confuse NGINX on which one to render. First, move them into the same one and redirect based on $scheme instead of $host. But that is not the real issue. You've got an redirect that is looping because it matches every request ending with '/'

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参数".

The https redirect like this means: "If the $scheme is not 'https', redirect to https://$host$request_uri?$args". Which translates to: "https:// address on the same host, on the same path and whith the same URL arguments used on the access".

这篇关于500内部服务器错误Nginx运行React App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆