OpenResty做反向代理的nginx配置
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是刚开辟的共享内存, h o s t host hosturi i s a r g s is_args isargsargs作为一个整体的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"])
';
}