您的位置:首页 > 运维架构 > 反向代理

OpenResty做反向代理的nginx配置

2020-03-06 19:41 1521 查看

OpenResty做反向代理的nginx配置

  • OpenResty集成lua代码
  • include vhost/*.conf;
    upstream local {
    server 127.0.0.1:8080;
    }
    
    location / {
    proxy_set_header Host $host;   #  Host  用上游代理处理或反向代理处理
    proxy_set_header X-Real-IP $remote_addr;     #  取tcp协议的远端地址,
    #  X-Real-IP  把远端服务器地址发送给上游nginx
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://local;
    }
    • 在nginx.org的documentation参看命令行

    为减轻上游服务器压力,反向代理nginx进行缓存服务

    proxy_cache_path /tmp/nginxcache levels=1:2 	keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
    • 在nginx.conf的http中用proxy_cache_path设置缓存文件写在哪个目录下,文件命名方式,缓存大小10m。

    • 在location中加入以下代码

      proxy_cache my_cache;
      proxy_cache_key $host$uri$is_args$args;
      proxy_cache_valid 200 304 302 1d;

    my_cache是刚开辟的共享内存,hosthosthosturiisargsis_argsisa​rgsargs作为一个整体的key来区别用户资源的不同,proxy_cache_valid设置哪些响应不返回。

    然后重启动openresty,再停止nginx静态资源,实现缓存功能。

    OpenResty集成lua代码

    在location中应用lua代码

    location /lua {
    default_type text/html;
    content_by_lua '
    ngx.say("User-Agent: ", ngx.req.get_headers()["User-Agent"])
    ';
    }
    • 点赞
    • 收藏
    • 分享
    • 文章举报
    追梦人Kai 发布了31 篇原创文章 · 获赞 1 · 访问量 386 私信 关注
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: