当前位置: 首页 > news >正文

Nginx重定向

Nginx重定向

location 匹配

location匹配的就是后面的URL /WordPress

192.168.118.10/wordpress

location匹配的分类和优先级

1.精确匹配

location=/对字符串进行完全匹配,必须完全符合

2.正则匹配

^~ 前缀匹配,以什么为开头~ 区分大小写的匹配~* 不区分大小写!~: 区分大小写的取反!~*: 不区分大小写的取反

3.一般匹配

location /字符串

优先级总结

location = 完整路径 > location ^~ > (location ~ location~*) >location /部分起始位置 > location /

实际网站中的使用规则

第一个规则,网站首页一般用精确=/
location = / {root html;index index.html index.htm index.php;}第二个规则,处理静态请求的页面
location ^~ /static/ {root /web/static/;index index.html index.htm;
}
用来匹配静态页面 访问图片或者是指定的后缀名
location ~* \.(jpg|gif|png|jpeg|css)$ {root /web/pictures/;index index.html index.htm;}第三个规则,一般是通用规则,用来转发.php.js为后缀的动态请求到后端服务器(数据库)
location / {proxy_pass
}
转发后端请求和负载均衡

rewrite重定向:

rewrite就是把当前访问的页面跳转到其他页面

rewrite的工作方式:通过nginx的全局变量或者自定义变量,结合正则表达式和标志位实现url的重定向

nginx的变量

$uri 客户端请求的uri地址
$host: 请求的主机名
$http_user_agent:客户端请求的浏览器和操作系统
$http_referer:请求头的referer信息,表示当前页面来源的uri
$remote_addr:客户端的ip地址
$remote_port:客户端的端口号
$server_addr:服务端的ip地址
$server_port:服务端的端口号
$request_method:获取客户端请求的方法
$scheme:请求的协议,要么是http,要么是https
x_forwarded_for: 用来获取请求头当中,客户端的真实ip地址(代理服务器添加,在代理服务器当中,指示客户端的ip地址)
x-Real-ip:客户端真实的ip地址nginx.conf
proxy_set_header X-Real-IP $remote_addr (加上这一字段,客户端的真实ip地址就会传递给后端数据库)

vim nginx.conf

systemctl restart nginx

nginx标志位

flg

permanent:永久重定向,返回码是301,浏览器地址栏会显示跳转后的URL地址
redirect: 临时重定向,返回码是302浏览器地址栏会显示跳转后的URL地址
break:	永久重定向,返回码也是301,但是他匹配到规则之后,不会再向下匹配其他规则,URI也不会发生变化
last:	重定向,但是会继续向下匹配其他的location规则

rewrite的执行顺序:

1.server模块的rewrite优先级最高
2.匹配location的规则
3.执行选定的location规则

rewrite的语法

rewrite 正则表达式 跳转后的内容 标志位;
mkdir test1
mkdir xy102
echo 123 > test1/index.html
echo 456 > xy102/index.html
vim nginx.conf
location /{root html;rewrite /test1/(.*) /xy102/$1 permanent;#$1就是访问过来的时候,捕获组
}

会报500错

基于域名进行跳转

老的不用了,但是依然能够访问,通通跳转到新的域名

[root@test1 conf]# vim nginx.confserver {listen       80;server_name  www.xy102.com;charset utf-8;#access_log  logs/host.access.log  main;location / {root   html;if ($host = 'www.xy102.com') {rewrite ^/(.*)$ http://www.cj.com/$1 permanent;}index index.html;}
#其他代码
:wq
[root@test1 conf]# systemctl restart nginx.service 
[root@test1 conf]# vim /etc/hosts
192.168.118.10 www.xy102.com www.cj.com
#虚拟机浏览器http://www.cj.com/

基于客户端ip进行跳转

公司有新业务上线,测试阶段,其他的ip只能显示维护中,只有192.168.118.10能正常访问

[root@test1 conf]# vim nginx.confserver {listen       80;server_name  www.xy102.com;charset utf-8;#access_log  logs/host.access.log  main;set $rewrite true;#设置一个变量名,rewrite,值是true#来进行判断ip是否是合法ipif ( $remote_addr = "192.168.118.10" ){set $rewrite false;}if ( $rewrite = true ){rewrite (.+) /error.html;#重定向,192.168.118.10/error.html}location = /error.html {root html;}location / {root   html;if ($host = 'www.xy102.com') {rewrite ^/(.*)$ http://www.cj.com/$1 permanent;}index index.html;}
#其他代码
:wq
[root@test1 conf]# systemctl restart nginx.service 
[root@test1 conf]# cd ..
[root@test1 conf]# cd html/
[root@test1 html]# echo "网页维护中!" > error.html
[root@test1 html]# vim /etc/hosts
192.168.118.10 www.xy102.com www.cj.com
#虚拟机浏览器http://www.cj.com/

t nginx.service
[root@test1 conf]# cd …
[root@test1 conf]# cd html/
[root@test1 html]# echo “网页维护中!” > error.html
[root@test1 html]# vim /etc/hosts
192.168.118.10 www.xy102.com www.cj.com
#虚拟机浏览器http://www.cj.com/


http://www.lryc.cn/news/396655.html

相关文章:

  • 私有化地图离线部署方案之高程检索服务
  • PostgreSQL 中如何实现数据的增量更新和全量更新的平衡?
  • 数据结构--二叉树相关习题5(判断二叉树是否是完全二叉树 )
  • Python 轻松生成多种条形码、二维码 (Code 128、EAN-13、QR code等)
  • Python: 分块读取文本文件
  • 服务攻防——中间件Jboss
  • 宏碁F5-572G-59K3笔记本笔记本电脑拆机清灰教程(详解)
  • 基于FPGA的LDPC编译码算法设计基础知识
  • 国际网课平台Udemy上的亚马逊云科技AWS免费高分课程和创建、维护EC2动手实践
  • 空中交通新动能!2024深圳eVTOL展动力电池展区核心内容抢先看!
  • 代码江湖:Python 中的进程与线程
  • 根据H在有限域GF(2^m)上求解生成矩阵G
  • Django 实现子模版继承父模板
  • 数据安全治理:从库级权限申请到表级权限申请
  • vue3源码(六)渲染原理-runtime-core
  • python拆分Excel数据,自动发邮箱
  • 2024年福州延安中学夏季拿云杯拔尖创新人才素养测试(小高组)
  • ES6 之 Promise 构造函数知识点总结 (四)
  • KIVY 3D Rotating Monkey Head¶
  • 测试几个 ocr 对日语的识别情况
  • 华为机考前准备工作
  • 偏差、方差(训练误差,验证误差)
  • Retrofit框架源码深度剖析【Android热门框架分析第二弹】
  • C++Windows环境搭建(CLion)
  • 【区块链 + 智慧政务】省级一体化区块链平台 | FISCO BCOS应用案例
  • 局域网远程共享桌面如何实现
  • Ubuntu固定虚拟机的ip地址
  • python破解密码·筛查和选择
  • 【将应用程序注册为系统服务】
  • 从0-1搭建一个web项目(路由目录分析)详解