大网数据广告活动
服务器运维问答

nginx can only serve static files from subdirectories, not the website root (500 errors)

· 52CE 我爱测

I'm not using any frameworks or anything. I'm trying to place my style.css in the same directory as the index.html (which loads fine), but it throws a 500 error when I try to access it. So https://example.com/index.html loads fin…

问题原文

原帖链接 · barfurth · CC BY-SA 3.0

I'm not using any frameworks or anything. I'm trying to place my style.css in the same directory as the index.html (which loads fine), but it throws a 500 error when I try to access it. So https://example.com/index.html loads fine (without styles), but accessing https://example.com/style.css throws a 500 Internal Server Error. Strangely enough it works if I put my stylesheets into a subdirectory,so https://example.com/styles/style.css loads fine.

This is my configuration for the site (virtual host):

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;
}

Error.log

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"

Access.log

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"

已采纳解答

原帖链接 · 13dimitar · CC BY-SA 3.0

Having a look at http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files - try by modifying try_files $uri/index.html $uri; to try_files $uri/index.html $uri/ =404;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-01-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。