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

简单来讲,咱们的nginx服务器配置以下便可,除非要有特定的处理php

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指向访问 ,须要设置默认Serverhtml

若是不主动设置默认server,那么第一个server就会被当作默认serverjava

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

固然改为下面的更好
linux

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

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

default_server

经过这样的方式,咱们能够以下配置web

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/;
   }

站点目录服务器

站点的目录结构应该以下:dom

/data/wwwsite/domain.com/www/

========================================================jsp

另一个问题,咱们可能须要用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:

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

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

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

四、新建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)

五、修改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

六、启动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惟一。

相关文章
相关标签/搜索