您的位置:首页 > 新闻 > 资讯 > Nginx重定向

Nginx重定向

2024/10/6 12:23:59 来源:https://blog.csdn.net/qq_59169480/article/details/140298672  浏览:    关键词: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/


版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com