【问题标题】:How to configure basic FastCGI caching on nginx (Ubuntu 16.04)如何在 nginx (Ubuntu 16.04) 上配置基本的 FastCGI 缓存
【发布时间】:2016-12-06 15:29:37
【问题描述】:

我正在尝试使用基本的 FastCGI 缓存,但在关注 this tutorial 后遇到了麻烦。

在全新安装 Ubuntu 16.04 时,我运行了以下命令:

apt-get update
apt-get install -y nginx
apt-get install -y php-fpm

然后我将/etc/nginx/sites-available/default 更改为:

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

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html;

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

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include snippets/fastcgi-php.conf;
    #   fastcgi_cache MYAPP;
    #   fastcgi_cache_valid 200 60m;
    }
}

在服务器文档根目录 (/var/www/html) 中添加一个名为 time.php 的文件后,其内容如下:

<?php echo time();

并导航IP/time.php,文件执行并显示时间戳。重新加载时,会显示新的时间戳。

如果我取消注释以上行,则只会加载带有 <html><body></body></html> 的空白屏幕。

为什么要加:

        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 60m;

导致空白的html页面?如何解决此问题以缓存 time.php 并将缓存的版本提供给未来的请求?

注意:我确实看到 /etc/nginx/cache 已创建并包含数据。我将目录更改为777权限以消除权限问题。

【问题讨论】:

  • 你试过重启nginx吗? /etc/init.d/nginx 重启
  • 是的,我尝试了许多不同的配置(每次都重新启动),但没有任何效果。

标签: php ubuntu caching nginx fastcgi


【解决方案1】:
$ sudo mkdir -p /var/cache/nginxfastcgi
$ chown www-data: /var/cache/nginxfastcgi

然后在您的配置中

    fastcgi_cache_path /var/cache/nginxfastcgi levels=1:2 keys_zone=fastcgicache:10m inactive=10m max_size=64m;
    fastcgi_cache_key $scheme$request_method$host$request_uri;

    fastcgi_cache_lock on;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500;
    fastcgi_cache_valid 5m;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

server {
    listen 80;

    root **************;
    index index.php index.html index.htm;

    server_name *************;

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

    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            add_header X-Cache $upstream_cache_status;
            fastcgi_cache fastcgicache;
    }
}

【讨论】:

    猜你喜欢
    • 2012-06-06
    • 2017-06-14
    • 2021-01-30
    • 2017-01-29
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多