如何在 nginx 中为某些文件扩展名添加缓存控制 header 和代理传递
how to add cache control header with proxy pass in nginx for some file extensions
我喜欢用 nginx 添加缓存控制 header 用于一些扩展名,例如 .jpg 等,但到目前为止,我在网上找到的一些解决方案,我无法让它工作。我会告诉你我的尝试。
我在我网站的 .conf 文件的不同位置尝试了以下变体,当我尝试时网站变成空白,我发现控制台上有很多 404 错误。该站点是在 React 中开发的。
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1d;
add_header Cache-Control "public, no-transform";
}
我的 conf 文件如下所示。问题是我必须做反向代理,因为网站实际上托管在 Docker 容器中。
server {
server_name mysite.net;
root /usr/share/nginx/html;
location / {
proxy_pass http://web:3005/;
}
location /api/ {
rewrite ^/api(/.*)$ break;
proxy_pass http://api:5005/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
fastcgi_read_timeout 1200;
proxy_read_timeout 1200;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mysite.net-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mysite.net-0001/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = mysite.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name mysite.net;
return 404; # managed by Certbot
}
使用map
指令:
map $cache $control {
1 "public, no-transform";
}
map $cache $expires {
1 1d;
default off; # or some other default value
}
map $uri $cache {
~*\.(js|css|png|jpe?g|gif|ico)$ 1;
}
server {
...
expires $expires;
add_header Cache-Control $control;
...
}
(您也可以将 expires
和 add_header
指令放入 location
上下文中,甚至将它们留在 http
上下文中)。如果通过映射表达式为 $control
变量计算的值是空字符串,nginx 根本不会添加 header(或修改现有的 header)。这不是唯一可能的解决方案,您还可以依赖上游的 Content-Type
响应 header(示例请参见 this 答案)。
您应该了解 this 文档摘录:
There could be several add_header
directives. These directives are inherited from the previous configuration level if and only if there are no add_header
directives defined on the current level.
我喜欢用 nginx 添加缓存控制 header 用于一些扩展名,例如 .jpg 等,但到目前为止,我在网上找到的一些解决方案,我无法让它工作。我会告诉你我的尝试。
我在我网站的 .conf 文件的不同位置尝试了以下变体,当我尝试时网站变成空白,我发现控制台上有很多 404 错误。该站点是在 React 中开发的。
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1d;
add_header Cache-Control "public, no-transform";
}
我的 conf 文件如下所示。问题是我必须做反向代理,因为网站实际上托管在 Docker 容器中。
server {
server_name mysite.net;
root /usr/share/nginx/html;
location / {
proxy_pass http://web:3005/;
}
location /api/ {
rewrite ^/api(/.*)$ break;
proxy_pass http://api:5005/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
fastcgi_read_timeout 1200;
proxy_read_timeout 1200;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/mysite.net-0001/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/mysite.net-0001/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = mysite.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name mysite.net;
return 404; # managed by Certbot
}
使用map
指令:
map $cache $control {
1 "public, no-transform";
}
map $cache $expires {
1 1d;
default off; # or some other default value
}
map $uri $cache {
~*\.(js|css|png|jpe?g|gif|ico)$ 1;
}
server {
...
expires $expires;
add_header Cache-Control $control;
...
}
(您也可以将 expires
和 add_header
指令放入 location
上下文中,甚至将它们留在 http
上下文中)。如果通过映射表达式为 $control
变量计算的值是空字符串,nginx 根本不会添加 header(或修改现有的 header)。这不是唯一可能的解决方案,您还可以依赖上游的 Content-Type
响应 header(示例请参见 this 答案)。
您应该了解 this 文档摘录:
There could be several
add_header
directives. These directives are inherited from the previous configuration level if and only if there are noadd_header
directives defined on the current level.