1.nginx地址重写rewrite
Nginx Rewrite 相关指令有if,rewrite,set,return
1.1.if语句:应用环境:server,location
~ 正则匹配 (区分大小写)
~* 正则匹配 (不区分大小写)
!~ 正则不匹配 (区分大小写)
!~* 正则不匹配 (不区分大小写)
-f 和!-f 用来判断是否存在文件
-d 和!-d 用来判断是否存在目录
-e 和!-e 用来判断是否存在文件或目录
-x 和!-x 用来判断文件是否可执行在匹配过程中可以引用一些Nginx的全局变量
$args 请求中的参数;
$document_root 针对当前请求的根路径设置值;
$host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
$limit_rate 对连接速率的限制;
$request_method 请求的方法,比如"GET"、"POST"等;
$remote_addr 客户端地址;
$remote_port 客户端端口号;
$remote_user 客户端用户名,认证用;
$request_filename 当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images/a.jpg)
$request_uri 当前请求的文件路径名(不带网站的主目录/images/a.jpg)
$query_string 与$args相同;
$scheme 用的协议,比如http或者是https
$server_protocol 请求的协议版本,"HTTP/1.0"或"HTTP/1.1";
$server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
$server_name 请求到达的服务器名;
$document_uri 与$uri一样,URI地址;
$server_port 请求到达的服务器端口号;
1.2.rewrite :指令根据表达式来重定向URI,或者修改字符串,可以应用于server,location,if环境下每行rewrite指令最后跟一个flag标记,支持flag的标记有:
last 相当于Apache里的[L]标记,表示完成rewrite。默认为last。
break 本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 返回301永久重定向,浏览器地址会显示跳转后URL地址
rewrite匹配参考示例:
# http://www.testpm.com/2019/a/1.html ==> http://www.testpm.com/2018/a/1.html
location /2019/a {
root /var/www/html;
index 1.html index.hml;
rewrite ^/2019/(.*)$ /2018/$1 permanent;
}
location /2018/a {
root /var/www/html;
index 1.html index.htl;
}
# http://www.cc.com/a/1.html ==> http://jd.com
location /a {
root /html;
if ($host ~* www.cc.com ) {
rewrite .* http://jd.com permanent;
}
}
# http://www.tianyun.com/login/tianyun.html ==> http://www.tianyun.com/reg/login.html?user=tianyun
location /login {
root /usr/share/nginx/html;
rewrite ^/login/(.*)\.html$ http://$host/reg/login.html?user=$1;
}
location /reg {
root /usr/share/nginx/html;
index login.html;
}
1.3.set指令 :用于定义一个变量
应用环境:server,location,if
先进行本地域名解析host文件
10.0.105.202 www.testpm.com
10.0.105.202 alice.testpm.com
10.0.105.202 jack.testpm.com
server {
listen 80;
server_name www.testpm.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
if ( $host ~* ^www.testpm.com$) {
break;
}
if ( $host ~* "^(.*)\.testpm\.com$" ) {
set $user $1;
rewrite .* http://www.testpm.com/$user permanent;
}
}
location /jack {
root /usr/share/nginx/html;
index index.html index.hml;
}
location /alice {
root /usr/share/nginx/html;
index index.html index.hml;
}
}
1.4,return指令
应用场景:server,location,if
如果访问的.sh的文件则返回403操作拒绝错误
server {
listen 80;
server_name www.testpm.cn;
#access_log /var/log/nginx/http_access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~* \.sh$ {
return 403;
}
}
2.last,break详解
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/last.access.log main;location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /break/ {
root /usr/share/nginx/html;
rewrite .* /test/break.html break;
}
location /last/ {
root /usr/share/nginx/html;
rewrite .* /test/last.html last;
}
location /test/ {
root /usr/share/nginx/html;
rewrite .* /test/test.html break;
}}
此代码在在 /last与/break文件后方加入last时,如若他的下面还有/test代码,并且/test的状态为break时则跳转到/test下,无论是哪一条代码,在其后又last时下跳至第一个/test文件,如果下方没有/etst时则运行输出501报错,
last标记在本条rewrite规则执行完后,会对其所在的server标签重新发起请求
break标记在执行匹配完此条规则后,停止匹配,不再匹配执行
使用alias指令时必须使用last
3.nginx的localtion指令
nginx的http配置主要包括三个区块:
http { # 这个是协议级别
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
server { # 这个是服务器级别
listen 80;
server_name localhost;
location / { # 这个是请求级别
root html;
index index.html index.htm;
}
}
}
location前缀的含义
= | 表示精确匹配,优先级为最高 |
^~ | 表示uri以某个常规字符串开头,理解为匹配url路径即可 |
~ | 表示区分大小写的正则匹配 |
~* | 表示不区分大小写的正则匹配 |
!~ | 表示区分大小写不匹配的正则 |
!~* | 表示不区分大小写不匹配的正则 |
/ | 通用匹配,任何请求都会匹配到 |
@ | 内部服务跳转 |
= 大于 ^~ 大于 ~|~*|!~|!~* 大于 /
查找顺序和优先级
1:带有“=”的精确匹配优先
2:没有修饰符的精确匹配
3:正则表达式按照他们在配置文件中定义的顺序
4:带有"^~"修饰符的,开头匹配
5:带有"~"或"~*"修饰符的,如果正则表达式与URI匹配
6:没有修饰符的,如果指定字符串与URI开头匹配
4.root,alias指令区别
location /img/ {
alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。]
-
alias 是一个目录别名的定义,
-
root 则是最上层目录的定义。
-
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无
5.nginx错误页面配置
nginx错误页面包括404 403 500 502 504等页面,只需要在server中增加以下配置即可:
error_page 404 403 500 502 503 504 /404.html; 加上此行配置,为错误了跳转至404页面
location = /404.html {
root /usr/local/nginx/html;
}
6.nginx流量控制
6.1配置基本的限流
“流量限制”配置两个主要的指令,limit_req_zone
和limit_req
,如下所示:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
upstream myweb {
server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=1;
}
server {
listen 80;
server_name localhost;location /login {
limit_re zone=mylimit;
proxy_pass http://myweb;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
6.2 处理突发(在limit_req中使用burst参数)
在上一行代码中加入burst
limit_req zone=mylimit burst=20;
burst参数定义了超出zone指定速率的情况下,客户端还能发起多少请求,排队
6.3 无延迟的排队(可以在burst参数后添加nodelay参数)
limit_req zone=mylimit burst=20 nodelay;
6.4 高级配置示例
白名单
geo $limit {
default 1;
10.0.0.0/24 0;
192.168.0.0/24 0;
}
map $limit $limit_key { (此处意思为放通为0)
0 "";
1 $binary_remote_addr; (此处意思为为1的话去匹配下面的规则以限制)
}
limit_req_zone $limit_key zone=req_zone:10m rate=5r/s;server {
listen 80;
server_name localhost;
location / {
limit_req zone=req_zone burst=10 nodelay;
root /usr/share/nginx/html;
index index.html index.hml;
}
}
这个例子同时使用了
geo
和map
指令。geo
块将给在白名单中的IP地址对应的$limit
变量分配一个值0,给其它不在白名单中的分配一个值1。然后我们使用一个映射将这些值转为key,如下:
如果
$limit
变量的值是0,$limit_key
变量将被赋值为空字符串如果
$limit
变量的值是1,$limit_key
变量将被赋值为客户端二进制形式的IP地址两个指令配合使用,白名单内IP地址的
$limit_key
变量被赋值为空字符串,不在白名单内的被赋值为客户端的IP地址。当limit_req_zone
后的第一个参数是空字符串时,不会应用“流量限制”,所以白名单内的IP地址(10.0.0.0/24和192.168.0.0/24 网段内)不会被限制。其它所有IP地址都会被限制到每秒5个请求。
limit_req
指令将限制应用到/的location块,允许在配置的限制上最多超过10个数据包的突发,并且不会延迟转发。
7.nginx 访问控制
指定location拒绝所有请求,在location块中配置deny all指令
server {
listen 80;
server_name localhost;
location /foo.html {
root /home/www/html;
deny all;
}
}
指定location全部访问为配置 allow all指定,也可以使用ip网段配置方式入 allow 192.168.1.0/24,表示满足此网段的ip都可以访问
7.2 基于用户的信任登录
配置示例:
server {
listen 80;
server_name localhost;
location / {
root /home/www/html;
index index.html index.hml;
auth_basic "Auth access test!";
auth_basic_user_file /etc/nginx/auth.conf;
}
}
建立口令文件
[root@192 ~]# yum install -y httpd-tools #htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件
[root@192 ~]# htpasswd -cm /etc/nginx/auth_conf user10
[root@192 ~]# htpasswd -m /etc/nginx/auth_conf user20
[root@192 ~]# cat /etc/nginx/auth.conf
user10:$apr1$MOa9UVqF$RlYRMk7eprViEpNtDV0n40
user20:$apr1$biHJhW03$xboNUJgHME6yDd17gkQNb0
再访问页面测试