nginx 1.13.3 缓存配置 一直显示没命中
来源:3-20 Nginx作为缓存服务_缓存服务配置语法
troylc
2017-09-25
# cat default.conf upstream webfront { # Tomcat is listening on default 8090 port ip_hash; server web1:8518 ; server web2:8518 fail_timeout=0; } proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off; server { listen 80; server_name xxxxxx.com.cn; return 301 https://$host; ##由 rewrite 修改为 return } server { listen 443 ssl http2; server_name xxxxxx.com.cn; #换成你的域名 ssl on; ssl_certificate /etc/nginx/certificate/xxxxxx.com.cn.crt; #证书文件 ssl_certificate_key /etc/nginx/certificate/xxxxxx.com.cn.key; #秘钥文件 ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; ##访问网站后,由浏览器方记住该域名是受保护的https://,以后的http://访问不用走上边的return location /web/ { proxy_pass http://webfront; proxy_redirect off; 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-Frame-Options DENY; proxy_connect_timeout 60; proxy_read_timeout 3600s; proxy_set_header X-Forwarded-Proto $scheme; } #location /ufile/{ # proxy_pass http://info.xxxxxxx.cn/ufile/; #} location /advisory/{ proxy_pass http://webfront/web/advisory/; proxy_cache my_cache; proxy_cache_key $scheme$proxy_host$request_uri; proxy_cache_valid 200 1d; add_header cache-status $upstream_cache_status; } location /r/cms/{ proxy_pass http://webfront/web/r/cms/; proxy_cache my_cache; proxy_cache_key $scheme$proxy_host$request_uri; proxy_cache_valid 200 1d; add_header cache-status $upstream_cache_status; } location /home/static/ {root /etc/nginx/html;} location /index/static/ {root /etc/nginx/html;} location /home {try_files $uri /home/index.html;} location /index {try_files $uri /index.html;} location = / { rewrite ^ /index; } error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } # cat /etc/nginx/nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 2048; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; server_tokens off; keepalive_timeout 65; gzip on; gzip_comp_level 9; gzip_min_length 100; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif #image/png; gzip_vary on; gzip_static on; include /etc/nginx/conf.d/*.conf; }
访问后的效果是:
写回答
2回答
-
troylc
提问者
2017-09-26
1. nginx不缓存原因
默认情况下,nginx是否缓存是由nginx缓存服务器与源服务器共同决定的, 缓存服务器需要严格遵守源服务器响应的header来决定是否缓存以及缓存的时常。header主要有如下:
复制代码代码如下:
Cache-control:no-cache、no-store
如果出现这两值,nginx缓存服务器是绝对不会缓存的
复制代码代码如下:
Expires:1980-01-01
如果出现日期比当前时间早,也不会缓存。
2. 解决不缓存方案
2.1 方法一:
修改程序或者源服务器web程序响应的header
2.2 方法二:
nginx代理直接加上如下一句:复制代码代码如下:
location /advisory/{ proxy_pass http://webfront/web/advisory/; proxy_cache my_cache; proxy_cache_key $scheme$proxy_host$request_uri; proxy_cache_valid 200 1d; add_header cache-status $upstream_cache_status; proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; }
我这里是因为第一个原因,目前正在找服务器返回时设置的header,暂时解决办法是,第二种解决办法
122017-09-27 -
Jeson
2017-09-25
看配置没有发现什么太大的问题,建议看看你得nginx中的error(/var/log/nginx/error.log)日志,磁盘空间或者/etc/nginx/cache缓存目录权限是否可写,另外,如果是测试环境,尝试将/etc/nginx/cache目录下的内容清理掉,然后请求几次这个页面,观察是否有缓存文件生成。
022017-09-25
相似问题