Nginx限制目录访问

2个月之前部署的一套客户的项目,使用了lvs+nginx+tomcat做了集群,一共5台,效果不错,只是nginx配置起来稍显麻烦。
今天突然提出了一个问题,由于项目是放在公网,为了配合安全审计和自身的安全考虑,要限制站点中的后台管理页面只能允许被某几个IP访问,客户提出了解决方案貌似没看懂。。。要我们帮忙评估。。。
好吧,据我所知的有几种,无非在TCP/IP协议中的某一层做策略,比如可在4、7层做限制,比如硬件负载均衡器、防火墙等等;比如手头的这个软件伪集群(用不起radware\f5啥的),直接在nginx处理吧。
配置如下:
location ~* /admin/.*\.(action|jsp)?$ {
                proxy_pass  http://tomcat;
                root   /opt/tomcat/service;
                index  index.jsp index.html index.htm;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_next_upstream http_502 http_503 http_504 error timeout invalid_header;
                proxy_cache nginx_cache;
                proxy_cache_key $host$uri$is_args$args;
                proxy_cache_valid  200 302 10m;
                proxy_cache_valid  301 1h;
                proxy_cache_valid  any 1m;
                proxy_temp_path /opt/nginxtemp;
                allow 192.168.1.23;
                allow 192.168.2.20;
                deny all;
}    

上述配置的意思是限制http://host/admin/这个页面只允许被192.168.1.23、192.168.2.20两个IP来访问。

2012.7.16更新
配置了上述参数之后,存在着session丢失的问题,后来稍微做了调整,主要是参数配置所在的位置:

 location /
        {
                proxy_pass  http://tomcat;
                #root /opt/pthsh/service/WEB-INF/jsp;
                root   /opt/pthsh/service;
                index  index.jsp index.html index.htm;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_next_upstream http_502 http_503 http_504 error timeout invalid_header;
                proxy_cache nginx_cache;
                proxy_cache_key $host$uri$is_args$args;
                proxy_cache_valid  200 302 10m;
                proxy_cache_valid  301 1h;
                proxy_cache_valid  any 1m;
                proxy_temp_path /opt/nginxtemp;
        }


location ~* /admin/.*\.(action|jsp)?$ {
      proxy_pass http://tomcat;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      allow IP1;
      allow IP2/8;
      allow IP3;
      deny all;
}
    location ~* .*\.(action|jsp)?$
     {
      proxy_pass http://tomcat;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

2012.8.22更新
配置成:
location =/admin/login.action {
上述参数之后,存在着在IP(非法IP)切换之后还能保持会话的问题,改成如下解决:
location ~* /admin/ {
这样就保证了正则匹配过滤了。

-The End-
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值