NGINX缓存始终返回status = MISS

时间:2017-08-16 18:42:11

标签: django nginx gunicorn

我遇到的问题是NGINX服务器返回MISS以便后续请求到GUNICORN / django后端。我还没有得到一个HIT。

使用来自原始服务器的适当到期时间设置HTTP标头。我已经检查了所有配置文件,但没有发现任何错误。

这就是我在服务器上运行一个简单请求时看到的内容(顺便说一句,我在远程请求时得到了相同的结果):

http -p HBh http://10.1.1.5:443/api/1/vehicles

GET /api/1/vehicles HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: 10.1.1.5:443
User-Agent: HTTPie/0.9.6



HTTP/1.1 200 OK
Allow: OPTIONS, GET
Cache-Control: max-age=3600
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json
Date: Wed, 16 Aug 2017 18:27:30 GMT
Expires: Wed, 16 Aug 2017 19:27:30 GMT
Server: nginx/1.10.3 (Ubuntu)
Transfer-Encoding: chunked
X-Cache-Status: MISS                            <-- :(
X-Cache-Upstream-Connect-Time: 0.000
X-Cache-Upstream-Header-Time: 0.010
X-Cache-Upstream-Response-Time: 1502908050.768  <--- ?
X-Frame-Options: SAMEORIGIN
X-Request-Time: 0.010
X-SG-LOCATION: /api/
X-SG-URI: /api/1/vehicles
X-Upstream-Addr: unix:/srv/API/fleet_api/backend/mysite/mysite.sock

注意:对于某些情况,上游响应时间不正确是NGINX中的已知错误。在这种情况下,可能是因为没有正确配置。

这是我的/etc/nginx/nginx.conf文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 1024;
}

http {
        ##
        # Basic Settings
        ##
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Caching
        ##
        proxy_cache_path /var/nginx/sg_cache levels=1:2 keys_zone=api_zone:10m;


        ##
        # Logging Settings
        ##
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##
        gzip on;
        gzip_disable "msie6";   
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_http_version 1.0;

        ##
        # Virtual Host Configs
        ##
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

这是/etc/nginx/sites-available/api

server {
    listen 10.1.1.5:443;
    server_name server_domain_or_IP;
    root /srv/API/fleet_api/frontend/www;
    location = /favicon.ico { access_log off; log_not_found off; }

    gzip on;
    gzip_min_length 500;
    gzip_proxied any;
    gzip_types
        text/html
        text/css
        text/javascript
        text/plain
        application/javascript
        application/x-javascript
        application/json;

    proxy_cache api_zone;
    proxy_cache_lock on;            
    proxy_connect_timeout 20s;      
    proxy_cache_valid 404 1m;  

    location /api/ {
        proxy_cache api_zone;
        include proxy_params;
        proxy_pass http://unix:/srv/API/fleet_api/backend/mysite/mysite.sock;

        add_header X-SG-LOCATION /api/ always;
        add_header X-SG-URI $uri always;
        add_header X-SG-GZIP-RATIO $gzip_ratio always;
        add_header X-Request-Time $request_time;
        add_header X-HTTP-REQUEST-Cache-Control $http_cache_control;

        add_header X-Cache-Upstream-Response-Time $upstream_response_time;
        add_header X-Cache-Upstream-Connect-Time $upstream_connect_time;
        add_header X-Cache-Upstream-Header-Time $upstream_header_time;

        add_header X-Upstream-Addr $upstream_addr;
        add_header X-Upstream-Expires $upstream_http_expires;
        add_header X-Upstream-Cache-Control $upstream_http_cache_control;

        #
        # Commenting out the next three sections does not resolve problem of getting status=MISS for each request
        #

        # Don't pass on these headers from client to upstream/proxied server (Django)
        proxy_set_header Cache-Control "";
        proxy_set_header Expires "";
        proxy_set_header Vary "";

        # Hide headers from upstream server
        proxy_hide_header Cache-Control;
        proxy_hide_header Expires;
        proxy_hide_header Vary;

        # Ignore the following headers from upstream server
        proxy_ignore_headers Cache-Control;
        proxy_ignore_headers Expires;
        proxy_ignore_headers Vary;

        expires 3600;

        add_header X-Cache-Status $upstream_cache_status;

        location /api/1 {
            expires 60;
            proxy_cache api_zone;
            proxy_pass http://unix:/srv/API/fleet_api/backend/mysite/mysite.sock;
        }

        location /api/1/vehicles {
            proxy_cache api_zone;
            expires 3600;
            proxy_pass http://unix:/srv/API/fleet_api/backend/mysite/mysite.sock;
        }

        # More endpoints...
    }
}

这来自access.log

10.1.1.5 - - [16/Aug/2017:18:27:25 +0000] "GET /api/1/vehicles HTTP/1.1" 200 73 "-" "HTTPie/0.9.6"
10.1.1.5 - - [16/Aug/2017:18:27:27 +0000] "GET /api/1/vehicles HTTP/1.1" 200 73 "-" "HTTPie/0.9.6"
10.1.1.5 - - [16/Aug/2017:18:27:28 +0000] "GET /api/1/vehicles HTTP/1.1" 200 73 "-" "HTTPie/0.9.6"
10.1.1.5 - - [16/Aug/2017:18:27:29 +0000] "GET /api/1/vehicles HTTP/1.1" 200 73 "-" "HTTPie/0.9.6"
10.1.1.5 - - [16/Aug/2017:18:27:29 +0000] "GET /api/1/vehicles HTTP/1.1" 200 73 "-" "HTTPie/0.9.6"
10.1.1.5 - - [16/Aug/2017:18:27:30 +0000] "GET /api/1/vehicles HTTP/1.1" 200 73 "-" "HTTPie/0.9.6"
10.1.1.5 - - [16/Aug/2017:18:27:30 +0000] "GET /api/1/vehicles HTTP/1.1" 200 73 "-" "HTTPie/0.9.6"

我在这里提供了最简单的方案,但我运行了类似的测试,使用Chrome浏览器监控来自Web应用程序的响应,并且即使在到期前请求api端点,也始终获得status = MISS。

0 个答案:

没有答案