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.conf
 server {
        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.conf
 server {
        listen       80;
        server_name  www.xy102.com;
        charset utf-8;
        #access_log  logs/host.access.log  main;
        set $rewrite true;
        #设置一个变量名,rewrite,值是true
        #来进行判断ip是否是合法ip
        if ( $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/


相关推荐

  1. Nginx定向

    2024-07-10 08:56:03       11 阅读
  2. nginx定向

    2024-07-10 08:56:03       8 阅读
  3. Nginx定向

    2024-07-10 08:56:03       6 阅读
  4. 使用 Nginx 将 HTTP 定向到 HTTPS

    2024-07-10 08:56:03       30 阅读
  5. Nginx 中将 HTTP 定向到 HTTPS

    2024-07-10 08:56:03       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 08:56:03       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 08:56:03       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 08:56:03       4 阅读
  4. Python语言-面向对象

    2024-07-10 08:56:03       4 阅读

热门阅读

  1. Apache Flink 任意 JAR 包上传漏洞利用及防范策略

    2024-07-10 08:56:03       8 阅读
  2. QTreeWidget 树遍历

    2024-07-10 08:56:03       9 阅读
  3. 技术浅谈:如何入门一门编程语言

    2024-07-10 08:56:03       6 阅读
  4. C#如何进行深度学习对行人进行识别?

    2024-07-10 08:56:03       8 阅读
  5. 金南瓜科技的SECS/GEM解决方案

    2024-07-10 08:56:03       12 阅读
  6. Linux 系统监控工具深度解析:Glances 与 Nmon

    2024-07-10 08:56:03       7 阅读