nginx地址跳转

<a name="br1"></a>**nginx rewrite**

**一、nginx rewrite概述**

||<p>a.</p><p>**rewrite跳转场景**</p>|
| :- | :- |
||<p>i.</p><p>URL看起来更规范。</p>|
||<p>ii.</p><p>企业会将动态URL地址伪装成静态地址提供服务。</p>|
||<p>iii.</p><p>地址换新域名后,让旧的访问跳转到新的域名上。</p>|
||<p>iv.</p><p>服务端某些业务调整。</p>|
||<p>b.</p><p>**Rewrite跳转实现**</p>|
||<p>c.</p><p>**Rewrite实际场景**</p>|
||<p>i.</p><p>**Nginx跳转需求的实现方式**</p>|
||<p>1\.</p><p>使用rewrite进行匹配跳转。</p>|
||<p>2\.</p><p>使用if匹配全局变量后跳转</p>|
||<p>3\.</p><p>使用location匹配再跳转。</p>|
||<p>ii.</p><p>**rewrite放在server{}if{},location{}段中**</p>|
||<p>1\.</p><p>location只对域名后边的除去传递参数外的字符串起作用。</p>|
||<p>iii.</p><p>**对域名或参数字符串**</p>|
||<p>1\.</p><p>使用if全局变量匹配。</p>|
||<p>2\.</p><p>使用proxy——pass反向代理。</p>|
**二、常用命令字符**

||<p>a.</p><p>nginx正则表达式</p>|
| :- | :- |
||<p>i.</p><p>常用的正则表达式元字符</p>|




<a name="br2"></a>**字符 说明**

^ 匹配输入字符串的起始位置

$ 匹配输入字符串的结束位置

\* 匹配前面的字符零次或多次

\+ 匹配前面的字符一次或多次

? 匹配前面的字符零次或一次

. 匹配除“\n”之外的任何单个字符

\ 将后面接着的字符表记为一个特殊字符或一个原
 意字符或一个向后引用

\d 匹配纯数字

{n} 重复n次

{n,} 匹配n次或更多次

{c} 匹配单个字符c

[a-z] 匹配a-z小写字母的任意一个

[a-zA-Z] 匹配a-z小写字母或A-Z大写字母的任意一个

||<p>b.</p><p>**rewrite命令**</p>|
| :- | :- |
||<p>i.</p><p>rewrite命令语法</p>|
1 rewrite <regex> <replacement> [flag];

2 正则 跳转后的内容 rewrite支持的flag标记

||<p>c.</p><p>**flag标记说明**</p>|
| :- | :- |
标记 说明

last 相当于apache的[L]标记,表示完成rewrite

break 本条规则匹配完成即终止,不再匹配后面的任何
 规则

redirect 返回302临时重定向,浏览器地址会显示跳转后
 的URL地址,爬虫不会更新URL

permanent 返回301永久重定向,浏览器地址栏会显示跳转
 后的URL地址,爬虫更新URL

||<p>d.</p><p>**last和break比较**</p>|
| :- | :- |




<a name="br3"></a>last break

使用场景 一般写在server和if中 一般使用在lication中

URL匹配 不终止重写后的URL匹配 终止重写后的URL匹配

||<p>e.</p><p>**比较rewrite和location**</p>|
| :- | :- |
||<p>i.</p><p>**相同点**</p>|
||<p>1\.</p><p>都能实现跳转。</p>|
||<p>ii.</p><p>**不同点**</p>|
||<p>1\.</p><p>rewrite是在同一域名内更改获取资源的路径。</p>|
||<p>2\.</p><p>location是对一类路径做控制访问或反向代理,还可以proxy——pass到其他机器。</p>|
||<p>iii.</p><p>**rewrite会写在location里,执行顺序**</p>|
||<p>1\.</p><p>执行server块里的rewrite指令。</p>|
||<p>2\.</p><p>执行location匹配。</p>|
||<p>3\.</p><p>执行选定的location中的rewrite指令</p>|
||<p>f.</p><p>**location优先级的示例**</p>|




<a name="br4"></a>g.

**location优先级规则**

i.

**匹配某个具体文件**

1\.

(location = 完整路径)>(location^~完整路径)>(lication~\*完整路径)>

(location ~ 完整路径)>(location 完整路径)>(location /)

ii.

**用目录做匹配访问某个文件**

1\.

