ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

nginx rewite重定向详解及实例解析

2020-09-07 11:31:20  阅读:256  来源: 互联网

标签:index rewrite com html server nginx 详解 rewite root


静态和动态最大的区别是是否调用数据库。

什么是rewrite

将浏览器发送到服务器的请求重写,然后再返回给用户。

就是修改url,提高用户体验

rewrite的用途

  1. 80强转443 (优化用户体验)
  2. 匹配客户端规则,返回对应页面 (优化用户体验),电脑登陆淘宝为www.taobao.com 手机登陆是m.taobao.com
  3. 伪静态(便于做SEO)

什么是伪静态?

原本的动态页面,需要调用数据库,但是在浏览器中的url里面,返回的是一个静态页面,以html,css,js,shtml结尾。

为什么要做伪静态?

  1. 安装
  2. 为了百度的推广做SEO

rewrite使用

rewrite标记flage

flag 作用
last 本条规则匹配完成后,停止匹配,不再匹配后面的规则 开发做
break 本条规则匹配完成后,停止匹配,不再匹配后面的规则 开发做
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永久重定向,地址栏会显示跳转后的地址
[root@web02 /etc/nginx/conf.d]# vi rewrite.conf
server {
        listen 80;
        server_name rewrite.gong.com;
        root /website;

        location ~ ^/break {
                rewrite ^/break /test/ break;
        }
        location ~ ^/last {
                rewrite ^/last /test/ last;
        }
        location /test/ {
                default_type application/json;
                return 200 "ok";
        }
}

break 只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件;
而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。

break请求:
1、请求rewrite.gong.com/break
2、首先:会去查找本地的/website/test/index.html;
3、如果找到了,则返回return的内容;
4、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403

last请求:
1、请求rewrite.gong.com/last
2、首先:会去查找本地的/website/test/index.html;
3、如果找到了,则返回/website/test/index.html的内容;mv
4、如果没找到,会对当前server重新的发起一次请求,rewrite.gong.com/test/
5、如果有location匹配上,则直接返回该location的内容。
4、如果也没有location匹配,再返回404;

所以,在访问/break和/last请求时,虽然对应的请求目录/test都是不存在的,理论上都应该返回404,但是实际上请求/last的时候,是会有后面location所匹配到的结果返回的,原因在于此。

redirect和permanent的区别

[root@web01 /etc/nginx/conf.d]# vi blog_rewrite.conf 
server {
        listen 80;
        server_name www.linux.com;

        location / {
                root /website/dist;
                index index.html;
        }

        location /download {
                rewrite ^(.*)$ http://download.linux.com redirect;
                # return 302 "http://download.linux.com";
        }

        location /friends {
                rewrite ^(.*)$ http://friend.linux.com permanent;
                # return 302 "http://friend.linux.com";
        }
        location /blog {
                # rewrite ^(.*)$ http://blog.linux.com redirect;
                return 302 "http://blog.linux.com";
        }
}

访问下载站点的时候使用的redirect302 跳转

访问下载站点的时候使用的permanent301 跳转

结论

301 和 302 的不同,永久重定向在不清除本地缓存的情况下,服务端挂了,客户端也可以重定向。(.\*)和 ^(.\*)$ 的区别,前者匹配uri后者匹配整个url 。

rewrite案例

案例一

用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html

# 浏览器输入rewrite.gong.com/abc 的时候会自动跳转
[root@web02 /etc/nginx/conf.d]# vi rewrite.conf 
server {
        listen 80;
        server_name rewrite.gong.com;
        root /website;
        index index.html;

        location /abc {
        		# (.*) 表示的是只替换uri部分
                rewrite (.*) /ccc/bbb/2.html redirect;
                 #return 302 /ccc/bbb/2.html;
        }
}

# 2、创建站点
[root@web02 /website]# mkdir -p ccc/bbb
[root@web02 /website]# echo 123 > ccc/bbb/2.html

案例二

用户访问/2018/ccc/2.html实际上真实访问的是/2014/ccc/bbb/2.html

[root@web02 /etc/nginx/conf.d]# vi rewrite.conf 
server {
        listen 80;
        server_name rewrite.gong.com;
        root /website;
        index index.html;

        location /2018 {
        
        		# 这种写法具有一定的可扩展性,引入了变量。
                rewrite ^/2018/(.*)$ /2014/$1 redirect;
                # 如果使用这种写法,在重定向目录过多的情况下显然不好使。
                # rewrite (.*) /2014/ccc/bbb/2.html redirect;
        }
}

[root@web02 /website]# mkdir -p 2014/ccc/bbb/
[root@web02 /website]# echo 222 > 2014/ccc/bbb/2.html

案例三

用户访问/test实际上真实访问的是rewrite.gong.com

[root@web02 /etc/nginx/conf.d]# vi rewrite.conf 
server {
        listen 80;
        server_name rewrite.gong.com;
        root /website;
        index index.html;

        location /test {
                rewrite (.*) http://rewrite.gong.com redirect;
        }
}

这中就像是平时在访问网站的时候在后面输入index.html,会自动的变成一个域名。

