Nginx实现缓存

目录

资源列表

基础环境

关闭防火墙

关闭内核安全机制

修改主机名

一、安装httpd

二、安装nginx

准备nginx源

配置nginx

启动

部分页面不缓存(可选)

测试

在client节点请求nginx

关闭httpd请求nginx


本文详细记录了nginx实现缓存的配置步骤,nginx是一个非常优秀的web服务,同时还具有正向代理,反向代理,负载均衡以及缓存等功能。

资源列表

操作系统 配置 主机名 IP
CentOS7.3.1611 2C4G nginx 192.168.207.131
CentOS7.3.1611 2C4G httpd 192.168.207.165
CentOS7.3.1611 2C4G client 192.168.207.166

基础环境

关闭防火墙

bash 复制代码
systemctl stop firewalld
systemctl disable firewalld

关闭内核安全机制

bash 复制代码
setenforce 0
sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config

修改主机名

bash 复制代码
hostnamectl set-hostname nginx
hostnamectl set-hostname httpd
hostnamectl set-hostname client

一、安装httpd

bash 复制代码
yum -y install httpd
echo httpd > /var/www/html/index.html
systemctl start httpd

二、安装nginx

准备nginx源

bash 复制代码
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
EOF
​
yum -y install nginx

配置nginx

bash 复制代码
# 在/etc/nginx/nginx.conf的http段中添加
upstream node {
    server 192.168.207.166:80;
}
proxy_cache_path /cache levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m use_temp_path=off;
​
# 在/etc/nginx/conf.d/default.conf的server段下的 location / 中添加
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://node;
        proxy_cache cache;
        proxy_cache_valid   200 304 12h;
        proxy_cache_valid   any 10m;
        add_header  Nginx-Cache "$upstream_cache_status";
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    }
  • 配置详解
bash 复制代码
proxy_cache_path /cache levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m use_temp_path=off;
    #proxy_cache    //存放缓存临时文件
    #levels         //按照两层目录分级
    #keys_zone      //开辟空间名,10m:开辟空间大小,1m可存放8000key
    #max_size       //控制最大大小,超过后Nginx会启用淘汰规则
    #inactive       //60分钟没有被访问缓存会被清理
    #use_temp_path  //临时文件,会影响性能,建议关闭
    
proxy_cache cache;
proxy_cache_valid   200 304 12h;
proxy_cache_valid   any 10m;
add_header  Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    #proxy_cache            //开启缓存
    #proxy_cache_valid      //状态码200|304的过期为12h,其余状态码10分钟过期
    #proxy_cache_key        //缓存key
    #add_header             //增加头信息,观察客户端respoce是否命中
    #proxy_next_upstream    //出现502-504或错误,会跳过此台服务器访问下一台服务器

启动

bash 复制代码
nginx -t
​
systemctl start nginx

部分页面不缓存(可选)

bash 复制代码
if ($request_uri ~ ^/(static|login|register|password)) {
    set $cookie_nocache 1;
}
location / {
    proxy_pass http://node;
    proxy_cache     cache;
    proxy_cache_valid       200 304 12h;
    proxy_cache_valid       any     10m;
    add_header      Nginx-Cache     "$upstream_cache_status";
    proxy_next_upstream     error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
    proxy_no_cache $http_pargma $http_authorization;
}

测试

在client节点请求nginx

bash 复制代码
curl 192.168.207.131
httpd

关闭httpd请求nginx

bash 复制代码
curl 192.168.207.131
httpd
相关推荐
赖small强1 分钟前
[Linux 内核]翻译kernel-4.4.94/Documentation/sysctl/vm.txt
linux·min_free_kbytes·nr_trim_pages·overcommit
舰长11511 分钟前
ubuntu24安装mysql遇到的坑----解决Mysql报错缺少libaio.so.1
linux·mysql·ubuntu
初级程序员Kyle15 分钟前
开始改变第四天 Java并发(2)
java·后端
gtr202018 分钟前
Ubuntu24.04 赋予 Qt 应用程序 修改系统时间 权限
linux
SimonKing24 分钟前
【开发者必备】Spring Boot 2.7.x:WebMvcConfigurer配置手册来了(六)!
java·后端·程序员
dessler27 分钟前
Elasticsearch(ES)Cerebro部署和使用
linux·运维·elasticsearch
神工坊39 分钟前
仿真科普|CAE技术赋能无人机,低空经济蓄势起飞
中间件·云计算·无人机·云平台·hpc·cae·高性能仿真
an869500143 分钟前
ubuntu 安装 JDK8
linux·运维·ubuntu
小马哥编程1 小时前
【软件架构】数据库系统与缓存设计:五种缓存一致性方案
数据库·缓存