nginx重定向

nginx的重定向

location 匹配

location 匹配的就是后面的URI

/wordpress

location 匹配的分类和优先级**

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

2、正则匹配

^~ 前缀匹配, 以什么开头

-区分大小写的匹配

~*不区分大小写

!~ :区分大小写的取反

!~* 不区分大小写的取反

3、一般匹配

location / 字符串

精确匹配的优先级最高 正则 一般

优先级总结

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

bash 复制代码
location = / {

   root html

​    index index.html  index.htm index.php

}

网站首页

第二个必选额规则:处理静态请求的页面

bash 复制代码
location ^~ /static/ {

​    root /web static/;

  index index.html indexhem;



}

用来匹配静态页面

bash 复制代码
location ~* `\.`  {

 root /web/picturs /;

  index index.heml index.heml;

}

访问 图片或者指定的后缀名

第三个规则,一般是通用规则,用转发.php .js 为后缀的动态请求到后盾服务器(数据库)

bash 复制代码
location / {

proxy_pass

}

抓发后台请求和负载均衡

rewrite重定向:

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

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

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

标志位

flag

permanent:永久重定向,返回码是301,浏览器地址栏会显示跳转后的url地址

redirect:临时重定向,返回码是302,浏览器地址栏会显示跳转后的url地址

break:永久重定向,返回码也是301,但是他匹配到规则之后不会向下匹配其他规则,url也不会发生变化

last:重定向,但是会继续向下匹配其他的location规则

rewrite的执行顺序:

1、server模块的rewrite优先级最高

2、匹配location的规则

3、执行选定的location规则。

rewrite的语法

rewrite 正则表达式 跳转后的内容 标志位;

在重定向过程中,使用last就会陷入死循环 nginx默认循环十次超过十次就会自动结束循环返回码为500

基于域名,老的不用了,能够访问 直接跳到新域名

bash 复制代码
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;

         }
        }

然后映射

bash 复制代码
20.0.0.20 www.xy102.com www.cj.com

放入内容html目录下

bash 复制代码
root@test2 html]# pwd
/usr/local/nginx/html
[root@test2 html]# echo 123 > index.html
[root@test2 html]# ls
50x.html  index.html
[root@test2 html]# cat index.html

基于客户端ip进行跳转 设置一个固定接口可以正常访问 其他ip都访问不了

bash 复制代码
   server {
        listen       80;
        server_name  www.xy102.com;

         charset utf-8;

        #access_log  logs/host.access.log  main;
         set $rewrite true;
         if ( $remote_addr = "20.0.0.10" ) {
              set $rewrite false;
}
         if ( $rewrite = true ) {
              rewrite (.+) /error.html;
}
        location = /error.html {
           root html;
}

location 匹配的优先级

=

~

~*

/

重定向

permanent

redriect

lat 500的报错不一定都是客户端可能是配置文件的问题

相关推荐
姚不倒2 分钟前
三节点 TiDB 集群部署与负载均衡搭建实战
运维·数据库·分布式·负载均衡·tidb
gpio_012 分钟前
自建gitlab服务器并用sakurafrp穿透
运维·服务器·gitlab
k7Cx7e5 分钟前
Debian执行ssh root@localhost 提示认证失败
运维·debian·ssh
闻道且行之6 分钟前
Nginx 安装、做成服务及 HTTPS 配置全流程
linux·运维·nginx·https
人工智能训练11 分钟前
Qwen3.5 开源全解析:从 0.8B 到 397B,代际升级 + 全场景选型指南
linux·运维·服务器·人工智能·开源·ai编程
蜕变的小白12 分钟前
Linux系统编程-->UDP编程:C/S模型实战详解
linux·运维·网络协议·udp
FIT2CLOUD飞致云14 分钟前
里程碑丨JumpServer开源堡垒机GitHub Star数突破30,000个!
运维·开源·堡垒机
草莓熊Lotso15 分钟前
MySQL 数据库基础入门:从概念到实战
linux·运维·服务器·数据库·c++·人工智能·mysql
芒果披萨16 分钟前
linux系统管理基本命令行
linux·运维·服务器
喵叔哟17 分钟前
69.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--财务健康度
运维·微服务·.net