Nginx-学习目录
1、if-指令
1.1、if-语法
Syntax: if (condition) { ... }
Default: —
Context: server, location
~ 模糊匹配
~* 不区分大小写的匹配
!~ 不匹配
= 精确匹配
---------------------------------
示例:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
}
if ($request_method = POST) {
return 405;
}
if ($slow) {
limit_rate 10k;
}
if ($invalid_referer) {
return 403;
}
1.2、if-实战
1.2.1、需求
过滤Nginx请求中包含a1=3526的http请求到192.168.10.5的8080端口处理。
1.2.2、配置nginx
# 生成返回默认的页面
mkdir /opt/code
echo "if没有匹配到" >/opt/code/index.html
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name rewrite.cyc.com;
root /opt/code;
default_type application/json;
location / {
index index.html;
if ($request_uri ~* 'a1=\d{4}'){
proxy_pass http://192.168.10.5:8080;
}
include proxy_params;
}
}
EOF
# 注意if与()是要空格隔开的
1.2.3、配置属性解析
\d表示数字
{4,8}表示数字出现的次数是4到8次
if ($request_uri ~* 'good=\d{4,8}/'){ # 如gid=12345678就符合该条件。
动作;
}
1.2.4、重新加载nginx并且测试
~]# curl rewrite.cyc.com?a1=2345
web-01
~]# curl rewrite.cyc.com?a1=
if没有匹配到
~]# curl rewrite.cyc.com?a1=1
if没有匹配到
~]# curl rewrite.cyc.com
if没有匹配到
2、set-指令
2.1、set-语法
Syntax: set $variable value;
Default: —
Context: server, location, if
# 设置变量
2.2、set-实战
2.2.1、需求
将用户请求 url.cyc.com.zh 跳转至 url.cyc.com/zh
将用户请求 url.cyc.com.jp 跳转至 url.cyc.com/jp
2.2.2、准备测试代码
mkdir /opt/code/{zh,jp} -p
echo "zh" >/opt/code/zh/index.html
echo "jp" >/opt/code/jp/index.html
2.2.3、配置nginx
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server {
listen 80;
server_name url.cyc.com;
location / {
root /opt/code;
index index.html;
}
include proxy_params;
}
server{
listen 80;
server_name url.cyc.com.zh url.cyc.com.jp;
location / {
index index.html;
if ($http_host ~* 'zh'){
set $language zh;
}
if ($http_host ~* 'jp'){
set $language jp;
}
rewrite ^/$ http://url.cyc.com/$language/ permanent;
}
}
EOF
2.2.4、测试主机配置hosts
192.168.10.4 url.cyc.com.zh
192.168.10.4 url.cyc.com.jp
192.168.10.4 url.cyc.com
2.2.5、重新加载nginx并且测试
systemctl reload nginx
~]# curl -L url.cyc.com.zh
zh
~]# curl -L url.cyc.com.jp
jp
3、return-指令
3.1、return-语法
Syntax: return code [text];
return code URL;
return URL;
Default: —
Context: server, location, if
3.2、return-实战
3.2.1、需求1:如果用户使用IE浏览器访问url.cyc.com,则返回段字符串。
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name url.cyc.com;
root /opt/code;
default_type text/html;
charset utf-8;
location / {
if ($http_user_agent ~* 'MSIE|firefox'){
return 200 '请更换浏览器';
}
index index.html;
}
include proxy_params;
}
EOF
3.2.2、需求2:如果用户使用E浏览器访问url.cyc.com,则返回500错误。
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name url.cyc.com;
root /opt/code;
default_type text/html;
charset utf-8;
location / {
if ($http_user_agent ~* 'MSIE|firefox|WOW64'){
return 500;
}
index index.html;
}
include proxy_params;
}
EOF
3.2.3、需求3:如果用户使用E浏览器访问url.cyc.com,则直接跳转至百度首页。
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name url.cyc.com;
root /opt/code;
default_type text/html;
charset utf-8;
location / {
if ($http_user_agent ~* 'MSIE|firefox|WOW64'){
return 302 https://www.baidu.com;
}
index index.html;
}
include proxy_params;
}
EOF
4、rewrite-指令
4.1、语法
关键字 正则 替代内容 flag标记
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
4.1.1、flag选项参数
last # 本条规则匹配完成后,继续向下匹配新的Location URI规则
break # 本条规则匹配完成即终止,不再匹配后面的任何规则
redirect # 返回302 临时重定向,地址栏会显示跳转后的地址
permanent # 返回301永久重定向,地址栏会显示跳转后的地址
4.2、break与last区别
4.2.1、准备测试代码
echo "1.html" > /opt/code/1.html
echo "2.html" > /opt/code/2.html
echo "3.html" > /opt/code/3.html
echo "a.html" > /opt/code/a.html
echo "b.html" > /opt/code/b.html
4.2.2、nginx配置【rewrite默认示例 】
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name url.cyc.com;
root /opt/code;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
include proxy_params;
}
EOF
# 条件不存在,则匹配/,因为没有条件中断,一直匹配到最后。
~]# curl url.cyc.com/1.html
b.html
~]# curl url.cyc.com/2.html
a.html
~]# curl url.cyc.com/3.html
b.html
4.2.3、nginx配置【rewrite-break示例 】
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name url.cyc.com;
root /opt/code;
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
include proxy_params;
}
EOF
# 加上break,匹配到直接返回。
~]# curl url.cyc.com/1.html
2.html
# 因为∶在Location{}内部,遇到break,本Location}内以及后面的所有Location}内的所有指令都不再执行。
4.2.4、nginx配置【rewrite-last示例 】
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name url.cyc.com;
root /opt/code;
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
include proxy_params;
}
EOF
# 因为︰在Location{}内部,遇到Last,本Location{内后续指令不再执行,
# 而重写后的url会对所在的server{ ...}标签重新发起请求,从头到尾匹配一遍规则,那个匹配则执行哪个。
~]# curl url.cyc.com/1.html
a.html
4.2.4、break与last区别说明?
当rewrite规则遇到break后,本location{}与其他ocation{}的所有rewrite/return规则都不再执行。
当rewrite规则遇到last后,本location{}里后续rewrite/return规则不执行,但重写后的url再次从头开始执行所有规则,哪个匹配执行哪个。
4.3、redirect与permanent区别
4.3.1、nginx配置【redirect】
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name url.cyc.com;
root /opt/code;
location / {
rewrite /1.html /2.html redirect;
rewrite /2.html /3.html;
}
include proxy_params;
}
EOF
4.3.2、nginx配置【permanent】
cat >/etc/nginx/conf.d/rewrite.cyc.com.conf<<'EOF'
server{
listen 80;
server_name url.cyc.com;
root /opt/code;
location / {
rewrite /1.html /2.html permanent;
rewrite /2.html /3.html;
}
include proxy_params;
}
EOF
4.3.3、测试访问
通过浏览器访问测试,会发现无论是permanent、还是redirect会进行跳转。
# permanent
]# curl url.cyc.com/1.html -L
3.html
# redirect
]# curl url.cyc.com/1.html -L
3.html
4.3.4、redirect和permanent的区别
Flag 跳转 状态码 排名情况
redirect 临时跳转 302 对旧网站无影响,新网站会有排名
permanent 永久跳转 301 新跳转网站有排名,旧网站排名会被清空