nginx配置访问控制、rewrite应用、nginx代理

简介:

一、访问控制

限制只让某个ip访问:

    allow 192.168.1.100;

    deny all;

限制只有本地地址可以访问,白名单;

    allow 127.0.0.1;

    deny all;

拒绝本地访问,黑名单:

    deny 127.0.0.1;

    allow all;


deny all 直接拒绝所有,下面的allow就不生效了。

1
2
3
4
5
6
7
8
9
10
[root@localhost vhosts] # vi default.conf
server
{
     listen 80 default_server;
     server_name localhost;
     index index.html index.htm index.php;
     root  /usr/local/nginx/html ;
     deny all;
     allow 2.2.2.2;
}

1
2
3
4
5
6
7
[root@localhost vhosts] # curl -x127.0.0.1:80  192.168.20.30/index.html -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .6.2
Date: Fri, 15 May 2015 08:46:05 GMT
Content-Type: text /html
Content-Length: 168
Connection: keep-alive


禁止某个IP或者IP段访问站点的设置方法:

1、首先建立下面的配置文件放在nginx的conf目录下面,命名为deny.ip  

1
2
3
4
# cat /usr/local/nginx/conf/deny.ip
deny 192.168.20.10;
deny 192.168.20.11;
deny 192.168.10.0 /24 ;

2、在nginx的配置文件nginx.conf中加入:include deny.ip;

3、重启一下nginx的服务:/usr/local/nginx/sbin/nginx -s reload 就可以生效了。

deny.ip 的格式中也可以用deny all; 如果你想实现这样的应用,除了几个IP外,其他全部拒绝,

allow 1.1.1.1;

allow 1.1.1.2;

deny all;


针对目录限制php解析:

location ~ .*(diy|template|attachments|forumdata|attachment|image/.*\.php$ {

        deny all;

}


根据 user_agent 控制客户端访问

        location / {

        if ($http_user_agent ~ 'bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315'){

                return 403;

                }

        }


实验:限定IE7.0 和 curl 不能访问;IE8.0可以正常打开;

    location / {

        if ($http_user_agent ~ 'MSIE 7.0|curl'){

                return 403;

                }

        }

curl -A 代表浏览器标识agent;模拟浏览器标识;测试包含bingbot/2.0,MSIE 7.0,curl的返回值为403;

1
2
3
4
5
6
7
[root@localhost vhosts] # curl -x127.0.0.1:80 -A "aabingbot/2.0ss" www.111.com -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .6.2
Date: Fri, 15 May 2015 21:09:35 GMT
Content-Type: text /html
Content-Length: 168
Connection: keep-alive
1
2
3
4
5
6
7
[root@localhost vhosts] # curl -x127.0.0.1:80 -A "MSIE 7.0aa"  www.111.com -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .6.2
Date: Fri, 15 May 2015 21:13:50 GMT
Content-Type: text /html
Content-Length: 570
Connection: keep-alive
1
2
3
4
5
6
7
[root@localhost vhosts] # curl -x127.0.0.1:80  192.168.20.30/index.html -I
HTTP /1 .1 403 Forbidden
Server: nginx /1 .6.2
Date: Fri, 15 May 2015 09:15:27 GMT
Content-Type: text /html
Content-Length: 168
Connection: keep-alive


二、nginx的rewrite应用

现在有这样的的需求,访问 www.abc.com  请求到 www.abc.com/abc/

在nginx虚拟主机配置文件中加入 :

   if ($document_uri !~ 'abc') 

    {

       rewrite ^/(.*)$ http://www.abc.com/abc/$1 permanent;

    }

而不是单独加一句rewrite ^/(.*)$ http://www.abc.com/abc/$1 permanent;

如果只加rewrite 规则,而不限定条件,那么会造成死循环。  

会访问到http://www.abc.com/abc/abc/abc/abc/....


实验测试,只加一行rewrite规则,redirect 302 临时重定向;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost vhosts] # cat 111.conf 
server
{
     listen 80;
     server_name www.aaa.com aaa.com;
     index index.html index.htm index.php;
     root  /data/www2 ;
  
     rewrite ^/(.*)$  /aaa/ $1 redirect;
    
     location ~ \.php$ {
         include fastcgi_params;
         fastcgi_pass unix: /tmp/php-fcgi-www2 .sock;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME  /data/www2 $fastcgi_script_name;
     }
}


使用curl测试跳转到http://www.aaa.com/aaa/asdfasdfa

1
2
3
4
5
6
7
8
9
[root@localhost vhosts] # curl -x127.0.0.1:80  www.aaa.com/asdfasdfa -I
HTTP /1 .1 302 Moved Temporarily
Server: nginx /1 .6.2
Date: Fri, 15 May 2015 09:54:10 GMT
Content-Type: text /html
Content-Length: 160
Location: 
http: //www .aaa.com /aaa/asdfasdfa
Connection: keep-alive

 浏览器输入http://www.aaa.com/asdfasdfa 出现死循环网址;

wKioL1VbCd-yBH_MAABlkBkfKdo220.jpg

spacer.gif

加入if判断,域名不匹配aaa的时候跳转到aaa地址下;浏览器访问跳转正确,出现404错误是我们做实验没有这个目录;
    if ($document_uri !~ 'aaa')

     {

     rewrite ^/(.*)$ /aaa/$1 redirect;

     }
spacer.gifwKiom1VbCLHyjTsoAACXJYBk58c903.jpg

