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的报错不一定都是客户端可能是配置文件的问题

相关推荐
缘友一世7 分钟前
Armbian 1panel面板工具箱中FTP服务无法正常启动的解决方法
linux·运维·后端·1panel
程序猿零零漆10 分钟前
【面向就业的Linux基础】从入门到熟练,探索Linux的秘密(十二)-管道、环境变量、常用命令
linux·运维·服务器
A-刘晨阳12 分钟前
ELFK 8.12.2 部署 -- docker部署方式⚽
linux·运维·elk·docker·容器
little redcap12 分钟前
下载linux的吐槽
linux·运维·服务器
j_linlian33 分钟前
Mac OS ssh 连接提示 Permission denied (publickey)
运维·ssh
大卫的纯爱战士35 分钟前
多线程网络实战之仿qq群聊的服务器和客户端
运维·服务器
网络研究院38 分钟前
Rejetto HFS 服务器存在严重漏洞受到攻击
运维·服务器·安全·黑客·漏洞·警告·hfs
jcLee951 小时前
运维系列.Nginx中使用HTTP压缩功能
运维·nginx·http
coisini.cn1 小时前
基于CentOS Stream 9平台搭建RabbitMQ3.13.4以及开机自启
linux·运维·服务器·rabbitmq·centos stream 9
林叔聊渠道分销1 小时前
从0到1构建渠道运营体系:实战案例与策略指南
大数据·运维·人工智能·产品运营·流量运营·渠道运营