nginx各种常用的配置

server

{

    listen 80;

    server_name www.1.com www.a.com www.b.com;


#域名跳转   

   if ($host != 'www.a.com' ) {

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

    }

    index index.html index.htm index.php;

    root /data/www;


#    location  /uc_server/ {

#          auth_basic              "Auth";

#          auth_basic_user_file   /usr/local/nginx/conf/.htpasswd;

#    }


#黑名单

#    deny 127.0.0.1;

#    allow all;

#白名单

#    allow 127.0.0.1;

#    allow 192.168.31.141;

#    deny all;



#某个目录下限制ip

    location /uc_server/ {

        allow 192.168.31.0/24;     ##只允许31网段访问这个目录,其余的全部拒绝

        deny all;

        location ~ \.php$ {

            include fastcgi_params;

            fastcgi_pass 127.0.0.1:9000;

            fastcgi_index index.php;

            fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

        }

    }


#针对目录限制php解析

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

    {

        deny all;

    }


#根据user_agent控制

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

            return 403;

    }


    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

    }


#缓存时间

#    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

#    {

#          expires      30d;

#          access_log off;

#    }


    location ~ .*\.(js|css)?$

    {

          expires      12h;

          access_log off;

    }


#防盗链

    location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ {

         expires 10d;

         valid_referers none blocked server_names *.1.com *.a.com *.b.com *.baidu.com\

         *.google.com *.google.cn *.soso.com ;

         if ($invalid_referer) {

              return 403;

              #rewrite ^/ http://www.example.com/nophoto.gif;

         }

         access_log off;   

    }

#设置日志不记录某些东西

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$  ##(设置一下图片格式的日志不被记录 )

{

  access_log off;

}


 location ~(static|cache)     



{

   access_log off;

}



# 伪静态rewrite规则

    rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;

    rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

    rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;

    rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;

    rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;

    rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;


#docment_uri

#    if ($document_uri !~ 'abc')

#    {

#           rewrite ^/(.*)$ /abc/$1 redirect;

#    }



    access_log /home/logs/discuz.log combined_realip;

}


你可能感兴趣的:(nginx配置文件)