案例四

用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html

[root@web02 /etc/nginx/conf.d]# vi rewrite.conf 
server {
        listen 80;
        server_name rewrite.gong.com;
        root /website;

        location / {
        		# 这里需要引入变量,如果说在没有变量的情况下,每变一个uri就要配置一条rewrite就不适用了
                rewrite ^/course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html  redirect;
                 #固定配法
                #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
        }
}


[root@web02 /website]# mkdir -p course/11/22/33/
[root@web02 /website]# echo course > course/11/22/33/course_33.html

案例五

http重定向https

[root@web02 /etc/nginx/conf.d]# vi rewrite.conf 
server {
        listen 80;
        server_name rewrite.gong.com;

        location / {
                rewrite ^(.*)$ https://rewrite.gong.com  redirect;
        }
}

浏览器输入rewrite.gong.com会自动的跳转到 https://rewrite.gong.com

搭建Discuss

环境

角色 IP 主机名
web服务器 10.0.0.8 web02
数据库服务器 10.0.0.51 db01
# 1、编辑配置文件
[root@web02 /etc/nginx/conf.d]# vi dis.conf 
server {
        listen 80;
        server_name dis.gong.com;
        root /website/upload;
        index index.php;

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

                include fastcgi_params;
        }
}

# 2、解压写代码
[root@web02 /website]# unzip Discuz_X3.3_SC_GBK.zip

# 3、授权
[root@web02 ~]# chown www.www -R /website/

# 4、配置数据库服务器
[root@db01 ~]# mysql -uroot -p123

MariaDB [(none)]> create database dis;

MariaDB [(none)]> grant all on dis.* to dis_user@'%' identified by '123';

# 5、重新加载nginx配置文件
[root@web02 ~]# nginx -s reload

把网页上自动生成的放到配置文件中。

root@web02 /etc/nginx/conf.d]# cat dis.conf 
server {
	listen 80;
	server_name dis.gong.com;
	root /website/upload;
	index index.php;

	rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
	rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
	rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
	rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
	rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
	rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
	rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
	rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
	rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
	if (!-e $request_filename) {
		return 404;
}

	location ~ \.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

		include fastcgi_params;
	}
}

nginx中的常用变量

Rewrite与Nginx全局变量

Rewrite在匹配过程中,会用到一些Nginx全局变量

$remote_addr        //获取客户端ip
$binary_remote_addr //客户端ip(二进制)
$remote_port        //客户端port,如:50472
$remote_user        //已经经过Auth Basic Module验证的用户名
$host           //请求主机头字段,否则为服务器名称,如:blog.sakmon.com
$request        //用户请求信息,如:GET ?a=1&b=2 HTTP/1.1
$request_filename   //当前请求的文件的路径名,由root或alias和URI request组合而成,如:/2013/81.html
$status         //请求的响应状态码,如:200
$body_bytes_sent        // 响应时送出的body字节数数量。即使连接中断,这个数据也是精确的,如:40
$content_length        // 等于请求行的“Content_Length”的值
$content_type          // 等于请求行的“Content_Type”的值
$http_referer          // 引用地址
$http_user_agent      // 客户端agent信息,如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
$args            //与$query_string相同 等于当中URL的参数(GET),如a=1&b=2
$document_uri        //与$uri相同  这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2013/81.html
$document_root       //针对当前请求的根路径设置值
$hostname        //如:centos53.localdomain
$http_cookie        //客户端cookie信息
$cookie_COOKIE      //cookie COOKIE变量的值
$is_args    //如果有$args参数,这个变量等于”?”,否则等于”",空值,如?
$limit_rate //这个变量可以限制连接速率,0表示不限速
$query_string       // 与$args相同 等于当中URL的参数(GET),如a=1&b=2
$request_body      // 记录POST过来的数据信息
$request_body_file  //客户端请求主体信息的临时文件名
$request_method       //客户端请求的动作,通常为GET或POST,如:GET
$request_uri          //包含请求参数的原始URI,不包含主机名,如:/2013/81.html?a=1&b=2
$scheme            //HTTP方法(如http,https),如:http
$uri            //这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2013/81.html
$request_completion //如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty),如:OK
$server_protocol    //请求使用的协议,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1
$server_addr        //服务器IP地址,在完成一次系统调用后可以确定这个值
$server_name        //服务器名称,如:blog.sakmon.com
$server_port        //请求到达服务器的端口号,如:80
$server_name    # 当前用户请求的域名

server {
        listen 80;
        server_name test.drz.com;
       # 重定向为https
        rewrite ^(.*)$ https://$server_name$1;
}
$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)
$request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg)

#大多数用于http协议转gttps协议
server {
        listen 80;
        server_name php.drz.com;
        # return的方式也可用作跳转。
        return 302 https://$server_name$request_uri;
}
$scheme 用的协议,比如http或者https

rewrite匹配的优先级

匹配 优先级
server中的rewrite 1
location匹配 2
location中的rewrite 3

标签:index,rewrite,com,html,server,nginx,详解,rewite,root
来源: https://www.cnblogs.com/gshelldon/p/13625917.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有