ubuntu

ubuntu nginx缓存配置详解

小樊
39
2025-10-02 07:58:41
栏目: 智能运维

Ubuntu Nginx缓存配置详解

一、Nginx缓存概述

Nginx缓存是提升网站性能的关键技术,通过存储后端响应到本地磁盘,减少重复请求对后端的压力,缩短响应时间。Ubuntu环境下,Nginx缓存主要分为HTTP代理缓存(适用于静态资源、反向代理)和FastCGI缓存(适用于PHP等动态内容)。

二、基础准备

1. 安装Nginx

若未安装Nginx,可通过以下命令安装:

sudo apt update
sudo apt install nginx

2. 创建缓存目录

缓存文件需存储在专用目录(如/var/cache/nginx),并设置正确的权限:

sudo mkdir -p /var/cache/nginx
sudo chown -R www-data:www-data /var/cache/nginx  # Nginx默认用户为www-data

三、HTTP代理缓存配置(适用于静态资源、反向代理)

HTTP代理缓存通过proxy_cache系列指令实现,适用于反向代理后端服务(如Apache、Node.js)或静态资源。

1. 定义缓存路径与参数

http块中添加proxy_cache_path指令,配置缓存存储细节:

http {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
    # 参数说明:
    # - /var/cache/nginx:缓存文件存储路径
    # - levels=1:2:缓存目录层级(1级目录+2级子目录,避免单目录文件过多)
    # - keys_zone=my_cache:10m:缓存区域名称(my_cache)及共享内存大小(10MB,用于存储缓存键)
    # - max_size=1g:缓存最大总大小(1GB),超过后自动清理旧文件
    # - inactive=60m:缓存文件60分钟未被访问则自动删除
    # - use_temp_path=off:禁用临时路径,直接写入缓存目录
}

2. 启用缓存并设置规则

serverlocation块中添加以下指令,启用缓存并定义缓存行为:

server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass http://backend_server;  # 替换为后端服务器地址(如127.0.0.1:8080)
        proxy_cache my_cache;              # 使用名为my_cache的缓存区域
        proxy_cache_valid 200 302 10m;     # 200/302状态码缓存10分钟
        proxy_cache_valid 404 1m;          # 404状态码缓存1分钟
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;  # 后端错误时使用陈旧缓存
        add_header X-Proxy-Cache $upstream_cache_status;  # 添加响应头,显示缓存状态(HIT/MISS/BYPASS)
    }
}

3. 关键指令说明

四、FastCGI缓存配置(适用于PHP动态内容)

FastCGI缓存针对PHP等动态内容,通过fastcgi_cache系列指令实现,可显著减少PHP脚本的执行次数。

1. 定义FastCGI缓存路径与参数

http块中添加fastcgi_cache_path指令:

http {
    fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=fcgi_cache:10m max_size=1g inactive=60m use_temp_path=off;
    # 参数说明与proxy_cache_path类似,但专为FastCGI设计
}

2. 启用FastCGI缓存

location块中(匹配PHP文件,如\.php$)添加以下指令:

server {
    listen 80;
    server_name example.com;
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;  # 包含PHP-FPM配置(Ubuntu默认路径)
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;  # 替换为PHP-FPM socket路径(根据版本调整)
        fastcgi_cache fcgi_cache;            # 使用名为fcgi_cache的缓存区域
        fastcgi_cache_valid 200 302 10m;     # 200/302状态码缓存10分钟
        fastcgi_cache_valid 404 1m;          # 404状态码缓存1分钟
        fastcgi_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        fastcgi_cache_bypass $http_cache_control;  # 根据客户端Cache-Control头跳过缓存
        fastcgi_no_cache $http_cache_control;      # 根据客户端Cache-Control头禁用缓存
        add_header X-FastCGI-Cache $upstream_cache_status;  # 添加响应头,显示FastCGI缓存状态
    }
}

3. 关键指令说明

五、静态文件缓存优化(可选但推荐)

对于图片、CSS、JS等静态文件,可直接通过expires指令设置浏览器缓存,减少Nginx的请求处理:

server {
    listen 80;
    server_name example.com;
    
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
        expires 30d;  # 浏览器缓存30天
        add_header Cache-Control "public, no-transform";  # 允许公共缓存,禁止转换内容(如图片压缩)
    }
}

六、测试与验证

1. 测试配置语法

修改配置文件后,使用以下命令测试语法是否正确:

sudo nginx -t

若输出nginx: configuration file /etc/nginx/nginx.conf test is successful,则表示配置正确。

2. 重启Nginx

测试通过后,重启Nginx应用配置:

sudo systemctl restart nginx

3. 验证缓存生效

通过浏览器或curl命令访问网站,查看响应头中的缓存状态:

curl -I http://example.com/

七、常见问题排查

1. 缓存不生效

2. 缓存占用过多磁盘空间

通过以上配置,Ubuntu环境下的Nginx可高效利用缓存提升网站性能。根据实际需求(如动态内容更新频率、静态资源大小),可进一步调整缓存时间、层级结构等参数,实现最优性能。

0
看了该问题的人还看了