Nginx#Rewrite address rewriting

1. What is Rewrite

​ Rewrite Symmetric URL Rewrite, or URL rewriting, is the process of redirecting incoming Web requests to other URLs.

  • The most common application of URL Rewrite is URL pseudo-static, which is a technique for displaying dynamic pages as static pages. For example,
    http://www.123.com/news/index.php?id=123 can be displayed as http://www.123.com/news/123.html after conversion using URLRewrite
    for website designers pursuing perfectionism , Even the address of the webpage wants to look as concise and clear as possible.
    In theory, search engines prefer web pages in the form of static pages, and search engines generally rate static pages higher than dynamic pages. Therefore, UrlRewrite can make the pages of our website easier to be indexed by search engines.
  • From a security perspective, if too many parameters are exposed in the URL, it will undoubtedly cause a certain amount of information leakage, which may be used by some hackers
    and cause certain damage to your system, so the static URL address can give us Bring higher security.
  • Realize the website address redirection, for example, the user visits 360buy.com and redirects it to jd.com. For example, when a user accesses
    port 80 of tianyun.com , it will be redirected to port 443.
2. Rewrite related instructions
  • Nginx Rewrite related commands include if, rewrite, set, return
2.1, if statement
  • Application Environment

    server,location
    

    Agreement level, service level, matching level

    http、server、location

    grammar:

    if (condition) {
          
          }
    if 可以支持如下条件判断匹配符号
    ~ 					正则匹配 (区分大小写)
    ~* 				    正则匹配 (不区分大小写)
    !~                  正则不匹配 (区分大小写)
    !~*		            正则不匹配  (不区分大小写)
    -f 和!-f 		    用来判断是否存在文件
    -d 和!-d 		    用来判断是否存在目录
    -e 和!-e 		    用来判断是否存在文件或目录
    -x 和!-x 		    用来判断文件是否可执行
    
    在匹配过程中可以引用一些Nginx的全局变量
    $args				请求中的参数;
    $document_root	    针对当前请求的根路径设置值;
    $host				请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
    $limit_rate			对连接速率的限制;
    $request_method		请求的方法,比如"GET""POST";
    $remote_addr		客户端地址;
    $remote_port		客户端端口号;
    $remote_user		客户端用户名,认证用;
    $request_filename   当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images /a.jpg)
    $request_uri		当前请求的文件路径名(不带网站的主目录/images/a.jpg)
    $query_string$args相同;
    $scheme				用的协议,比如http或者是https
    $server_protocol	请求的协议版本,"HTTP/1.0""HTTP/1.1";
    $server_addr 		服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
    $server_name		请求到达的服务器名;
    $document_uri$uri一样,URI地址;
    $server_port 		请求到达的服务器端口号;
    
    2.2、Rewrite flag

    The rewrite command redirects the URI based on the expression, or modifies the string. It can be applied to the server, location, and if environment of each rewrite command followed by a flag at the end. The supported flags are:

    last 			    相当于Apache里的[L]标记,表示完成rewrite。默认为last。
    break 				本条规则匹配完成后,终止匹配,不再匹配后面的规则
    permanent 		    返回301永久重定向,浏览器地址会显示跳转后的URL地址
    redirect 			返回302临时重定向,浏览器地址会显示跳转后的URL地址
    返回301,以后URL会被搜索引擎记住
    返回302,浏览器记住一个自己认为好的
    

    The difference between redirect and permanent is the redirection in different ways. For the client, there is no difference in the general state. For search engines, 301 redirects are relatively more friendly. If we redirect an address using the 301 jump method, the search engine will bring the relevant information of the old address to the new address, and at the same time in the search engine index library Completely discard the original old address. When using 302 redirection, search engines (especially google) sometimes check which URL is more intuitive before and after the jump, and then decide which one to display. If it feels that the URL before the jump is better, maybe the address bar will not change, then It is very likely that there will be URL hijacking. When doing URI rewriting, sometimes it is found that the URI contains related parameters. If you need to save these parameters and re-quote during the rewriting process, you can use () and $N to solve them.

Instance

server {
    listen  80;

#  default page,location的这写法表示10.11.67.31/.*都匹配,就是表示以匹配、开头
#    location / {
#       root /usr/share/nginx/html;
#       index index.html;   
#}


# http://10.11.67.31/a/1.html ==> http://10.11.67.31/b/2.html,location /,表示10.11.67.31/.*都可以进行rewrite,location = /,表示10.11.67.31/可以,其他的都不可以。
#    location / {
#        rewrite .* /b/2.html;
#    }

#    location = / {
#        rewrite .* /b/2.html;
#    }

#    location = /a {
#        rewrite .* /b;
#    }
#
#    location /b {
#        root /usr/share/nginx/html;
#        index 2.html;
#    }


#  http://10.11.67.31/2019/a/1.html ==> http://10.11.67.31/2018/a/1.html
#  location /2018/a 浏览器会补全 /2018/a/
#
#    location /2019/a {
#        rewrite /(.*)/(.*) /2018/$2 permanent;
#    }
#
#    location /2018/a {
#        root /usr/share/nginx/html;
#        index 1.html;
#    }


# http://www.sun.com/a/1.html ==> http://jd.com/a/1.html
#
#ocation /a {
#   if ( $host ~* sun.com ){
#   rewrite .* http://baidu.com permanent;
#   }
#
 

# 在访问目录后添加/  (如果目录后已有/,则不加/)
#    location /a/b/c {
#        root /usr/share/nginx/html;
#        index index.html;
#        if ( -d $request_filename ){
#        rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
#        }
#    }

# http://www.tianyun.com/login/tianyun.html
# http://www.tianyun.com/reg/login.html?user=tianyun

#    location /login {
#        rewrite ^/login/(.*)\.html http://$host/reg/login.html?user=$1;
#    }
#    location /reg {
#        root /usr/share/nginx/html;
#        index login.html;
#    }



#http://www.tianyun.com/qf/11-22-33/1.html  ==>  http://www.tianyun.com/qf/11/22/33/1.html
    location /qf {
        rewrite ^/qf/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /qf/$1/$2/$3$4 permanent;
    }
    location /qf/11/22/33 {
        root /usr/share/nginx/html;
        index 1.html;
    }

}

Guess you like

Origin blog.csdn.net/kakaops_qing/article/details/109052910