2017-07-25 246 views
0

我有一個以example.com作爲根的conf文件。在example.com目錄中,有一個html,css,img和js文件夾。我知道這是偏離傳統的HTML目錄作爲根。我嘗試了許多不同的配置(使用基於文件類型,變量的正則表達式等),但我總是得到太多的重定向錯誤。任何人都可以幫助一個良好的配置文件這種類型的目錄結構?這是我的conf文件。導致過多重定向的Nginx配置

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 
    # return 301 https://$server_name$request_uri; 

    listen 443 ssl http2 default_server; 
    listen [::]:443 ssl http2 default_server; 
    include snippets/ssl-example.com.conf; 
    include snippets/ssl-params.conf; 

    server_name www.example.com example.com; 

    root /var/www/www.example.com/; 
    index index.php index.html; 
    client_max_body_size 100m; 

    error_page 404 = error.html?error=404; 

    location ~ /.well-known { 
      allow all; 
    } 

    location/{ 
      location ~* \.(html|php)$ { 
        root html/; 
      } 
      location ~* \.css$ { 
        root css/; 
      } 
      location ~* \.js$ { 
        root js/; 
      } 
      location ~* \.(png|jpeg|gif)$ { 
        root img/; 
      } 
      try_files $uri =404; 
    } 
} 

在此先感謝您的幫助!

+0

我認爲它應該工作。它出什麼問題了?什麼是日誌文件? – uzsolt

+0

我在回答中發佈了問題。它必須處理導致重定向循環的相對路徑。 – Quintin

回答

0

這是我最後使用的配置:

server { 
    listen 80 default_server; 
    listen [::]:80 default_server; 
    # return 301 https://$server_name$request_uri; 

    listen 443 ssl http2 default_server; 
    listen [::]:443 ssl http2 default_server; 
    include snippets/ssl-example.com.conf; 
    include snippets/ssl-params.conf; 

    server_name www.example.com example.com; 

    root /var/www/www.example.com/; 
    index index.php index.html; 
    client_max_body_size 100m; 

    error_page 404 = /html/error.html?error=404; 

    location ~ /.well-known { 
      allow all; 
    } 

    location =/{ 
      try_files /html/index.html =404; 
    } 

    location/{ 
      location ~* \.(html|php)$ { 
        try_files $uri /html/$uri =404; 
      } 
      location ~* \.css$ { 
        try_files $uri /css/$uri =404; 
      } 
      location ~* \.js$ { 
        try_files $uri /js/$uri =404; 
      } 
      location ~* \.(png|jpeg|gif)$ { 
        try_files $uri /img/$uri =404; 
      } 
      try_files $uri =404; 
    } 

}

我的問題是,我所有的重定向的使用,而不是絕對路徑功能相對路徑(如try_files HTML/$ URI)從網站根目錄(/ html/$ uri)。這導致像/ html/html/html/...

我認爲,如果我使用絕對路徑,它將是絕對的服務器的根,而不是該網站。

我現在唯一的問題是,我的錯誤頁面重定向(?錯誤= 404)上的參數不適用於絕對路徑,但這不是一個大問題。