目录
资源列表
基础环境
关闭防火墙
关闭内核安全机制
修改主机名
一、安装httpd
二、安装nginx
准备nginx源
设置nginx
启动
部分页面不缓存(可选)
测试
在client节点请求nginx
关闭httpd请求nginx
本文具体记录了nginx实现缓存的设置步骤,nginx是一个非常优秀的web服务,同时还具有正向代理,反向代理,负载均衡以及缓存等功能。
资源列表
操作系统设置主机名IPCentOS7.3.16112C4Gnginx192.168.207.131CentOS7.3.16112C4Ghttpd192.168.207.165CentOS7.3.16112C4Gclient192.168.207.166 基础环境
关闭防火墙
- systemctl stop firewalld
- systemctl disable firewalld
复制代码
关闭内核安全机制
- setenforce 0
- sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
复制代码
修改主机名
- hostnamectl set-hostname nginx
- hostnamectl set-hostname httpd
- hostnamectl set-hostname client
复制代码
一、安装httpd
- yum -y install httpd
- echo httpd > /var/www/html/index.html
- systemctl start httpd
复制代码 二、安装nginx
准备nginx源
- 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
- # 在/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;
- }
复制代码
- 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或错误,会跳过此台服务器访问下一台服务器
复制代码 启动
- nginx -t
-
- systemctl start nginx
复制代码 部分页面不缓存(可选)
- 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
- curl 192.168.207.131
- httpd
复制代码
关闭httpd请求nginx
- curl 192.168.207.131
- httpd
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |