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
相关推荐
步菲18 小时前
springboot canche 无法避免Null key错误, Null key returned for cache operation
java·开发语言·spring boot
毕设源码-朱学姐18 小时前
【开题答辩全过程】以 基于SpringBoot的中医理疗就诊系统为例,包含答辩的问题和答案
java·spring boot·后端
大聪明-PLUS19 小时前
面向开发者的实用 GNU/Linux 命令(第二部分)
linux·嵌入式·arm·smarc
2201_757830871 天前
全局异常处理器
java
小徐Chao努力1 天前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
sorry#1 天前
top简单使用
linux·运维
Coder_Boy_1 天前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
广东大榕树信息科技有限公司1 天前
如何通过动环监控系统提升机房运行安全与效率?
运维·网络·物联网·国产动环监控系统·动环监控系统
Coder_Boy_1 天前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
半壶清水1 天前
开源免费的在线考试系统online-exam-system部署方法
运维·ubuntu·docker·开源