nginx之location和rewrite
location
一、什么是location
location指令作用是可以根据用户请求URI来执行不同的应用,其实就是根据用户请求的网站的地址URI匹配,匹配成功即进行相关的操作。
location语法
location [ = | ~ | ~* | ^~ ] uri {
...
}
location | 指令 |
---|---|
[ =,~ ,~ *,^~] | 匹配标识 |
uri | 匹配的网站网址 |
{…} | 匹配uri后要执行的配置段 |
~ 用于区分大小写,~ *用于不区分大小写,^~作用是在常规的字符串匹配检查之后,不做正则表达式的检查。
二、location语法匹配优先级
先配置文件
[root@web01 conf.d]# vim 01_www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
root html/www;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
}
然后在命令行检测
[root@web01 conf.d]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/
402
[root@web01 conf.d]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org
402
直接输入域名,= 优先匹配
[root@web01 conf.d]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/index.html
401
匹配了/
[root@web01 conf.d]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/document.html
403
匹配了/documents/
[root@web01 conf.d]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/1.jpg
500
匹配了~* .(gif|jpg|jpeg)$
[root@web01 conf.d]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/oldboy/
401
谁都没匹配上,找默认的401
[root@web01 conf.d]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/1.gif
404
返回404,匹配任何以/images/开头的任何查询并且停止搜索,任何正则表达匹配将不会被检查
优先级排序:
=(精确匹配) > ^~(匹配常规字符,不做正则表达式检查) > ~*(不区分大小写) > /路径/(匹配常规字符串) >默认的/(所有Location都不能匹配后的默认匹配)
rewrite
一、什么是rewrite
Nginx Rewrite的主要功能就是实现URL地址重写,需要pcre软件的支持,其中rewrite的典型企业应用就是伪静态功能的实现。
rewrite语法:
rewrite regex replacement [flag]
其实和sed里的替换s/A/B/g有点相似,regex表示要被替换的(和A)支持正则,replacement表示替换成什么内容(B)。结尾是flag标记。
flag标记的说明
last | 本条规则匹配完成后,继续向下匹配新的location URI规则 |
---|---|
break | 本条规则匹配完成即终止。不再匹配后面的任何规则 |
redirect | 返回302临时重定向,浏览器地址栏会显示跳转后的URL地址 |
permanent | 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址 |
二、Nginx Rewrite的企业应用场景
三、实现Nginx跳转
首先修改配置文件,访问etiantian.org就可以跳转至www.etiantian.org了
[root@web01 conf.d]# vim 01_www.conf
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
}
在命令行测试,可以看到返回301状态码
[root@web01 conf.d]# curl -I etiantian.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0
Date: Fri, 16 Apr 2021 13:19:15 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.etiantian.org/
Nginx访问认证
企业在实际工作中要求我们为网站设置访问账号和密码权限,这样后,只有拥有账号密码的用户才可以访问网站,如下图。
Nginx配置认证
首先配置文件
[root@web01 conf.d]# vim 01_www.conf
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
auth_basic "oldboy training"; #加入提示说明,可有可无
auth_basic_user_file /etc/nginx/htpasswd; #认证的用户以及密码会保存在/etc/nginx/htpasswd里
}
我们配置了此文件后,还要下载一个工具
yum install httpd-tools -y
然后我们设置用户以及密码到上面配置的文件里,并为文件修改属主以及权限
[root@web01 conf.d]# htpasswd -cb /etc/nginx/htpasswd oldboy 123456
c为create,创建新文件
b为不需要确认,直接改密码,非交互性形式
[root@web01 conf.d]# chmod 400 /etc/nginx/htpasswd
[root@web01 conf.d]# chown nginx /etc/nginx/htpasswd
查看文件,可以看到加密后的文件
cat /etc/nginx/htpasswd
oldboy:$apr1$i/hueSO/$BEtRIhafzKWV6srShztmp.
然后检查语法,平滑重启就可以了
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# nginx -s reload
最终效果如下:
一、出现403的错误的集中情况
1、首页文件不见了,index.xxx
2、站点目录权限太低,比如chmod 700 www,但默认的权限应该是755
3、显示的主机是禁止状态
allow 10.0.0.0/24;
deny all;
但是我们的虚拟主机区段不在10.0.0.0/24区段内
以上三种情况都会出现403错误