User-Profile-Image
hankin
  • 5
  • centos7
  • docker
  • mysql
  • PostgreSQL
  • git/gitlab
  • ELK
  • python
    • python-Tornado
    • python-django
  • redis
  • nginx
  • kvm
  • proxmox
  • mongo
  • kubernetes
  • prometheus
  • GlusterFs
  • nfs
  • freeswitch
  • httpd
  • shell脚本
  • linux
  • fastdfs
  • nextcloud
  • openssl
  • openvpn
  • rabbitmq
  • sqlite
  • svn
  • java
  • ubuntu
  • vue2
  • wordpress
  • php
  • IOT物联网
  • 项目
  • 故障处理
  • 树莓派
  • 博客存档
  • 未分类
  • 杂项
  • #1742(无标题)
  • 新视野
  • 分类
    • 项目
    • 树莓派
    • 杂项
    • 未分类
    • 新视野
    • 故障处理
    • 博客存档
    • 交换机
    • wordpress
    • vue2
    • ubuntu
    • svn
    • sqlite
    • shell脚本
    • redis
    • rabbitmq
    • python-django
    • python
    • proxmox
    • prometheus
    • PostgreSQL
    • php
    • openvpn
    • openssl
    • nginx
    • nfs
    • nextcloud
    • mysql
    • mongo
    • linux
    • kvm
    • kubernetes
    • java
    • IOT物联网
    • httpd
    • GlusterFs
    • git/gitlab
    • freeswitch
    • fastdfs
    • ELK
    • docker
    • centos7
  • 页面
    • #1742(无标题)
  • 友链
      请到[后台->主题设置->友情链接]中设置。
Help?

Please contact us on our email for need any support

Support
    首页   ›   nginx   ›   正文
nginx

nginx反向代理配置缓存

2023-04-02 14:21:53
941  0 0

nginx反向代理配置缓存,提升速度、减少原站网络请求节约带宽

第一步 在nginx.conf的 http{}内添加
proxy_cache_path /home/cache levels=1:2 keys_zone=data_cache:1024m max_size=2048m inactive=1d;
#定义缓存目录 /home/cache,nginx会自动创建
#设置缓存的数据保留时间 inactive=1d
#缓存目录结构  levels=1:2
第二步 修改对应的反向代理配置文件a.conf
server {
    listen 443 ssl;
    ssl_certificate /etc/nginx/conf.d/fullchain.pem;
    ssl_certificate_key /etc/nginx/conf.d/privatekey.pem;
    server_name  xx.com;
    client_max_body_size  10m;
    client_body_buffer_size 1024k;    

    location  / {
       add_header Cache-Control no-store;
          proxy_pass http://192.168.2.100/;
          proxy_set_header Host $http_host;
          proxy_set_header X-Forwarded-Host $http_host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      proxy_cache data_cache;
      proxy_cache_valid 200 1d;
      #为不同的响应状态码设置不同的缓存时间,比如这里200状态码为1天
      proxy_cache_use_stale error timeout invalid_header updating http_500 http_503 http_404;
      #后端服务器出现这些情况,nginx直接使用的过期缓存响应
    }
}

server {
    listen 80;
    server_name xx.com;
    return 301 https://$host$request_uri;
}
第三步 测试
浏览器访问url 
然后查看/home/cache下已有缓存
    tree /home/cache
    du -sh /home/cache
评论 (0)

点击这里取消回复。

欢迎您 游客  

Copyright © 2025 网站备案号 : 蜀ICP备2022017747号
smarty_hankin 主题. Designed by hankin
主页
页面
  • #1742(无标题)
博主
tang.show
tang.show 管理员
linux、centos、docker 、k8s、mysql等技术相关的总结文档
213 文章 2 评论 201599 浏览
测试
测试