2017-06-30 91 views
0

我正在嘗試提供維護頁面。所以我給了try_files一試,總是爲maintenance.html服務。如果url具有.html擴展名,則Nginx不重定向

如果url像app.amazing.com或類似app.amazing.com/[a-z0-9]*的東西,但只要有html擴展名,Nginx會嘗試提供這個文件(例如:app.amazing.com/test.html),並返回一個404,如果它不存在。

server { 
    listen [::]:443; 
    listen 443; 

    server_name app.amazing.com; 
    root /var/www/front/public; 
    include /etc/nginx/conf.d/expires.conf; 

    charset utf-8; 

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

我也試過這樣的重寫:

location/{ 
     rewrite (.*) /maintenance.html break; 
} 

我甚至試過這種if-based solution,但似乎沒有任何工作。我錯過了什麼?

回答

1

UPDATE:測試和工作

server { 
    listen [::]:443; 
    listen 443; 

    server_name app.amazing.com; 
    root /var/www/front/public; 
    include /etc/nginx/conf.d/expires.conf; 

    charset utf-8; 

    set $maintenance on; 
    if ($uri ~* \.(ico|css|js|gif|jpe?g|png|html)(\?[0-9]+)?) { 
     set $maintenance off; 
    } 
    if ($maintenance = on) { 
     return 503; 
    } 
    error_page 503 @maintenance; 
    location @maintenance { 
     rewrite ^(.*)$ /maintenance.html break; 
    } 
    location/{ 
     # here hoes your usual location/rules 
     try_files $uri $uri/ =404; 
    } 

} 

請404碼改變爲503,是這樣的:

server { 
    listen [::]:443; 
    listen 443; 

    server_name app.amazing.com; 
    root /var/www/front/public; 
    include /etc/nginx/conf.d/expires.conf; 

    charset utf-8; 

    location/{ 
     try_files $uri $uri/ =404; 
    } 
} 

404 = NOT FOUND

UPDATE: 302 = TEMPORARY REDIRECT

+0

503不重定向。應該使用3XX。 – diarpi

+0

謝謝@diarpi的更正。 –

+0

我以爲'= 404'只是爲了防止'maintenance.html'不存在。但是對於重定向,它更準確。無論如何,這並沒有改變這樣一個事實,即輸入'app.amazing.com/test.html'仍然會返回404。 – Buzut

相關問題