Nginx重定向

一、重定向概述

重定向:也叫url重定向,也叫url改写

未来需求:

  • 网站是http(80)---------------->https(443) URL重定向
    用户http://www.baidu.com https://www.baidu.com
  • 根据客户端访问类型进行跳转 希望根据用户客户端进行判断
    如果用户的客户端是ios,iphone,android,访问m.jd.com. 否则默认访问www.jd.com
  • 新老域名跳转:
    www.360buy.com > jd.com
  • 需要我们调整url格式:伪静态(搜索引擎收入) 运营要求.

二、模块与指令

相关的指令

说明

return

实现对url的改写,一般与nginx变量一起使用

rewrite

实现对url的改写,使用正则匹配uri进行改写,还有各种标记

set

创建或修改nginx变量

if

判断,一般与nginx变量一起使用

1. return 指令

格式

说明

格式1

return code URL; 返回新的状态码+新的URL地址

格式2

return code 返回指定的状态码

放哪

server,location,if

#如果用户访问/admin/页面返回403
[root@web01 /etc/nginx/conf.d]# cat rewrite.cn.conf.bak 
server{
	listen 80;
	server_name rewrite.cn;
	root /app/code/rewrite;
	error_log  /var/log/nginx/rewrite.cn-error.log notice;
	access_log  /var/log/nginx/rewrite.cn-access.log  main;

	location / { 
		index index.html;
	
	}
	location /admin/ {
		return 403;
	}
[root@web01 /etc/nginx/conf.d]# mkdir /app/code/rewrite
[root@web01 /etc/nginx/conf.d]# echo rewrite > /app/code/rewrite/index.html
[root@web01 /etc/nginx/conf.d]# mkdir /app/code/rewrite/admin
[root@web01 /etc/nginx/conf.d]# echo admin index  > /app/code/rewrite/admin/index.html
[root@web01 /etc/nginx/conf.d]# curl -H Host:rewrite.cn http://10.0.0.7/admin/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>
[root@web01 /etc/nginx/conf.d]# curl -H Host:rewrite.cn http://10.0.0.7
rewrite
#域名间跳转
[root@web01 /etc/nginx/conf.d]# cat rewrite.cn.conf.bak 
server{
	listen 80;
	server_name rewrite.cn;
	return 301 http://www.baidu.com;
}

#-L location 跟随跳转,响应是301,302跳转的时候使用.
[root@web01 /etc/nginx/conf.d]# curl -Lv -H Host:rewrite.cn http://10.0.0.7
* About to connect() to 10.0.0.7 port 80 (#0)
*   Trying 10.0.0.7...
* Connected to 10.0.0.7 (10.0.0.7) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Accept: */*
> Host:rewrite.cn
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.22.1
< Date: Fri, 17 Feb 2023 06:40:29 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
< Location: http://www.baidu.com
< 
* Ignoring the response-body
* Connection #0 to host 10.0.0.7 left intact
* Issue another request to this URL: 'http://www.baidu.com'
* About to connect() to www.baidu.com port 80 (#1)
*   Trying 110.242.68.4...
* Connected to www.baidu.com (110.242.68.4) port 80 (#1)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Fri, 17 Feb 2023 06:40:35 GMT
< Etag: "588604c1-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:29 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18

使用浏览器插件查看

  1. 安装插件
  2. 查看跳转

nginx配置所有页面都重定向到首页 nginx 重定向url_html

2. if判断

指令

说明

放在哪里

server,loaction

#访问rewrite.cn 网站只准许GET,POST 请求方法,其他访问禁止访问
[root@web01 /etc/nginx/conf.d]# cat rewrite.cn.conf.bak 
server{
	listen 80;
	server_name rewrite.cn;
    root /app/code/rewrite ;
    if ( $request_method  !~ "GET|POST"  ) {
       return 403;
    }
  
    location / {
      index index.html;
    }        
	
}
#正常访问
[root@web01 /etc/nginx/conf.d]# curl -H Host:rewrite.cn http://10.0.0.7
rewrite
#HEAD方式访问
[root@web01 /etc/nginx/conf.d]# curl -I -H Host:rewrite.cn http://10.0.0.7
HTTP/1.1 403 Forbidden
Server: nginx/1.22.1
Date: Fri, 17 Feb 2023 07:14:47 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

注:
 $request_method 取出请求方法.

if指令可以使用的符号

常用~,~*, !~,!~* nginx取反,排除,只能用if.

补充:

if只能有一重判断,不能套

3. set

温馨提示:

nginx变量,进行赋值与使用都需要加上$符号

#设置网站是否为维护状态:如果维护状态:返回503状态码,否则正常访问
#设置标记$flag 默认是0.
#判断如果$flag的值是1则网站返回503
server{
	listen 80;
	server_name rewrite.cn;
    root /app/code/rewrite ;
    set $flag 1;
    if ( $flag = 1 ){
	  return 503;
    }
    location / {
      index index.html;
    }	
}

4. rewrite

4.1 rewrite指令

  • rewrite正则用于匹配用户请求的uri.
  • 命令的格式与sed 'sg' 类似,实现替换功能,rewrite替换url内容.(改写)

指令

说明

格式

rewrite 找什么(具体内容/正则/保护分组) 替换成什么(具体内容,后向引用) [标记];

标记可以省略,默认使用 redirect标记(302)

放在哪里

server,location,if

rewrite标记

redirect

301

permanent

302

温馨提示:

rewrite匹配的内容,匹配uri

标记的时候,用的是单词,不是数字

#域名跳转
[root@web01 /etc/nginx/conf.d]# cat rewrite.cn.conf
server{
	listen 80;
	server_name rewrite.cn;
	rewrite ^(.*)$ http://www.baidu.com$1;	
}

4.2 rewrite标记

标记

说明

补充

redirect

302 临时 用户访问的时候,收到302提示及新的位置Location(响应头),用户根据 Location新的位置进行访问(让用户重新发出http请求)

新旧地址都可以用.

permanent

301 永久 用户访问的时候,收到302提示及新的位置Location(响应头),用户根据 Location新的位置进行访问(让用户重新发出http请求)

旧的地址排名取消,旧地旧的 不用了,只用新的网站.

break

用户的请求匹配到包含break指令或rewrite规则后,及时后面还有location规则, 不会继续运行.终止运行.

last

用户请求匹配到包含last标记的rewrite规则后,停止继续执行,ngx会重新发出内 部请求,请求与location规则进行匹配.

开启nginx rewrite_log才能 看到

server {
   listen 80;
   server_name flag.cn;
   root /app/code/flag;
   error_log /var/log/nginx/flag-error.log notice;
   rewrite_log on; #需要错误日志debug  notice
   location / {
   rewrite /1.html /2.html ;
   rewrite /2.html /3.html ;
   }
   location /2.html {
     rewrite /2.html /3.html ;
   }
   location /3.html {
     rewrite /3.html /a.html ;
   }
}
echo 1.html url >/app/code/flag/1.html
echo 2.html url >/app/code/flag/2.html
echo 3.html url >/app/code/flag/3.html
echo a.html url >/app/code/flag/a.html
echo b.html url >/app/code/flag/b.html

1.访问/1.html显示a.html内容
[root@web01 /etc/nginx/conf.d]# curl -H Host:flag.cn 10.0.0.7/1.html
a.html url

2.访问/2.html显示a.html内容
[root@web01 /etc/nginx/conf.d]# curl -H Host:flag.cn 10.0.0.7/2.html
a.html url

3. 在rewrite /1.html /2.html的时候加上标记break标记.
rewrite /1.html /2.html break; 执行完成rewrite后直接结束.
server {
   listen 80;
   server_name flag.cn;
   root /app/code/flag;
   error_log /var/log/nginx/flag-error.log notice;
   rewrite_log on; #需要错误日志debug  notice
   location / {
   rewrite /1.html /2.html break;
   rewrite /2.html /3.html ;
   }
   location /2.html {
     rewrite /2.html /3.html ;
   }
   location /3.html {
     rewrite /3.html /a.html ;
   }
}
[root@web01 /etc/nginx/conf.d]# curl -H Host:flag.cn 10.0.0.7/1.html
2.html url

4. 在rewrite /1.html /2.html的时候加上标记last标记.
注意这一步修改下配置文件,创建新的页面b.html.
[root@web01 /etc/nginx/conf.d]# cat
flag.cn.conf
server {
   listen 80;
   server_name flag.cn;
   root /app/code/flag;
   error_log /var/log/nginx/flag-error.log notice;
   rewrite_log on; #需要错误日志debug  notice
   location / {
   rewrite /1.html /2.html last;
   rewrite /2.html /3.html ;
   }
   location /2.html {
     rewrite /2.html /b.html ;
   }
   location /3.html {
     rewrite /3.html /a.html ;
   }
}
[root@web01 /etc/nginx/conf.d]# curl -H Host:flag.cn 10.0.0.7/1.html
b.html url