Nginx访问控制

#配置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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值