如何一次性为所有站点启用默认 Nginx 对象缓存?

如何一次性为所有站点启用默认 Nginx 对象缓存?

nginx.conf假设我有 20 个站点,并且我希望自动执行每 20 个不同的虚拟主机粘贴 1X1 缓存指令和 1X20 缓存指令的过程?

此外,我的虚拟主机已经被 Certbot 修改并且变得有点混乱(Certbot0.21.1-1+ubuntu16.04.1+certbot+0.2添加了许多空格和注释),所以我现在更加不确定如何有效地为我的所有网站启用 Nginx 对象缓存。

您怎样才能高效地做到这一点?

答案1

使用include秒。

这是我的一个网站的典型配置:

server {
        server_name www.yes-www.org;

        root /srv/www/yes-www.org;

        access_log /var/log/nginx/yes-www.org-access.log nginx;
        access_log /var/log/nginx/cache.log cache;
        error_log /var/log/nginx/yes-www.org-error.log;

        ssl_certificate /etc/letsencrypt/live/www.yes-www.org/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.yes-www.org/privkey.pem;
        ssl_trusted_certificate /etc/letsencrypt/live/www.yes-www.org/fullchain.pem;

        include includes/listen-443;
        include includes/cloudflare;
        include includes/letsencrypt;
        include includes/ssl;
        include includes/ssl_stapling;
        include includes/hsts;
        include includes/csp_wordpress;
        include includes/favicon;
        include includes/wordpress;
        include includes/php;
        include /srv/www/yes-www.org/nginx.conf;

        location ~ /\.(ht|git) {
                deny all;
        }
}

/etc/nginx/includes/letsencrypt例如的内容为:

location /.well-known/acme-challenge/ {
    root /var/www;
    try_files $uri =404;
}

包括/etc/nginx/includes/listen-443

listen 443 ssl http2;
listen [::]:443 ssl http2;

通过这种方式,您可以提取server块中重复的任何内容,并将其包含在您需要的任何位置。

相关内容