在 CentOS 上配置 Nginx 缓存,可以通过以下步骤实现:
sudo yum install epel-release
sudo yum install nginx
sudo yum install nginx-mod-http-cache
/etc/nginx/conf.d/cache.conf
,并添加以下内容:http {
proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_server;
proxy_cache my_cache;
proxy_cache_valid 200 302 30m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_bypass $http_cache_control;
proxy_cache_key $scheme$host$request_uri$is_args$args;
}
}
}
在这个配置文件中,我们定义了一个缓存路径 /tmp/nginx
,并设置了缓存的级别、键区域、最大大小、不活跃时间等参数。我们还为不同类型的响应设置了不同的缓存有效期。
/etc/nginx/nginx.conf
中,引入刚刚创建的缓存配置文件:http {
...
include /etc/nginx/conf.d/cache.conf;
...
}
sudo systemctl restart nginx
现在,Nginx 应该已经配置好了缓存功能,可以显著提高访问速度。请注意,这个示例仅适用于单个服务器,如果你有一个负载均衡器或者多个服务器,你可能需要根据需要进行更复杂的配置。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:centos nginx负载均衡实践