#server定义一个虚拟主机

server

 

{

listen 80;

#网站的域名,可以有多个

server_namewww.123.com www.abc.com;

#301域名重定向

   if ($host != 'www.123.com')

    {

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

    }

index index.htmlindex.htm index.php;

#网站根目录

root /data/www;

#访问日志存放路径,aming为日志的格式,自己定义,在nginx配置文件

access_log/tmp/access.log aming;

#访问控制,对于外部控制,越精确越优先匹配

#deny 127.0.0.1;

#插入一个文件

   #include deny.sh;

         #用户认证管理后台

location ~.*admin\.php$ {

         #访问控制

       #allow 127.0.0.1;

       #deny all;

                   #用户认证是通过apachehtpasswd工具

       #auth_basic "tingshi";

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

                   #nginxphp-fpm通信的方式,解析php脚本

       include fastcgi_params;

       fastcgi_pass unix:/tmp/php-fcgi.sock;

       fastcgi_index index.php;

                   #解析php的根目录

       fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

}

 

   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|rar|zip|bz2)$

       {

                                     #gif|jpg|png等静态文件过期时间

                expires      30d;

                                     #不记录gif|jpg|png等格式访问日志

                access_log off;

 

                                     #图片,音乐等防盗链

                valid_referers none blocked*.123.com *.abc.com;

                if ($invalid_referer)

                {

                    return 403;

                }

       }

 

   location ~ .*\.(js|css)

       {

                                     #js|css静态文件过期时间

                expires      12h;

                                     #不记录js|css等格式访问日志

                access_log off;

       }

 

location ~\.php$ {

         #nginxphp-fpm通信的方式,解析php脚本

       include fastcgi_params;

       fastcgi_pass unix:/tmp/php-fcgi.sock;

       #fastcgi_pass 127.0.0.1:9000;

       fastcgi_index index.php;

                   #解析php的根目录

       fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

}

#禁止指定的user_agent(搜索引擎)

   #if ($http_user_agent  ~*'curl|baidu|youdao')

   #    {

   #        return 403;

   #    }

}

 

 

#简单虚拟主机

server

 

{

   listen 80;

   server_name www.woo.com;

   index index.html index.htm index.php;

   root /data/wordpress;

 

   location ~ \.php$ {

       include fastcgi_params;

       fastcgi_pass unix:/tmp/php-fcgi.sock;

       fastcgi_index index.php;

       fastcgi_param SCRIPT_FILENAME /data/wordpress$fastcgi_script_name;

    }

}

 

 

 

#nginx扩展

Nginx禁止某个目录解析php

location ~ .*abc/.*\.php?$

       {

        deny all;

       }

 

 

Nginx禁止访问.txt文件

location ~* \.(txt|doc)$ {

               if (-f $request_filename) {

                  root/home/domain/public_html/test;

                  break;

                  }

               }-