环境:centos-6.8,mysql-5.6,nginx-1.8,php-5.6,discuzX3.2
$ vim /usr/local/nginx/conf/vhosts/test.com.conf
server
{
#监听端口
listen 80;
#服务器目录
root /data/www;
#访问日志文件位置和格式
access_log /tmp/test.com_access.log combined_realip;
#网站首页文件
index index.html index.php;
#域名
server_name test.com www.test.com test2.com www.test2.com;
#域名301重定向
if ($host != 'www.test.com') {
rewrite ^/(.*) http://www.test.com/$1 permanent;
}
#禁止指定的user_agent访问网站(主要针对垃圾UA爬虫)
if ($http_user_agent ~* 'soso|yahoo|curl') {
return 403;
}
#访问控制
#禁止一个或几个IP访问
deny 192.168.3.3;
deny 192.168.3.6;
#禁止IP段访问
deny 192.0.0.0/8;
deny 192.168.0.0/16;
deny 192.168.3.0/24;
#对某个文件进行访问控制
location ^~ /auth/ {
#允许192.168.3.9访问/auth/目录下的文件,其余拒绝。
allow 192.168.3.9;
deny all;
}
#用户认证
#对站内以admin.php结尾的页面进行用户认证
location ~ admin\.php$ {
#基本用户认证
auth_basic "authentication";
#用户和密码文件,使用htpasswd生成
auth_basic_user_file /usr/local/nginx/conf/vhosts/.htpasswd;
#解析php文件
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params;
}
#对目录/data/www/auth/下的文件访问需要用户认证
location ^~ /auth/ {
auth_basic "用户认证";
auth_basic_user_file /usr/local/nginx/conf/vhosts/.htpasswd;
}
#解析php文件
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(gif|jpg|jpeg|png|bmp|swf|zip|flv|mp3|mp4)$ {
#对匹配文件不记录访问日志
access_log off;
#文件的客户端缓存过期时间
expires 15d;
#防盗链
valid_referers none blocked *.test.com *.test2.com;
if ($invalid_referer) {
return 403;
# rewrite ^/ http://www.xxx.com/.../xxx.gif;
}
}
#设置js或css的URI的请求
location ~* (js|css) {
access_log off;
expires 2h;
}
}