nginx泛域名解析并禁止IP访问,禁止多余www.泛域名访问

vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
user www www;
worker_processes  auto;
#add by Happy; set the value == system's ulimit -sn 's value
worker_rlimit_nofile  65535;


error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;




events {
    # set by Happy; set the value == system's "ulimit -sn" is value
    # if use is epoll,the worker_connections 65535+1  but sys's ulimit -sn == 65536
    worker_connections  65535;
    multi_accept  on;
    use  epoll;
}




http {
    include       mime.types;
    default_type  application/octet-stream;
    # add by Happy 2014-04-18
    server_names_hash_bucket_size  128;
    client_header_buffer_size      32K;
    large_client_header_buffers    4 32k;
    # set user upload size
    client_max_body_size           8m;
    # add end 


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    access_log  logs/access.log main;


    #display nginx version add this,
    #but you have set fastcgi.conf set 
    #fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
    #fastcgi_param SERVER_SOFTWARE nginx;
    #this is by Happy set for 2014-04-19
    server_tokens off;


    sendfile       on;
    #tcp_nopush     on;
    tcp_nopush   on;
    tcp_nodelay    on;
    #add by Happy 2014-04-19
    fastcgi_connect_timeout    300;
    fastcgi_send_timeout       300;
    fastcgi_read_timeout       300;
    fastcgi_buffer_size        64k;
    fastcgi_buffers         4  64k;
    fastcgi_busy_buffers_size  128k;
    fastcgi_temp_file_write_size  128k;
    #keepalive_timeout  0;
    keepalive_timeout  60;
    client_header_timeout  10;
    client_body_timeout  10;
    #reset_timeout_connection  on;
    send_timeout  10;
    #open gzip by Happy
    gzip  on;
    gzip_disable      "msie6";
    gzip_proxied      any;
    gzip_min_length   1k;
    gzip_buffers      4  16k;
    #if you set squid2.x,please set 1.0
    #by Happy set 2014-04-19
    gzip_http_version 1.1; 
    gzip_comp_level   4;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_vary        on;
    ## add by Happy,set 2014-04-23
    #xian zhi dan ge IP  bing fa wei 10 ge
    limit_conn_zone  $binary_remote_addr zone=happy:10m;
    limit_conn happy 20;
    #xian zhi lian jie su du, da yu 1m de wen jian xian su 100k/s
    #if you user's one link down file's size > 1M,the set user one down is 100k/S
    limit_rate_after 1m;
    limit_rate       200k;
    #open_file_cache max=100000 inactive=60s;
    #open_file_cache_valid 65s;
    #open_file_cache_min_uses 2;
    #open_file_cache_errors off;

    #close ip link look

   #设置80端口默认虚拟机,禁止通过IP访问,注意后面的 default参数必选

    server {
        listen 80 default;
        return 404;
    }

    # add web com,only by the com list is look 

    server {

        #绑定服务器域名,以及泛域名

        listen 80;
        server_name  www.abc.cn  abc.cn  *.abc.cn;

#server_name _;        
#charset  utf-8;
        access_log  logs/host.access.log;

#close www. two or more 

        #禁止带有多余"www."的域名及泛域名访问

        #禁止通过 www.www.abc.com ,www.123.abc.com 等访问站点,禁止除www.abc.com以外的所有带有“www.”的泛域名访问

if ( $host ~ www\.(.*)\.(.*)\.(cn|com|net|cc)$ ) {
            return 404; 
        }



        location / {

            #root   html;
   root   /home/var/www;
   index  index.php index.html index.htm;
   if (!-e $request_filename) {
    rewrite ^(.*) /index.php last;
   }


}


        location ~* \.(?:gif|ico|jpe?g|png|swf|bmp|swf)$ {


            expires 30d;
            log_not_found off;
            ## No need to bleed constant updates. Send the all shebang in one
            #
            #    fell swoop.
            #
            tcp_nodelay off;
            #
            #    Set the OS file cache.
            #
            open_file_cache max=1000 inactive=120s;
            open_file_cache_valid 125s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
   root /home/var/www;
            #
        }
        location ~* \.(js|css)$ {
            expires   1d;
            root /home/var/www;
        }
#error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
   #root   html;
            root           /home/var/www;
            if (!-e $request_filename) {
                rewrite (.*) /index.php last;
   }
   fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
   include        fastcgi_params;
        }


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
location ~*\.(html|log|sh|bash)?$ {
   return 403;
}
        location ~ ^/home/var/www/MVC/ {
            deny  all;
            break;
        }
       
    }




    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;


    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_timeout  5m;


    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}

你可能感兴趣的:(linux,nginx,server,LNMP)