CentOS,Nginx - 缓存文件夹为空

CentOS,Nginx - 缓存文件夹为空

我已经在 DigitalOcean 上的 CentOS7.2 上设置了 nginx 网络服务器(单个站点)。

按照一些操作指南,我成功设置了 nginx 和缓存,但尽管反复访问我的网站,我的缓存文件夹仍然是空的。

没有报告错误并且其他一切正常。

nginc.conf如下:


# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 2048;
    multi_accept       on;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   15;
    gzip_vary           on;
    gzip_types          text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    client_body_buffer_size 128K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    #include /etc/nginx/conf.d/*.conf; --> nothing there, hence commenting

    fastcgi_cache_path /etc/nginx-cache levels=1:2 keys_zone=PHPCACHE:1024m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";

    server {
                listen          80;
                listen          [::]:80;
                server_name     www.31west.net;
                return 301      https://www.31west.net$request_uri;
    }
    server {
                listen          80;
                listen          [::]:80;
                server_name     31west.net;
                return 301      https://www.31west.net$request_uri;
    }   

# Settings for a TLS enabled server.

    server {
                listen       443 ssl http2 default_server;
                listen       [::]:443 ssl http2 default_server;
                server_name  www.31west.net;

                root        /usr/share/nginx/html;
                index       index.php index.html index.htm;
                error_log   /var/log/error_log info;

                ssl on;
                ssl_certificate "/etc/nginx/ssl/31West-bundle.crt";
                ssl_certificate_key "/etc/nginx/ssl/31west.key";

                ssl_prefer_server_ciphers on;
                ssl_session_cache shared:SSL:10m;
                ssl_session_timeout 10m;
                ssl_buffer_size 16k;
                ssl_ciphers HIGH:!aNULL:!MD5;

                if ($host = '31west.net' ) {
                    rewrite  ^/(.*)$  https://www.31west.net/$1  permanent;
                }

                #Cache everything by default
                set $no_cache 0;
                #Don't cache POST requests
                if ($request_method = POST)
                {
                    set $no_cache 1;
                }
                #Don't cache if the URL contains a query string
                if ($query_string != "")
                {
                    set $no_cache 1;
                }
                #Don't cache the following URLs
                if ($request_uri ~* "/(administrator/|login.php)")
                {
                    set $no_cache 1;
                }
                #Don't cache if there is a cookie called PHPSESSID
                if ($http_cookie = "PHPSESSID")
                {
                    set $no_cache 1;
                }

                open_file_cache          max=10000 inactive=10m;
                open_file_cache_valid    20m;
                open_file_cache_min_uses 5;
                open_file_cache_errors   on;
                large_client_header_buffers 4 16k;

                location ~ \.php$ {
                    fastcgi_cache PHPCACHE;
                    fastcgi_cache_valid 200 60m;
                    fastcgi_cache_methods GET HEAD;
                    add_header X-Fastcgi-Cache $upstream_cache_status;
                    fastcgi_cache_bypass $no_cache;
                    fastcgi_no_cache $no_cache;
                    try_files $uri $uri/ =404;
                    fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                    fastcgi_buffer_size 128k;
                    fastcgi_buffers 256 16k;
                    fastcgi_busy_buffers_size 256k;
                    fastcgi_temp_file_write_size 256k;
                    include fastcgi_params;
                }

                # Load configuration files for the default server block -- nothing there, hence commented
                #include /etc/nginx/default.d/*.conf;

                location / {
                    try_files $uri $uri/ =404;
                }

                location /blog/ {
                    try_files $uri $uri/ /blog/index.php?$args;
                }

                error_page 404 /404.php;
                    location = /40x.php {
                }

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

                location ~*  \.(jpg|jpeg|png|gif|ico|css|js|woff)$ {
                    expires 1w;
                }

                # We enable the Gzip compression for some mime types
                gzip                    on;
                gzip_disable            "msie6";
                gzip_min_length         1100;
                gzip_buffers            4 32k;
                gzip_proxied            expired no-cache no-store private auth;
                gzip_comp_level         5;
                gzip_types              text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss application/x-javascript text/javascript;
                gzip_vary               on;
        }
    }

请帮我理解我可能遗漏了什么。我正在尝试将 TTFB 缩短至 100 毫秒以下(目前为 420 毫秒)

提前致谢!

答案1

看起来它运行良好。

测试我的网站页面得到以下结果,因此造成混淆。

curl -I https://www.31west.net
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Fri, 12 May 2017 12:05:25 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.16
X-XSS-Protection: 0
Set-Cookie: PHPSESSID=d4vsle1tbersa504vpfr058ln7; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-    check=0
Pragma: no-cache
X-Fastcgi-Cache: MISS

使用小型演示页面进行测试得出以下结果。

curl -I https://www.31west.net/a.php
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Fri, 12 May 2017 12:17:25 GMT
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.16
X-Fastcgi-Cache: HIT

但即使只有 10 字节测试页面和上述 nginx 配置,托管在 2 GB RAM、2 核处理器硬件的 DO 上,我的 TTFB 也为 430 毫秒

我已经用尽所有可能的配置来改进它。

任何建议都将不胜感激。

相关内容