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
相关推荐
小羊没烦恼!2 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里2 天前
java中的继承和多态的区别
java
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
未济2 天前
linux 配置环境变量
linux
傲世仙尊2 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
譕痕2 天前
JSONObject与JSONArray封装数据格式区别
java·json
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
胡写代码2 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖2 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
还是大剑师兰特2 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师