#配置Nginx,禁止访问指定目录下的指定程序
location ~ ^/images/.*.(php|php5|sh|pl|py)$
{
deny all;
}
#Nginx下配置禁止访问*.txt和*.doc文件
location ~* \.(txt|doc)$
{
if (-f $request_filename) { #如果$request_filename是个文件
root /data/www; #路径
#rewrite #可以重定向到某个URL
break;
}
}
location ~* \.(txt|doc)$
{
root /data/www;
deny all;
}
#禁止访问单个目录
location ~ ^/static
{
deny all;
}
#禁止访问多个目录
location ~ ^/(static|js)
{
deny all;
}
#访问控制及账户
步骤一修改配置文件
server {
listen 80;
server_name localhost;
allow all;
deny 10.125.192.2; #拒绝某个IP或IP段
auth_basic “pass”; #开启账户验证冒号中的内容随意写
auth_basic_user_file “/application/nginx-1.12.2/conf/pass.txt”; #指定账户及密码的保存文件路径
location / {
root html;
index index.html index.htm;
}
步骤二 创建密码文件
yum –y install httpd-tools
htpasswd -c /application/nginx-1.12.2/conf/pass.txt test #文件路径必须要与配置文件中相同。test为用户(随意)
New password:
Re-type new password:
htpasswd /application/nginx-1.12.2/conf/pass.txt user2 # 加第二个用户是不要加-c (-c为创建文件) -m以MD5算法加密默认方法可以不加
htpasswd -b /application/nginx-1.12.2/conf/pass.txt test 123456 # -b免交互模式输入密码 test为用户 123456为密码
#需要禁止大量ip访问网站
vim /application/nginx/conf/extra/blocksip.conf
格式:deny 118.31.157.0/24; #禁止网段
allow 121.230.43.112; #允许ip
deny all; #禁止所有
#nginx限制http请求方法
$request_method 请求方法
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 501;
}