nginx的反向代理及缓存功能。

来源:互联网 发布:mac桌面壁纸下载网站 编辑:程序博客网 时间:2024/06/11 01:11

nginx是一个强大的反向代理服务器。
nginx反向代理的指令不需要新增额外的模块,默认自带proxy_pass指令,只需要修改配置文件就可以实现反向代理。



配置反向代理主要在nginx配置文件中的location中。
先说明一下实验环境。
nginx-1    是前面的反向代理服务器。
nginx-2    是后端服务器1
nginx-3    是后端服务器2



简单的配置。
        location / {
            proxy_pass http://nginx-2/;
        }

这样就可以将访问代理至nginx-2这台后端主机上,也就是说,不论uri是什么。都会将访问代理至后端。 





或者是访问 nginx-1 的时候代理至 后端的 nginx-2/bbs。
        location / {
            proxy_pass  http://nginx-2/bbs/;
        }

注意,这里的bbs后面必须加上/,这样才能代理成功,否则是失败的。



或者是访问 nginx-1/forum 的时候代理至 后端的 nginx-2/bbs。
        location /forum {
            proxy_pass http://nginx-2/bbs;
        }

注意,这里的bbs后面就不需要 / 了。当然加上也没关系。



或者是将对图片的访问全部代理至后端。如访问 nginx-1/forum/picture.jpg 可以反向代理
至后端的nginx-2/forum/picture.jpg
        location ~* \.(jpg|png|gif|jpeg)$ {
            proxy_pass http://nginx-2;
        }

 

这里的正则表达式无论匹配到什么都会原封不动的加在nginx-2的后面。而nginx-2后面不能再加任何uri!!!


访问nginx-1/bbs 会被代理至 nginx-2/bbs/bbs1/forum。但是如果访问成
nginx-1/bbs/bbs1/forum。其实最终的结果是 访问/bbs/bbs1/forum/bbs1/forum .也就是错误的url了。

        location /bbs {
            proxy_pass  http://nginx-2/bbs/bbs1/forum;
        }


要想要获取真正客户端的IP的话, 就需要配置X-Real-IP。
在反向代理服务器nginx-1上配置:
location ~* \.(jpg|png|gif|jpeg)$ {
            proxy_pass http://nginx-2;
            proxy_set_header Host               $host;
            proxy_set_header X-Real-IP          $remote_addr;
            proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;
        }

而后在后端的真实服务器主机上配置日志的读取格式:
    log_format logstash_json '{ "@timestamp": "$time_local", '
                             '"@fields": { '
                             '"remote_addr": "$http_x_real_ip", '
                             '"body_bytes_sent": "$body_bytes_sent", '
                             '"request_time": "$request_time", '
                             '"status": "$status", '
                             '"request": "$request", '
                             '"request_method": "$request_method", '
                             '"http_referrer": "$http_referer", '
                             '"body_bytes_sent":"$body_bytes_sent", '
                             '"http_x_forwarded_for": "$remote_addr", '
                             '"http_user_agent": "$http_user_agent" } }';

其中remote_addr对应的http_x_real_ip就是真实客户的IP。具体的分析之类的,我找了一个特别好的博客去说明。


http://www.sohu.com/a/122989120_494947


nginx的反向代理缓存配置。
代理服务器可以将一些客户端经常访问的数据做缓存处理。
proxy_cache也有缺点,那就是没有实现自动清理磁盘上的缓存源数据的功能。一次时间长了,肯定会有一定的压力。


配置:
http {
...
    proxy_cache_path  /cache/nginx/  levels=1:2 max_size=200m inactive=5m  keys_zone=mycache:100m;    


server {
...
    location ~* \.(jpg|png|gif|jpeg)$ {
            proxy_cache mycache;
            proxy_cache_valid 200 1d;
            proxy_cache_valid 301 302 10m;
            proxy_cache_valid any 1m;
            proxy_cache_use_stale error timeout http_500 http_502 http_504;



这个配置的内容是这样的。首先在http中定义了缓存的存在位置。是/cache/nginx  ,注意该目录需要有nginx(运行workerpross)用户的权限。分为2级目录。第一级目录一个字符。第二级目录两个字符。 缓存的数据量最大200m。若缓存在5分钟之内没有被访问过就强制刷新。定义一个mycache的缓存空间,大小为100m。


在需要被缓存的location段里配置。使用mycache这个缓存空间。200的请求缓存1天(301,302的就不解释了)。。。其余的缓存一分钟。但是如果出现以下情况,可以使用过期缓存,如500错误等等、、、



  


原创粉丝点击