三、nginx代理配置

/conf/vhosts/目录下,新建一个proxy.conf 写入下面的内容:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost vhosts] # cat proxy.conf
server {
             listen 80;
             server_name  
             location / {
                 proxy_pass      http: //180 .97.33.108/;
                 proxy_set_header Host   $host;
                 proxy_set_header X-Real-IP      $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             }
         }

代理baidu.com,proxy_pass填写baidu的ip地址;

#curl -x192.168.20.30:80  www.baidu.com  使用本地解析baidu.com 就可以访问百度的主页; 


dig命令查看baidu的别名和对应的ip地址;

#dig www.baidu.com

;; ANSWER SECTION:
www.baidu.com. 1065 IN CNAME www.a.shifen.com.
www.a.shifen.com. 183 IN A 180.97.33.108
www.a.shifen.com. 183 IN A 180.97.33.107


如果代理的机器有多台,可以实现负载均衡。bbb为自定义的内容;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
upstream bbb
{
             server  180.97.33.108:80;
             server  180.97.33.107:80;
}
server {
         listen 80;
         server_name  
         location / {
                 proxy_pass      http: //bbb/ ;
                 proxy_set_header Host   $host;
                 proxy_set_header X-Real-IP      $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
}







本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1652905,如需转载请自行联系原作者

目录
相关文章
|
9月前
|
存储 缓存 应用服务中间件
Nginx 响应头 Vary 的介绍与应用
`Vary` 头部字段在Web开发中扮演着重要角色,通过合理使用 `Vary`,可以优化缓存策略,提升Web应用的性能和响应速度。本文介绍了 `Vary` 头部字段的基本概念、作用、常见使用场景及其在Nginx中的配置方法。通过这些内容,希望读者能够更好地理解和应用 `Vary` 头部字段,提高Web应用的缓存效率和用户体验。
382 10
|
网络协议 网络虚拟化 数据安全/隐私保护
访问控制列表(ACL)配置
访问控制列表(ACL)配置
294 1
访问控制列表(ACL)配置
|
网络协议 安全 网络性能优化
了解访问控制列表 (ACL):概念、类型与应用
了解访问控制列表 (ACL):概念、类型与应用
1099 2
|
网络虚拟化 数据安全/隐私保护 数据中心
对比了思科和华为网络设备的基本配置、接口配置、VLAN配置、路由配置、访问控制列表配置及其他重要命令
本文对比了思科和华为网络设备的基本配置、接口配置、VLAN配置、路由配置、访问控制列表配置及其他重要命令,帮助网络工程师更好地理解和使用这两个品牌的产品。通过详细对比,展示了两者的相似之处和差异,强调了持续学习的重要性。
593 2
|
Apache 数据安全/隐私保护
HAProxy的高级配置选项-ACL篇之基于源地址访问控制案例
这篇文章介绍了HAProxy的ACL(访问控制列表)功能,特别是如何基于源地址进行访问控制的高级配置选项,并通过实战案例展示了如何配置ACL规则以允许或阻止特定IP地址或IP范围的访问。
192 7
HAProxy的高级配置选项-ACL篇之基于源地址访问控制案例
|
Apache 数据安全/隐私保护
HAProxy的高级配置选项-ACL篇之基于策略的访问控制
这篇文章介绍了HAProxy的高级配置选项,特别是如何使用ACL(访问控制列表)进行基于策略的访问控制,通过实战案例展示了如何配置HAProxy以允许或拒绝来自特定源地址的访问。
258 6
HAProxy的高级配置选项-ACL篇之基于策略的访问控制
|
安全 Java 数据安全/隐私保护
如何配置 Java 安全管理器来避免访问控制异常
配置Java安全管理器以防止访问控制异常,需在启动JVM时通过 `-Djava.security.manager` 参数启用,并设置安全策略文件,定义权限规则,限制代码执行操作,确保应用安全。
854 1
|
Kubernetes 负载均衡 应用服务中间件
k8s学习--ingress详细解释与应用(nginx ingress controller))
k8s学习--ingress详细解释与应用(nginx ingress controller))
2090 0
ly~
|
消息中间件 搜索推荐 大数据
一般情况下在 RocketMQ 中添加 access key 的步骤: 一、确定配置文件位置 RocketMQ 的配置文件通常位于安装目录下的 conf 文件夹中。你需要找到 broker.conf 或相关的配置文件。 二、编辑配置文件 打开配置文件,查找与 ACL(访问控制列表)相关的配置部分。 在配置文件中添加以下内容:
大数据广泛应用于商业、金融、医疗和政府等多个领域。在商业上,它支持精准营销、客户细分及流失预测,并优化供应链管理;金融领域则利用大数据进行风险评估、市场预测及欺诈检测;医疗行业通过大数据预测疾病、提供个性化治疗;政府运用大数据进行城市规划和公共安全管理;工业领域则借助大数据进行设备维护、故障预测及质量控制。
ly~
871 2
|
NoSQL 关系型数据库 MySQL
HAProxy的高级配置选项-haproxy的四层负载及访问控制案例
这篇文章介绍了HAProxy的高级配置选项,特别是如何进行四层负载均衡和基于策略的访问控制。通过实战案例,展示了如何配置HAProxy以实现对特定IP地址的访问控制,以及如何通过四层负载均衡将流量分配到后端的MySQL和Redis服务。
440 6