轻量级web服务器nginx学习(9)———nginx中的rewrite重写规则

1.需求

  • 调整用户浏览的URL,看起来规范 合乎开发及产品人员的需求为了让搜索引擎收录网站内容,让用户体验更好 企业会将动态URL地址伪装成静态地址提供服务网站更换新域名后
  • 例如,访问京东360buy.com会跳转到jd.com根据特殊的变量、目录、客户端信息进行跳转 URL重写有利于网站首选域的确定,对于同一资源页面多条路径的301重定向有助于URL权重的集中

一个测试:
访问淘宝,发现淘宝做了重定向

[kiosk@foundation0 Packages]$ curl -I taobao.com
HTTP/1.1 302 Found ##302临时重定向,301永久重定向
Server: Tengine
Date: Sat, 13 Apr 2019 06:09:35 GMT
Content-Type: text/html
Content-Length: 258
Connection: keep-alive
Location: http://www.taobao.com/

[kiosk@foundation0 Packages]$ curl -I www.taobao.com
HTTP/1.1 302 Found
Server: Tengine
Date: Sat, 13 Apr 2019 06:09:42 GMT
Content-Type: text/html
Content-Length: 258
Connection: keep-alive
Location: https://www.taobao.com/
Set-Cookie: thw=cn; Path=/; Domain=.taobao.com; Expires=Sun, 12-Apr-20 06:09:42 GMT;
Strict-Transport-Security: max-age=31536000

那如何自动实现https加密呢?实质上就是做了重定向 http —> https
(访问www.taobao.com实质自动跳转到https://www.taobao.com )

2.nginx rewrite重写格式

 rewrite    <regex>    <replacement>    [flag];
 关键字      正则        替代内容          flag标记
关键字:其中关键字error_log不能改变
正则:perl兼容正则表达式语句进行规则匹配
替代内容:将正则匹配的内容替换成replacement
flag标记:rewrite支持的flag标记

flag标记说明:
last  #本条规则匹配完成后,继续向下匹配新的location URI规则
break  #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect  #返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent  #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

正则表达式含义:

.匹配除换行符以外的任意字符
?重复0次或1次 例如“do(es)?”能匹配“do”或者“does”,"?“等效于”{0,1}
+重复1次或更多次 如“ol+”能匹配“ol”及“oll”、“oll”,但不能匹配“o”
*重复0次或更多次 #如“ol*”能匹配“o”及“ol”、“oll”
\d匹配数字
^匹配字符串的开始
$匹配字符串的结束
{n}重复n次
{n,}重复n次或更多次
[c]匹配单个字符c
[a-z]匹配a-z小写字母的任意一个

一个例子:

rewrite ^/(.*) http://www.czlun.com/$1 permanent;

##说明:                                        

rewrite为固定关键字,表示开始进行rewrite匹配规则
regex部分是 ^/(.*) ,这是一个正则表达式,匹配完整的域名和后面的路径地址
replacement部分是http://www.czlun.com/$1 
$1,是取自regex部分()里的内容。匹配成功后跳转到的URL。
flag部分 permanent表示永久301重定向标记,即跳转到新的 http://www.czlun.com/$1 地址上

3.实验

  • 编译安装的pcre库–>为了rewrite规则
  • location 是在 server 块中配置。可以根据不同的 URI 使用不同的配置(location 中配置),来处理不同的请求。
    在这里插入图片描述
    实验一:http–>https重定向

实验配置

vim nginx.conf

    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
`
        location / {
            root   /web;
            index  index.html index.htm;
        }
    }

server {
        listen 80;
        server_name www.westos.org;
        rewrite ^/(.*)$ https://www.westos.org/$1;	##$1表示用户在这里输入的内容保留,只会重定向$1前面的
	#rewrite ^/(.*)$ https://www.westos.org/$1 permanent;	永久重定向(可以缓存,临时的不允许缓存)
}

permanent 返回301永久重定向

在这里插入图片描述

测试:

[kiosk@foundation0 Packages]$ curl -I www.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.9
Date: Sat, 13 Apr 2019 06:13:13 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.westos.org/

[kiosk@foundation0 Packages]$ curl -I www.westos.org/index.html #$1
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.9
Date: Sat, 13 Apr 2019 06:16:02 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.westos.org/index.html

[kiosk@foundation0 Packages]$ curl -I www.westos.org/test.html #$1
HTTP/1.1 301 Moved Permanently
Server: nginx/1.15.9
Date: Sat, 13 Apr 2019 06:16:35 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.westos.org/test.html

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
实验二:不同域名网页重定向
实验配置

(1)编辑配置文件
vim   /usr/local/nginx/conf/nginx.conf               
    server {
        listen       443 ssl;
        server_name  www.westos.org;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        #location / {
        #    root   /web;
        #    index  index.html index.htm;
        #}
        location / {
                root    /web;
                index   index.html;
        }

    }
server {
        listen 80;
        server_name www.westos.org;
	#rewrite ^/(.*)$ https://bbs.westos.org/$1 permanent;
	rewrite ^/www$ https://www.westos.org/index.html permanent;	#表示访问bbs.westos.org并且以www结尾的,都定向到https://www.westos.org
	}
(2)创建发布目录
mkdir /bbs
vim /bbs/index.html
<h1>bbs.westos.org</h1>
(3)重启服务
nginx -s reload
尝试访问是否正常

在这里插入图片描述

测试:

在物理上修改本地域名解析
vim /etc/hosts
172.25.24.11 bbs.westos.org
[root@foundation3 ~]# curl -I http://bbs.westos.org/www
HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3
Date: Tue, 22 Oct 2019 11:04:44 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://www.westos.org/index.html

[root@foundation3 ~]# curl http://bbs.westos.org/www
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

在这里插入图片描述
在这里插入图片描述
实验三:多次重定向
实验配置:

server {
        listen 80;
        server_name www.westos.org;

        location / {
                root    /web;
                index   index.html;
        }
}

server {
        listen 80;
        server_name bbs.westos.org;
	#rewrite ^/(.*)$ https://bbs.westos.org/$1 permanent;
        #rewrite ^/www$ http://www.westos.org permanent;
        rewrite ^/www/(.*)$ http://www.westos.org/$1 permanent;

测试:

[root@foundation3 ~]# curl -I http://bbs.westos.org/www/html
HTTP/1.1 301 Moved Permanently  ##永久重定向成功
Server: nginx/1.10.3
Date: Tue, 22 Oct 2019 11:22:39 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://www.westos.org/html

[root@foundation3 ~]# curl  http://bbs.westos.org/www/html
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

在浏览器中访问http://bbs.westos.org/www/html ,发现域名发生改变,变为https://www.westos.org/html 这与我们所写的rewrite规则相符合;没有访问到内容的原因是我们没有建立对应的发布目录

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值