Nginx 反向代理给出缓存 MISS

Nginx 反向代理给出缓存 MISS

我已经为域配置了 nginx 反向代理。但是,当我使用 进行测试时curl,它显示缓存未命中。

curl -I http://www.mixtapemadness.com
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Content-Type: text/html; charset=UTF-8
Date: Wed, 23 Nov 2016 13:58:23 GMT
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Pragma: no-cache
Server: nginx/1.8.1
Set-Cookie: ci_session=2fb702c0f6a7528e1198db89b1df85142ee1bfad; 
expires=Wed, 23-Nov-2016 15:58:21 GMT; Max-Age=7200; path=/; HttpOnly
Vary: Accept-Encoding
X-Proxy-Cache: MISS
Connection: keep-alive

我的nginx.conf

user nginx;
worker_processes 8;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
    worker_connections 1024;
}
http {

log_format rt_cache '$remote_addr - $upstream_cache_status [$time_local]  
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';
 access_log  /var/log/nginx/access.log rt_cache;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    server_names_hash_bucket_size 128;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

gzip                on;
gzip_min_length     1000;
gzip_buffers        4 8k;
gzip_http_version   1.0;
gzip_disable        "msie6";
gzip_types
    text/plain
    text/css
    text/js
    text/xml
    text/javascript
    application/javascript
    application/x-javascript
    application/json
    application/xml
    application/rss+xml
    image/svg+xml;
gzip_vary           on;
gzip_proxied any;
    proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g
                 inactive=60m use_temp_path=off;
    proxy_cache_key "$scheme$host$request_uri";
    include /etc/nginx/conf.d/*.conf;
    index   index.html index.htm;
    client_max_body_size 50M;
    server {


    listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {

       }

    proxy_cache my_cache;
    add_header X-Proxy-Cache $upstream_cache_status;
    proxy_cache_valid  200 302  60m;
    proxy_cache_valid  404      1m;
 proxy_pass       http://127.0.0.1:8080; 
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

domain.conf的如下。

server {
        listen    80;
        server_name  example.com www.example.com;
        access_log /var/log/nginx/mixtape.access.log;
        error_log  /var/log/httpd/yourdomain.com-error_log crit;

location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|html|htm|wml)$ 
{
        root /var/www/html;
        expires 30d;
        }

location / {
        client_max_body_size    50m;
        client_body_buffer_size 128k;

        proxy_send_timeout   90;
        proxy_buffer_size    128k;
        proxy_buffers     4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 256k;
        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 $scheme;
    proxy_cache            my_cache;
    proxy_cache_valid 200 10m;
    add_header X-Proxy-Cache $upstream_cache_status;
        proxy_read_timeout 90;
    proxy_pass   http://127.0.0.1:8080/;
        }
}

答案1

您可以使用 proxy_ignore_headers 来修复这个问题

例子

proxy_ignore_headers Expires Cache-Control Set-Cookie Vary;

答案2

响应中包含“Pragma: no-cache”和“Expires: Wed, 11 Jan 1984 05:00:00 GMT”。缓存永远不会存储它。您应该更改应用程序发送的标头,或者尝试重写标题,这需要使用以下方式构建的 Nginxheaders_more

相关内容