(location = 目录)>(location^~目录)>(location ~ 目录)>(location~\*目

录)>(location 目录)>(location /**三、nginx部署跳转(每个跳转服务完成后,再做新的跳转就必须删除之前跳转配**

**置的文件配置)**

a.

**基于域名跳转(在location里写)**

i.

公司旧域名www.kgc.cn,因业务需求有变更,需要使用新域名www.newkgc.cn代替




<a name="br5"></a>1.

不能废除旧域名。

2\.

从旧域名跳转到新域名,且保持其参数不变。

1 vim /usr/local/nginx/conf/nginx.conf 1

2 if ($host = 'www.kgc.com') 2

3 { 3

4 rewrite ^/(.\*)$ http://www.newkgc.com/$1 permanent; 4

5 } 5

6 wq 6

7

nginx -t

8 /etc/init.d/nginx restart 8

9 然后拿火狐浏览器访问www.kgc.cn看是否跳转到www.newkgc.cn 9

10

11 域名后面加参数跳转

12 cd /usr/local/nginx/html 12

13 mkdir -p test/1 13

14 cd test/1 14

15

vim 1.html

16 <h1>1111</h1> 16

17 wq 17

18 然后在www.kgc.cn/test/1/1.html检测是否跳转到www.newkgc.cn 18

b.

**基于客户端IP访问跳转(server段utf-8下)**

i.

今天公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司IP访问

正常。

1 vim /usr/local/nginx/conf/nginx.conf 1

2 21 set $rewrite true; 2

3 22 if ($remote\_addr = "192.168.50.19"){ 3

4

23 set $rewrite false;

5 24 } 5

6 25 if ($rewrite = true){ 6

7 26 rewrite (.+) /maintenance.html; 7

8 27 } 8

9 28 location = /maintenance.html { 9

10 29 root /usr/local/nginx/html; 10

11 30 } 11

12 wq 12




<a name="br6"></a>nginx -t 13

/etc/init.d/nginx restart 14

15

16 cd /usr/local/nginx/html 16

17

echo "<h1>website is maintianing,please visit later.</h1>" > maintenance.html

18 然后拿火狐浏览器访问是否进入维修界面

cd /usr/local/nginx/html/test/ 19

20

vim 2.html

<h1>22222</h1> 21

22 wq 22

23

24

25

26

1 user nginx nginx; 27

28 2 worker\_processes 1; 28

29 3
29

30 4 events { 30

31 5 worker\_connections 10240; 31

6 } 32

33

7

8 http { 34

9 include mime.types; 35

36 10 default\_type application/octet-stream; 36

37 11 log\_format main '$remote\_addr - $remote\_user [$time\_local] "$request" ' 37

38

12 '$status $body\_bytes\_sent "$http\_referer" '

39

13 '"$http\_user\_agent" "$http\_x\_forwarded\_for"';

14 access\_log logs/access.log main; 40

41

15 sendfile on;

42

16 keepalive\_timeout 65;

43 17 server { 43

44 18 listen 80; 44

45 19 server\_name www.kgc.cn; 45

20 charset utf-8; 46

47

31

48 32 location / { 48

49 33 root html; 49

50 34 index index.html index.htm; 50

51 35 } 51

52 36 error\_page 500 502 503 504 /50x.html; 52




<a name="br7"></a>53 37 location = /50x.html { 53

54 38 root html; 54

55 39 } 55

56 40 } 56

57 41 } 57

58 上面紫色字体是配置文件内行号,删除多余的,只留下上述行号

c.

**基于旧、新域名跳转并加目录(写在location上)**

i.

将域名http://bbs.kgc.com下面的发帖都跳转到http://www.kgc.cn/bbs,且域名跳转后

保持不变。

1 基于旧、新域名跳转并加目录 )

(写在location上 1

2 旧域名是bbs.kgc.cn 2

3 新域名是www.kgc.cn/bbs 3

4

5 vim /usr/local/nginx/conf/nginx.conf 5

6 21 location /post { 6

7 22 rewrite (.+) http://www.kgc.cn/bbs$1 permanent; 7

8

23 }

9

24 location / {

10 wq 10

11 nginx -t 11

12 /etc/init.d/nginx restart 12

13

cd /usr/local/nginx/html 14

mkdir bbs/ 15

16

cd bbs/

17

mkdir post

18 vim 3.html 18

19 <h1>33333</h1> 19

mv 3.html post/ 20

21

cd /usr/local/nginx/html

22

mkdri post/

23

cd post/

24 cp ../bbs/post/3.html ./ 24

d.

**基于参数匹配的跳转(在location里写)**

1 vim /usr/local/nginx/conf/nginx.conf 1




<a name="br8"></a>24 if ($request\_uri ~ ^/100-(100|200)-(\d+).html){ 2

25 rewrite (.\*) http://www.kgc.cn permanent; 3

26 } 4

5

if ($request\_uri ~ ^/100-(100|200)-(\d+).html){ 6

rewrite (.\*) http://www.kgc.cn permanent; 7

} 8

wq 9

nginx -t 10

/etc/init.d/nginx restart 11

12 然后拿火狐浏览器检测

e.

**基于目录下所有html文件跳转(在location上写)**

vim /usr/local/nginx/conf/nginx.conf 1

21 location ~\* /upload/.\*\.html$ { 2

22 rewrite (.+) http://www.kgc.cn permanent; 3

23 } 4

cd /usr/local/nginx/html 5

创建upload目录 6

mkdir upload 7

cd upload/ 8

vim 4.html 9

<h1>44444</h1> 10

11 然后拿火狐浏览器检测

f.

**基于最普通的url请求的跳转**

vim /usr/local/nginx/conf/nginx.conf 1

21 location ~\* ^/1/test.html$ { 2

22 rewrite (.+) http://www.kgc.cn permanent; 3

23 } 4

cd /usr/local/nginx/html 5

mkdir 1 6

cd 1 7

vim test.html 8

<h1>55555</h1> 9

10 然后访问
ion ~\* /upload/.\*\.html$ { 2

22 rewrite (.+) http://www.kgc.cn permanent; 3

23 } 4

cd /usr/local/nginx/html 5

创建upload目录 6

mkdir upload 7

cd upload/ 8

vim 4.html 9

<h1>44444</h1> 10

11 然后拿火狐浏览器检测

f.

**基于最普通的url请求的跳转**

vim /usr/local/nginx/conf/nginx.conf 1

21 location ~\* ^/1/test.html$ { 2

22 rewrite (.+) http://www.kgc.cn permanent; 3

23 } 4

cd /usr/local/nginx/html 5

mkdir 1 6

cd 1 7

vim test.html 8

<h1>55555</h1> 9

10 然后访问
undefined
undefined
undefined
undefined
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值