Nginx 禁止恶意IP指向访问 & 二级域名的配置

简单来说,我们的nginx服务器配置如下即可,除非要有特定的处理

server {  
    listen        80;  
    server_name   linuxidc.net  www.linuxidc.net;  
    root          /data/www;  
  
    location / {  
        index     index.html  index.php;  
    }  
  
    location ~* \.(gif|jpg|png)$ {  
        expires   30d;  
    }  
  
    location ~ \.php$ {  
        fastcgi_pass   localhost:9000;  
        fastcgi_param  SCRIPT_FILENAME  
                       $document_root$fastcgi_script_name;  
        include        fastcgi_params;  
    }  
}


要防止IP和恶意IP指向访问 ,需要设置默认Server

如果不主动设置默认server,那么第一个server就会被当做默认server

    server  {  
       listen 80 default;  #表示默认匹配的端口(一般不会先匹配,等其他Server配置项无法匹配时自动转移到该处)
       server_name _;      #表示访问网站时,排除其他server项的所有域名和ip地址
       return 500;  
   }

当然改成下面的更好

server {
listen 80 dufault;
server_name _;
rewrite ^(.*) http://www.yourdomain.com permanent;
}

从0.8.21版本开始,使用default_server关键词

default_server

通过这样的方式,我们可以如下配置

server {
    listen 80 dufault;
    server_name _;
    rewrite ^(.*) http://www.domain.com permanent;
}

server
   {
     listen       80;
     server_name  ~^(.+)?\.domain\.com$;
     index index.html;
     if ($host = domain.com){
         rewrite ^ http://www.domain.com permanent;
     }
     root  /data/wwwsite/domain.com/$1/;
   }

站点目录

站点的目录结构应该如下:

/data/wwwsite/domain.com/www/

========================================================

另外一个问题,我们可能需要用ip访问某个目录,但其他目录一切跳转到主目录

server {
listen 80 default_server;
server_name _;
        location /testdir{
            stub_status on;
            access_log  off;
        }
        location /{
            rewrite ^ http://www.nginxs.com$request_uri?;
        }
}


参考博客:

Nginx全局变量 http://www.jb51.net/article/24598.htm

Apache防止恶意指向 http://www.linuxidc.com/Linux/2011-06/37437.htm


------------------------------------------------------------

Nginx配置二级子域名

准备:需要泛域名 domain.com。

主机:www.domain.com 或 domain.com。

思路:将*.domain.com 解析到主机www.domain.com/*/ (*不能为www或空)。

配置:

if ( $host ~* (\b(?!www\b)\w+)\.\w+\.\w+ ) {
	set $subdomain $1;
}
location / {
	root   D:\www\domain.com\$subdomain;
	index  index.html index.htm;
}

具体来说,配置如下也是一种可行的方式

server
   {
     listen       80;
     server_name  ~^(.+)?\.domain\.com$;
     index index.html;
     if ($host = domain.com){
         rewrite ^ http://www.domain.com permanent;
     }
     root  /data/wwwsite/domain.com/$1/;
   }

测试

环境:Window7 + Nginx1.1.15

ToDo:

1、安装Nginx至:D:\nginx-1.1.15;安装后路径

2、修改nginx配置文件,见上;

3、新建web应用目录:D:\www\;

4、新建domain.com项目目录:D:\www\domain.com\;使用tree命令打印项目目录部署如下:

D:\WWW
└─domain.com
    │  index.html (Welcome to www.domain.com!)
    ├─a
    │      index.html (www.domain.com/a/index.html)
    │      test.html (www.domain.com/test.html)
    ├─b
    │      index.html (www.domain.com/b/index.html)

5、修改hosts文件:C:\Windows\System32\drivers\etc\hosts,新增如下行:

127.0.0.1   www.domain.com
127.0.0.1   domain.com
127.0.0.1   a.domain.com

6、启动nginx。

用例

访问:http://www.domain.com/结果:Welcome to www.domain.com!
访问:http://domain.com/ 结果:Welcome to www.domain.com!
访问:http://a.domain.com/ 结果:www.domain.com/a/index.html
结果:http://a.domain.com/test.html结果:www.domain.com/a/test.html

结果:与用例相同!

补充:

请求参数作为二级域名如何处理?
Re:采用urlrewrite或类似url重写的工具实现,步骤如下:
www.domain.com/shop.jsp?shop_key=suning
重写成:
www.domain.com/suning/
注:前提是shop_key唯一。


转载于:https://my.oschina.net/ososchina/blog/406459

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值