nginx的缓存问题

大圈 发布于 2016/05/19 15:23
阅读 386
收藏 0
centos6.7 下 nginx之proxy_cache缓存测试:
nginx1.9.15


nginx编译参数:
[root@iZ25dl26ykoZ nginx]# nginx -V
nginx version: nginx/1.9.15
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6 --add-module=../nginx_upstream_check_module-0.3.0 --add-module=../nginx-sticky-module-ng-1.2.5 --add-module=../ngx_cache_purge-2.3


nginx配置文件:

[root@iZ25dl26ykoZ nginx]# grep -v "#" /etc/nginx/nginx.conf| grep -v "^$"

worker_processes  1;

pid        /var/run/nginx.pid;
events {
    use epoll;
    worker_connections  1024;
}

http {
    include       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"'
            '"$upstream_cache_status"';
    access_log  logs/access.log  main;
    sendfile        on;

    proxy_temp_path   /var/cache/nginx/proxy_temp;
    proxy_cache_path /var/cache/nginx/proxy_cache  levels=1:2 keys_zone=cache_one:50m inactive=2d max_size=1g;

    keepalive_timeout  65;
    gzip on;
    gzip_http_version 1.1;
    gzip_min_length  1k;
    gzip_disable "MSIE [1-6].";
    gzip_types text/plain application/x-javascript text/css text/javascript;

    server {
        listen       80;
        server_name  101.200.149.28;
        index  index.html index.htm index.php;
        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~ .*\.(gif|jpg|png|html|css|js|ico|swf|pdf)(.*) {
            root html;
            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_cache cache_one;
            proxy_cache_valid  200 304 301 302 8h;
            proxy_cache_valid 404 1m;
            proxy_cache_valid  any 2d;
            proxy_cache_key $host$uri$is_args$args;
            expires 30d;
        }

    location ~ /purge(/.*) {
           
            allow   127.0.0.1;
            allow   119.40.36.230;
            deny    all;
            proxy_cache_purge  cache_one $host$1$is_args$args;
            error_page 405 =200 /purge$1;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
include vhosts/*.conf;
}



缓存目录权限如下:
[root@iZ25dl26ykoZ nginx]# pwd
/var/cache/nginx

[root@iZ25dl26ykoZ nginx]# ls -lh
total 24K
drwxrwxrwx 2 nginx root 4.0K May 13 23:07 client_temp
drwxrwxrwx 2 nginx root 4.0K May 19 13:55 fastcgi_temp
drwxrwxrwx 2 nginx root 4.0K May 19 13:55 proxy_cache
drwxrwxrwx 2 nginx root 4.0K May 19 13:55 proxy_temp
drwxrwxrwx 2 nginx root 4.0K May  7 16:17 scgi_temp
drwxrwxrwx 2 nginx root 4.0K May  7 16:17 uwsgi_temp

cache 进程已启动。
[root@iZ25dl26ykoZ nginx]# ps -ef | grep nginx
root     19482     1  0 14:06 ?        00:00:00 nginx: master process nginx
nginx    19485 19482  0 14:06 ?        00:00:03 nginx: worker process
nginx    19486 19482  0 14:06 ?        00:00:00 nginx: cache manager process


测试:

请求 http://101.200.149.28/test.html
响应头信息如下:
Cache-Control   max-age=2592000
Connection  keep-alive
Date    Thu, 19 May 2016 07:15:30 GMT
Etag    "573d4fef-33d7"
Expires Sat, 18 Jun 2016 07:15:30 GMT
Last-Modified   Thu, 19 May 2016 05:32:31 GMT
Server  nginx/1.9.15

根据此信息也可以看出浏览器缓存失效时间是25920000s,即30天。第一次访问时状态码为200,后面再访问的话就是304了。

http://101.200.149.28/purge/test.html

我多次访问test.html文件后,然后再访问http://101.200.149.28/purge/test.html,依然提示404,那就说明test.html没有没有被缓存。

也可以看到缓存目录下都是空的。

[root@iZ25dl26ykoZ nginx]# pwd
/var/cache/nginx
[root@iZ25dl26ykoZ nginx]# ls -R proxy_*
proxy_cache:

proxy_temp:
[root@iZ25dl26ykoZ nginx]#

哪位前辈帮忙看看我的问题出在哪里?多谢!
加载中
返回顶部
顶部