静态和动态最大的区别是是否调用数据库。php
将浏览器发送到服务器的请求重写,而后再返回给用户。css
就是修改url,提升用户体验html
什么是伪静态?mysql
本来的动态页面,须要调用数据库,可是在浏览器中的url里面,返回的是一个静态页面,以html,css,js,shtml结尾。linux
为何要作伪静态?nginx
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(...)标签从新发起请求。web
break请求: 一、请求rewrite.gong.com/break 二、首先:会去查找本地的/website/test/index.html; 三、若是找到了,则返回return的内容; 四、若是没找到该目录则报错404,若是找到该目录没找到对应的文件则403 last请求: 一、请求rewrite.gong.com/last 二、首先:会去查找本地的/website/test/index.html; 三、若是找到了,则返回/website/test/index.html的内容;mv 四、若是没找到,会对当前server从新的发起一次请求,rewrite.gong.com/test/ 五、若是有location匹配上,则直接返回该location的内容。 四、若是也没有location匹配,再返回404;
因此,在访问/break和/last请求时,虽然对应的请求目录/test都是不存在的,理论上都应该返回404,可是实际上请求/last的时候,是会有后面location所匹配到的结果返回的,缘由在于此。sql
[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"; } }
访问下载站点的时候使用的redirect
302 跳转数据库
访问下载站点的时候使用的permanent
301 跳转json
结论
301 和 302 的不一样,永久重定向在不清除本地缓存的状况下,服务端挂了,客户端也能够重定向。(.\*)和 ^(.\*)$ 的区别,前者匹配uri后者匹配整个url 。
用户访问/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; } } # 二、建立站点 [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
角色 | IP | 主机名 |
---|---|---|
web服务器 | 10.0.0.8 | web02 |
数据库服务器 | 10.0.0.51 | db01 |
# 一、编辑配置文件 [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; } } # 二、解压写代码 [root@web02 /website]# unzip Discuz_X3.3_SC_GBK.zip # 三、受权 [root@web02 ~]# chown www.www -R /website/ # 四、配置数据库服务器 [root@db01 ~]# mysql -uroot -p123 MariaDB [(none)]> create database dis; MariaDB [(none)]> grant all on dis.* to dis_user@'%' identified by '123'; # 五、从新加载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; } }
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
匹配 | 优先级 |
---|---|
server中的rewrite | 1 |
location匹配 | 2 |
location中的rewrite | 3 |