使用nginx反向代理来实现浏览器caching(防止304未修改)

我使用nginx 1.13.5作为在gunicorn应用程序服务器(Django应用程序)之前的反向代理。 我的目标是采用浏览器caching,浏览器根本不要求服务器caching资产(即使不是304 Not Modified )。

我不能得到它的工作,需要专家的意见。 我在我的virutalhost文件中使用了地图块,如下所示:

 # Expires map, outside server blocks map $sent_http_content_type $expires { default off; text/html epoch; ~image/ max; } 

然后在服务器块内,我这样调用它:

expires $expires;

经过testing,我看到我的所有静态资产仍然创build304服务器的调用。 我该怎么办?

请注意,我已经实现了这个 SF问题的答案。


在需要的情况下,我把我的完整的nginx虚拟主机文件下面。 请注意,我将所有stream量redirect到https://example.com 。 httpwww.example.com都不允许。

 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=6m; upstream my_server { server unix:/home/ubuntu/myproj/myapp/myapp.sock fail_timeout=0; } # Expires map map $sent_http_content_type $expires { default off; text/html epoch; ~image/ max; } server { server_name example.com www.example.com; listen 80 reuseport; return 301 https://example.com$request_uri; } server { server_name www.example.com; listen 443 ssl reuseport; listen [::]:443 ssl reuseport; ... SSL related code ... return 301 https://example.com$request_uri; } server { server_name example.com; listen 443 ssl; listen [::]:443 ssl; ... SSL related code ... expires $expires; charset utf-8; underscores_in_headers on; limit_conn conn_limit_per_ip 10; limit_req zone=req_limit_per_ip burst=10 nodelay; error_log /var/log/nginx/https-error_log warn; location ~* \.(?:ico|css|js|gif|jpg|jpeg|png|svg|woff|ttf|eot)$ { root /home/ubuntu/myproj/myapp; access_log off; error_log off; } location = /favicon.ico { access_log off; log_not_found off; } location /static { access_log off; alias /home/ubuntu/myproj/myapp; } location /static/admin/ { alias /home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/admin/static/; } ... other server blocks ... } 

万一它很重要,我一直在testingFirefox 55.0.2(64位)。