Nginx

nginx 只能提供來自子目錄的靜態文件,而不是網站根目錄(500 錯誤)

  • January 13, 2017

我沒有使用任何框架或任何東西。我試圖將我的 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"

查看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;

引用自:https://serverfault.com/questions/826079