南京做网站南京乐识赞,北京网站优化效果怎样,平台类网站建设价格表,嘉兴设计公司有哪些nginx的重定向#xff08;rewrite#xff09;
location 匹配
location匹配的就是后面的uri
/wordpress 192.168.233.10/wordpress
location匹配的分类和优先级
1.精确匹配
location / 对字符串进行完全匹配#xff0c;必须完全符合
2.正则匹配
^-前缀级别#xff…nginx的重定向rewrite
location 匹配
location匹配的就是后面的uri
/wordpress 192.168.233.10/wordpress
location匹配的分类和优先级
1.精确匹配
location / 对字符串进行完全匹配必须完全符合
2.正则匹配
^-前缀级别以什么为开头
~区分大小写的匹配
~*不区分大小写
~区分大小写的取反
~*不区分大小写的取反
3.一般匹配通用匹配
location /字符串
精确匹配的优先级最高其次是正则最后是一般。
优先级总结
location 完整路径 location ^~ location ~ location~* location /部分起始位置 location /
实际网站中的所有规则
第一种网站首页
location / {root html;index index.heml index.htm index.php;
}第二章处理静态请求的页面
location ^~ /static/ {root /web/static/;index index.heml index.htm;
}访问图片或者是指定的后缀名
location ~* \\.(jpg|gif|png|css) {root /web/pictures/;index index.heml index.htm;
}第三章一般是通用规则原来转发.php .js为后缀的动态请求到后台服务器数据库。
转发后端请求和负载均衡
location / {
proxy_pass
}
rewrite重定向
rewrite就是把当前访问的页面跳转到其他页面。
rewrite的工作方式通过nginx的全局变量或者自定义变量结合正则表达式和标志位实现url的重定向
nginx的变量
$uri 客户端请求的uri的地址
$host 请求的主机名
$http_user_angent 客户端请求的浏览器和操作系统
$http_referer 请求头的referer信息表示当前页面来源的url
$remote_addr 客户端的IP地址
$remote_port 客户端的端口号
$server_addr 服务端的IP地址
$server_port 服务端的端口号
$request_method 获取客户端请求的方法
$scheme 显示请求的协议要么是http要么是https
x_forwarded_for 用来获取请求头当中客户端的真实IP地址。代理服务器添加在代理服务器当中指示客户端的IP地址
X-Real-IP:客户端真实的IP地址
vim nginx.conf
proxy_set_header X-Real-IP $remote_addr
加上这一字段客户端的真实IP地址就会传递给后端服务器
标志位
flag
permanent:永久重定向返回码是301浏览器地址栏会显示跳转后的URL地址
redirect:临时重定向返回码是302浏览器地址栏会显示跳转后的URL地址
break永久重定向返回码也是301但是它匹配到规则之后不会在向下匹配其他规则URL也不会发生变化
last重定向但是会继续向下匹配其他的location规则
rewrite的执行顺序
1.server模块的rewrite优先级最高
2.匹配location的规则
3.执行选定的location规则
rewrite的语法
rewrite 正则表达式 跳转后的内容 标志位
在重定向的过程中使用了last方式进行重定向但是没有结束语陷入了死循环nginx会自动循环10次last最多只能循环10次超过10次nginx就会自动结束返回码500。
基于域名进行跳转老的不用但是依然能够访问统统跳转到新的域名 vim /usr/local/nginx/conf/nginx.conf
server {listen 80;server_name www.xy102.com;charset utf-8;location / {root html;if ( $host www.xy102.com){rewrite ^/(.*)$ http://www.cj.com/$1 permanent;}index index.html;}
}
nginx -t
systemctl restart nginx
#进入/etc/hosts做映射
vim /etc/hosts
192.168.233.10 www.xy102.com www.cj.com
#在index.html中添加内容
echo /usr/local/nginx/html/index.html基于客户端的ip进行跳转公司新业务上线测试阶段
vim nginx.conf
set $rewrite true;
#设置应该变量名rewrite值是true
#来判断ip是否为合法ip
if ( $remote_addr 192.168.233.10) {set $rewrite false;
}
if ( $rewrite true ) {rewrite (.) /error.html;#重定向192.168.233.22/error.html
}
location /error.html {root html;
}location匹配的优先级
8.233.10) { set $rewrite false; } if ( $rewrite true ) { rewrite (.) /error.html; #重定向192.168.233.22/error.html } location /error.html { root html; }