
我在配置 Nginx 以正確服務 AngularJS 應用程式時遇到一些問題。伺服器配置如下:
www.example.com we have the landing page of the application
www.example.com/app we have the application itself
應用程式的路徑如下:
/usr/share/nginx/html/example.com/app/
靜態文件位於以下內容:
/usr/share/nginx/html/example.com/app/public/app/assets
現在我想將登陸頁面和應用程式中的所有 html 檔案的快取設定為“無快取”,但將所有 js、css 和映像檔的快取設定為 60 天。
這是我目前的 nginx 伺服器設定:
server {
listen 80;
index index.html;
server_name example.com www.example.com;
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 60d;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location ^~ /app {
alias /usr/share/nginx/html/example.com/app/public/;
expires -1;
add_header Pragma "no-cache";
}
location / {
root /usr/share/nginx/html/example.com;
expires -1;
add_header Pragma "no-cache";
add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
}
}
現在的問題是位置指令:
location ~* \.(?:ico|css|js|gif|jpe?g|png)$
永遠不會執行,因此快取被設定為 /app 指令中定義的無快取。
任何想法?
謝謝
答案1
引用 nginx 文件:
如果最長匹配前綴位置具有“^~”修飾符,則不檢查正規表示式。
所以,這裡的問題就是你的location ^~ /app
定義。此^
修飾符使 nginx 忽略影像的正規表示式。
你應該改用location /app
。這裡不需要正規表示式匹配,簡單的前綴匹配就足夠了。
答案2
我猜問題是“~*”,嘗試用“~”。例子:
location ~ \.(?:ico|css|js|gif|jpe?g|png)$