nginx 只能提供来自子目录的静态文件,而不能提供来自网站根目录的静态文件(500 错误)

nginx 只能提供来自子目录的静态文件,而不能提供来自网站根目录的静态文件(500 错误)

我没有使用任何框架或任何东西。我试图将 style.css 放在与 index.html 相同的目录中(加载正常),但当我尝试访问它时,它会抛出 500 错误。所以https://example.com/index.html加载正常(无样式),但访问https://example.com/style.css抛出 500 内部服务器错误。奇怪的是,如果我将样式表放入子目录中,它就会起作用,所以https://example.com/styles/style.css加载良好。

这是我对网站(虚拟主机)的配置:

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        return 301 https://$host$request_uri;
    }

    location /.well-known {
        alias /usr/share/nginx/html/.well-known;
        allow 0.0.0.0;
        deny all;
    }
}

server {
    listen 443;
    server_name example.com www.example.com;

    root /home/deployer/example.com;

    ssl on;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    include includes/ssl;

    try_files $uri/index.html $uri;

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

错误日志

2017/01/13 06:25:46 [error] 1279#0: *182 rewrite or internal redirection cycle while internally redirecting to "/style.css", client: 0.0.0.0, server: example.com, request: "GET /style.css HTTP/1.1", host: "example.com"
2017/01/13 06:25:47 [error] 1279#0: *183 rewrite or internal redirection cycle while internally redirecting to "/favicon.ico", client: 0.0.0.0, server: example.com, request: "GET /favicon.ico HTTP/1.1", host: "example.com", referrer: "https://example.com/style.css"

访问日志

0.0.0.0 - - [13/Jan/2017:06:25:46 -0500] "GET /style.css HTTP/1.1" 500 603 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"
0.0.0.0 - - [13/Jan/2017:06:25:47 -0500] "GET /favicon.ico HTTP/1.1" 500 603 "https://example.com/style.css" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"

答案1

看看http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files- 尝试修改try_files $uri/index.html $uri;try_files $uri/index.html $uri/ =404;

相关内容