nginx--location详细使用和账户认证

14 篇文章 0 订阅

在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri,uri是用户请求的字符串,即域名后面的web文件路径,然后使用该location模块中的正则url和字符串串,如果匹配成功就结束搜索,并使用此location处理此请求。

语法规则: location [=|~|~*|^~] /uri/ { … }

 正则表达

= 用于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停止向下匹配并立即处理请求。
~ 用于标准uri前,表示包含正则表达式并且区分⼤小写,并且匹配
!~ 用于标准uri前,表示包含正则表达式并且区分大小写,并且不匹配
~* 用于标准uri前,表示包含正则表达式并不区分大写,并且匹配
!~* 用于标准uri前,表示包含正则表达式并且不区大小写,并且不匹配
^~ 用于标准uri前,表示包含正则表达式并且匹配以什么开头
$ 用于标准uri前,表示包含正则表达式并且匹配以什么结尾
\ 用于标准uri前,表示包含正则表达式并且转义字符。可以转. * ?等
* 用于标准uri前,表示包含正则表达式并且代表任意⻓度的任意字符

匹配案例

精准匹配

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
}
  location = /1.jpg{
   root /data/nginx/static; 
}
}

 区分大小写

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location ~ /X.?\.jpg{
    root /data/nginx/static;
    index index.html;
  }
}

 不区分大小写

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location ~* /X.?\.jpg{
    root /data/nginx/static;
    index index.html;
  }
}

 区别:匹配访问路径,没有文件还是不能访问

URI开始

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
  location ^~ /images {
    root /data/nginx;
    index index.html;
  }
}

 文件名后缀

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
  }
   location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
      root /data/nginx/images1;
       index index.html;
  }
}

 优先级

匹配优先级:=, ^~, ~/~*,/
location优先级:(location =) > (location 完整路路径) > (location ^~ 路路径) >
(location ~,~* 正则顺序) > (location 部分起始路路径) > (/)

 生产使用

直接匹配⽹网站根会加速Nginx访问处理理:
location = /index.html {
......;
}
location / {
......;
}
静态资源配置:
location ^~ /static/ {
......;
}
# 或者
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
......;
}
多应⽤用配置
location ~* /app1 {
......;
}
location ~* /app2 {
......;
}

nginx三层访问控制

访问控制基于模块ngx_http_access_module实现,可以通过匹配客户端源IP地址进行限制

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
     allow 192.168.13.0/24;
     deny 192.168.33.179;
  }
}

 账户认证功能

 htpasswd

安装

 yum install httpd-tools -y #Centos

apt install apache2-utils #Ubuntu

 参数
-c 创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容.
-n 不更新passwordfile,直接显示密码
-m 使用MD5加密(默认)
-d 使用CRYPT加密(默认)
-p 使用普通文本格式的密码
-s 使用SHA加密
-b 命令行中一并输入用户名和密码而不是根据提示输入密码,可以看见明文,不需要交互
-D 删除指定的用户
使用创建用户

htpasswd -cbm /apps/nginx/conf/.htpassed user1 123456

htpasswd -bm /apps/nginx/conf/.htpassed user2 123456

查看

 nginx开启登录验证

[root@localhost ~]# cat /apps/nginx/conf/conf.d/pc.conf 
server {
  listen 80;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
     auth_basic  "login password";
     auth_basic_user_file /apps/nginx/conf/.htpassed;
  }
}

        

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值