(nginx) nginx3 地址重写

12、nginx 地址重写 rewrite

1、什么是Rewrite

Rewrite对称URL Rewrite,即URL重写,就是把传入Web的请求重定向到其他URL的过程。

  • URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。比如
    http://www.123.com/news/index.php?id=123 使用URLRewrite 转换后可以显示为 http://www.123 .com/news/123.html对于追求完美主义的网站设计师,就算是网页的地址也希望看起来尽量简洁明快。
    理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所以,UrlRewrite可以让我们网站的网页更容易被搜索引擎所收录。
  • 从安全角度上讲,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客
    利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。
  • 实现网站地址跳转,例如用户访问360buy.com,将其跳转到jd.com。例如当用户访问tianyun.com的80端口时,将其跳转到443端口。

2、Rewrite 相关指令

  • Nginx Rewrite 相关指令有 if 、rewrite、set、return

2.1、if 语句

  • 应用环境

server,location

语法:

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

rewrite  指令根据表达式来重定向URI,或者修改字符串。可以应用于server,location, if环境下每行rewrite指令最后跟一个flag标记,支持的flag标记有:

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

redirect 和 permanent区别则是返回的不同方式的重定向,对于客户端来说一般状态下是没有区别的。而对于搜索引擎,相对来说301的重定向更加友好,如果我们把一个地址采用301跳转方式跳转的话,搜索引擎会把老地址的相关信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。使用302重定向时,搜索引擎(特别是google)有时会查看跳转前后哪个网址更直观,然后决定显示哪个,如果它觉的跳转前的URL更好的话,也许地址栏不会更改,那么很有可能出现URL劫持的现像。在做URI重写时,有时会发现URI中含有相关参数,如果需要将这些参数保存下来,并且在重写过程中重新引用,可以用到 () 和 $N 的方式来解决。

2.3、Rewrite匹配参考示例

本地解析host文件
# http://www.testpm.com/a/1.html ==> http://www.testpm.com/b/2.html
		
    location /a {
        root    /html;
        index   1.html index.htm;
        rewrite .* /b/2.html permanent;
        }
    location /b {
        root    /html;
        index   2.html index.htm;
        }
例2:
# http://www.testpm.com/2019/a/1.html ==> http://www.testpm.com/2018/a/1.html

     location /2019/a {
        root    /var/www/html;
        index   1.html index.hml;
        rewrite ^/2019/(.*)$ /2018/$1 permanent;
        }
     location /2018/a {
        root    /var/www/html;
        index   1.html index.htl;
        }

例3:
# http://www.qf.com/a/1.html ==> http://jd.com

location /a {
        root    /html;
        if ($host ~* www.qf.com ) {
        rewrite .* http://jd.com permanent;
        }
        }

例4:
# http://www.qf.com/a/1.html ==> http://jd.com/a/1.html
location /a {
        root /html;
        if ( $host ~* testpm.com ){
        rewrite .* http://jd.com$request_uri permanent;
        }
        }

例5: 在访问目录后添加/  (如果目录后已有/,则不加/)
# http://www.tianyun.com/a/b/c  ==> http://www.tianyun.com/a/b/c/
# $1: /a/b
# $2: c
# http://$host$1$2$3/
location /a/b/c {
        root    /usr/share/nginx/html;	
        index   index.html index.hml;
        if (-d $request_filename) {
        rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
        }
        }
	
例6:
# http://www.tianyun.com/login/tianyun.html ==>  http://www.tianyun.com/reg/login.html?user=tianyun

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

        }

例7:
#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 /html;
                index   1.html;
        }

2.4、set 指令

set 指令是用于定义一个变量,并且赋值

应用环境:

server,location,if

应用示例

例8:
#http://alice.testpm.com ==> http://www.testpm.com/alice
#http://jack.testpm.com ==> http://www.testpm.com/jack

[root@nginx-server conf.d]# cd /usr/share/nginx/html/
[root@nginx-server html]# mkdir jack alice
[root@nginx-server html]# echo "jack.." >> jack/index.html
[root@nginx-server html]# echo "alice.." >> alice/index.html

a. DNS实现泛解析
*   		IN      A			    网站IP
或者本地解析域名host文件
10.0.105.202 www.testpm.com
10.0.105.202 alice.testpm.com
10.0.105.202 jack.testpm.com
编辑配置文件:
server {
    listen       80;
    server_name  www.testpm.com;

    location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
         if ( $host ~* ^www.testpm.com$) {
                break;
                }
         if ( $host ~* "^(.*)\.testpm\.com$" ) {
                set $user $1;
                rewrite .* http://www.testpm.com/$user permanent;
                }
        }
    location /jack {
         #/usr/share/nginx/html/jack/
         root /usr/share/nginx/html;
         index  index.html index.hml;
        }
    location /alice {
         #/usr/share/nginx/html/alice/
         root /usr/share/nginx/html;
         index index.html index.hml;
        }
}

2.5、return 指令

return 指令用于返回状态码给客户端

server,location,if

应用示例:

例9:如果访问的.sh结尾的文件则返回403操作拒绝错误
server {
    listen       80;
    server_name  www.testpm.cn;
    #access_log  /var/log/nginx/http_access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        }

    location ~* \.sh$ {
        return 403;
        }
}

例10:80 ======> 443 :80转443端口
server {
    listen       80; 
    server_name  www.chaosaigc.com;
    access_log  /var/log/nginx/http_access.log  main;
    return 301 https://www.chaosaigc.com$request_uri;
}

server {
    #SSL 默认访问端口号为 443
    listen 443 ssl;
    #绑定证书的域名
    server_name www.chaosaigc.com;
    access_log  /var/log/nginx/https_access.log  main;
    #ssl on;
    #证书文件的相对路径或绝对路径
    ssl_certificate   /etc/nginx/cert/www.chaosaigc.com_bundle.pem;
    #私钥文件的相对路径或绝对路径
    ssl_certificate_key  /etc/nginx/cert/www.chaosaigc.com.key;
    #指定客户端可以重用会话参数的时间
    ssl_session_timeout 5m;
    #ssl所使用的TLS的协议配置
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #套件配置,配置加密套件,写法遵循 openssl 标准。
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #如果不指定默认为off,当为on时,在使用SSLv3和TLS协议时,服务器加密算法将优于客户端加密算法。
    ssl_prefer_server_ciphers on;

    location / {
        root  /usr/share/nginx/html;
        index index.html index.htm;
    }
}

[root@nginx-server ~]# curl -I http://www.testpm.cn
HTTP/1.1 301 Moved Permanently
Server: nginx/1.16.0
Date: Wed, 03 Jul 2019 13:52:30 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.testpm.cn/

3、last,break详解

[root@localhost test]# cat /etc/nginx/conf.d/last_break.conf 
server {
    listen       80;
    server_name  localhost;
    access_log  /var/log/nginx/last.access.log  main;

    location / {
        #127.0.0.1
        #/usr/share/nginx/html
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location /break/ {
        #127.0.0.1/break/
        #127.0.0.1/test/break.html
        #127.0.0.1/test/test.html
        #/usr/share/nginx/html/test/break.html
        root /usr/share/nginx/html;
        rewrite .* /test/break.html break;
    }
    location /last/ {
        #127.0.0.1/last/index.html
        root /usr/share/nginx/html;
        
        #127.0.0.1/test/last.html
        rewrite .* /test/last.html last;
        
    }
    location /test/ {
        #/usr/share/nginx/html/test/test.html 
        root /usr/share/nginx/html;
        #127.0.0.1/test/test.html
        rewrite .* /test/test.html break;
    }

}
[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test
[root@localhost html]# echo "last" > test/last.html
[root@localhost html]# echo "break" > test/break.html
[root@localhost html]# echo "test" > test/test.html

http://10.0.105.196/break/break.html
http://10.0.105.196/last/last.html

注意:

  • last 标记在本条 rewrite 规则执行完后,会对其所在的 server { … } 标签重新发起请求;
  • break 标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配;
  • 使用 alias 指令时,必须使用 last;
  • 使用 proxy_pass 指令时,则必须使用break。
    4、Nginx 的 https  ( rewrite )(晚自习练习)

server {
        listen       80;
        server_name  *.chaosaigc.com chaosaigc.com;

        if ($host ~* "^www.chaosaigc.com$|^chaosaigc.com$" ) {
                return 301 https://www.chaosaigc.com$request_uri;
        }

        if ($host ~* "^(.*).chaosaigc.com$" ) {
                set $user $1;
                return 301 https://www.chaosaigc.com/$user;
        }

    }

    # Settings for a TLS enabled server.
    server {
        listen       443 ssl;
        server_name  www.chaosaigc.com;

        location / {
                root      /usr/share/nginx/html;
                index     index.php index.html;
        }

        #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        ssl on;
        ssl_certificate cert/214025315060640.pem;
        ssl_certificate_key cert/214025315060640.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        }

5、Apache 的 https ( rewrite )(拓展)

[root@localhost ~]# yum -y install httpd mod_ssl
[root@localhost ~]# vim /etc/httpd/conf.d/vip9999.conf

13、nginx的localtion指令详解

Nginx 的 HTTP 配置主要包括三个区块,结构如下:

http { 						# 这个是协议级别
  include mime.types;
  default_type application/octet-stream;
  keepalive_timeout 65;
  gzip on;
    server {			 # 这个是服务器级别
      listen 80;
      server_name localhost;
        location / {  # 这个是请求级别
          root html;
          index index.html index.htm;
        }
      }
}

1、location 区段

  • location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。
  • location 是有顺序的,会被第一个匹配的location 处理。
  • 基本语法如下:

location [=|~|~*|^~|@] pattern{……}

2、location 前缀含义

=    表示精确匹配,优先级也是最高的 		#老大
^~   表示uri以某个常规字符串开头,理解为匹配url路径即可	#老二		 
~    表示区分大小写的正则匹配  	#三哥
~*   表示不区分大小写的正则匹配	#三姐
/    通用匹配,任何请求都会匹配到		#老四
@    内部服务跳转			#老碎

3、location 配置示例

本地解析域名host

1、没有修饰符 表示:必须以指定模式开始

server {
    listen       80;
    server_name  qf.com;

    location  /abc {
        root    /home/www/nginx;
        index   2.html;
        }

那么,如下是对的:
http://qf.com/abc
http://qf.com/abc?p1
http://qf.com/abc/
http://qf.com/abcde

2、=表示:必须与指定的模式精确匹配

server {
    listen       80;
    server_name  www.testpm.cn;
    access_log  /var/log/nginx/http_access.log  main;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
    location = /abc {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
}

那么,如下是对的:
http://qf.com/abc
http://qf.com/abc?p1
http://qf.com/abc/
如下是错的
http://qf.com/abcde

3、~ 表示:指定的正则表达式要区分大小写

server {
server_name qf.com;
  location ~ ^/abc$ {
    ……
  }
}
那么,如下是对的:
http://qf.com/abc
http://qf.com/abc?p1=11&p2=22
如下是错的
http://qf.com/ABC
http://qf.com/abc/
http://qf.com/abcde

4、~* 表示:指定的正则表达式不区分大小写

server {
server_name qf.com;
location ~* ^/abc$ {
    ……
  }
}
那么,如下是对的:
http://qf.com/abc
http://qf.com/ABC
http://qf.com/abc?p1=11&p2=22
如下是错的:
http://qf.com/abc/
http://qf.com/abcde

5、^~ :类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了。

server {
    listen 80;
    server_name localhost;

    location  ^~ /abc {
        root /var/www/nginx;
        index index.html;
    }

}

6、@ :定义命名 location 区段,这些区段客户段不能访问,只可以由内部产生的请求来访问,如try_files或error_page等

server {
    listen 80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html;
        index index.html;
        try_files /index.htm /a.html /b.html @error;
    }

    location @error {
        return 409;
    }

}

server {
    listen 80;
    server_name 127.0.0.1;

    location /abc {
        return 302 https://www.baidu.com;
    }
    location ~ /abc {
        return 302 https://www.taobao.com;
    }
    location ^~ /abc/ {
        return 302 https://www.jd.com;
    }
    location ~* /ABC {
        return 302 https://www.pingduoduo.com;
    }
    location = /abc {
        return 302 https://www.vip.com;
    }
}

查找顺序和优先级

1:带有“=“的精确匹配优先

2:没有修饰符的精确匹配

3:正则表达式按照他们在配置文件中定义的顺序

4:带有“^~”修饰符的,开头匹配

**5:带有“~” 或“~” 修饰符的,如果正则表达式与URI匹配

6:没有修饰符的,如果指定字符串与URI开头匹配

= 大于 ^~  大于 ~|~*|!~|!~* 大于 /
多个location配置的情况下匹配顺序为:首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
================================================
(1) =:表示完全匹配;
(2) ^~:匹配URI的前缀,并且后面的正则表达式不再匹配,如果一个URI同时满足两个规则的话,匹配最长的规则;
(3) ~:匹配正则表达式,大小写敏感;
(4) ~*:匹配正则表达式,大小写不敏感;
优先级:(1)> (2) > (3) = (4)

location 区段匹配示例

location = / {
  # 只匹配 / 的查询.
  [ configuration A ]
}
location / {
  # 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。
  [ configuration B ]
}
location ^~ /images/ {  www.abc.
  # 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。
  [ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
  # 匹配任何以gif, jpg, or jpeg结尾的文件,但是所有 /images/ 目录的请求将在Configuration C中处理。
  [ configuration D ]
} 
各请求的处理如下例:
	/ → configuration A
	/documents/document.html → configuration B
	/images/1.gif → configuration C
	/documents/1.jpg → configuration D

4、root 、alias 指令区别

location /img/ {
    alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
    root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。]

server {
    listen 80;
    server_name localhost;

    location /cloud {
        #http://localhost/cloud
        #/usr/share/nginx/html/cloud
        root /usr/share/nginx/html;
        index index.html;
    }

    location /cloud {
        #http://localhost/cloud
        #/usr/share/nginx/html
        alias /usr/share/nginx/html/;
        index index.html;
    }
}

  • alias 是一个目录别名的定义,
  • root 则是最上层目录的定义。
  • 还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无

14、nginx 日志配置

1、nginx 日志介绍

nginx 有一个非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志, 所需日志模块 ngx_http_log_module 的支持,日志格式通过 log_format 命令来定义,日志对于统计和排错是非常有利的,下面总结了 nginx 日志相关的配置 包括 access_loglog_formatopen_log_file_cachelog_not_foundlog_subrequestrewrite_logerror_log

# 设置访问日志
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
# 关闭访问日志
access_log off;

  • path 指定日志的存放位置。
  • format 指定日志的格式。默认使用预定义的combined
  • buffer 用来指定日志写入时的缓存大小。默认是64k。
  • gzip 日志写入前先进行压缩。压缩率可以指定,从1到9数值越大压缩比越高,同时压缩的速度也越慢。默认是1。
  • flush 设置缓存的有效时间。如果超过flush指定的时间,缓存中的内容将被清空。
  • if 条件判断。如果指定的条件计算为0或空字符串,那么该请求不会写入日志。

作用域:

可以应用access_log指令的作用域分别有httpserverlocationlimit_except。也就是说,在这几个作用域外使用该指令,Nginx会报错。

access_log /var/logs/nginx-access.log

该例子指定日志的写入路径为/var/logs/nginx-access.log,日志格式使用默认的combined

access_log /var/logs/nginx-access.log buffer=32k gzip flush=1m

该例子指定日志的写入路径为/var/logs/nginx-access.log,日志格式使用默认的combined,指定日志的缓存大小为 32k,日志写入前启用 gzip 进行压缩,压缩比使用默认值 1,缓存数据有效时间为1分钟。

2、log_format 指令

Nginx 预定义了名为 combined 日志格式,如果没有明确指定日志格式默认使用该格式:

log_format combined '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';

如果不想使用Nginx预定义的格式,可以通过log_format指令来自定义。

语法

log_format name [escape=default|json] string ...;

  • name 格式名称。在 access_log 指令中引用。
  • escape 设置变量中的字符编码方式是json还是default,默认是default
  • string 要定义的日志格式内容。该参数可以有多个。参数中可以使用Nginx变量。

log_format 指令中常用的一些变量:

$remote_addr, $http_x_forwarded_for   #记录客户端IP地址
$remote_user                    #记录客户端用户名称
$request                   #记录请求的URL和HTTP协议
$status                     #记录请求状态
$body_bytes_sent      #发送给客户端的字节数,不包括响应头的大小
$bytes_sent               #发送给客户端的总字节数
$http_referer            #记录从哪个页面链接访问过来的,可以根据该参数进行防盗链设置
$http_user_agent      #记录客户端浏览器相关信息
$time_local               #通用日志格式下的本地时间。

自定义日志格式的使用:

access_log /var/logs/nginx-access.log main

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

使用log_format指令定义了一个main的格式,并在access_log指令中引用了它。假如客户端有发起请求:https://qf.com/,我们看一下我截取的一个请求的日志记录:

10.0.105.207 - - [01/Jul/2019:10:44:36 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "-"

我们看到最终的日志记录中$remote_user$http_referer$http_x_forwarded_for都对应了一个-,这是因为这几个变量为空。

面试时:注意日志里面的ip地址一定要在第一列。

3、error_log 指令

错误日志在Nginx中是通过error_log指令实现的。该指令记录服务器和请求处理过程中的错误信息。

语法

配置错误日志文件的路径和日志级别。

error_log file [level];
Default:	
error_log logs/error.log error;

file 参数指定日志的写入位置。

level 参数指定日志的级别。level可以是debug, info, notice, warn, error, crit, alert,emerg中的任意值。可以看到其取值范围是按紧急程度从低到高排列的。只有日志的错误级别等于或高于level指定的值才会写入错误日志中。默认值是error

基本用法

error_log /var/logs/nginx/nginx-error.log

配置段:mainhttp,  mail,  stream,  server, location作用域。

例子中指定了错误日志的路径为:/var/logs/nginx/nginx-error.log,日志级别使用默认的 error

4、open_log_file_cache 指令

每一条日志记录的写入都是先打开文件再写入记录,然后关闭日志文件。如果你的日志文件路径中使用了变量,如  access_log /var/logs/$host/nginx-access.log,为提高性能,可以使用open_log_file_cache指令设置日志文件描述符的缓存。

语法

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

默认值: 
open_log_file_cache off;

  • max 设置缓存中最多容纳的文件描述符数量,如果被占满,采用LRU算法将描述符关闭。
  • inactive 设置缓存存活时间,默认是10s。
  • min_usesinactive时间段内,日志文件最少使用几次,该日志文件描述符记入缓存,默认是1次。
  • valid:设置多久对日志文件名进行检查,看是否发生变化,默认是60s。
  • off:不使用缓存。默认为off。
    基本用法

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

配置段:httpserverlocation作用域中。
例子中,设置缓存最多缓存1000个日志文件描述符,20s内如果缓存中的日志文件描述符至少被被访问2次,才不会被缓存关闭。每隔1分钟检查缓存中的文件描述符的文件名是否还存在。

5、log_not_found 指令

是否在error_log中记录不存在的错误(404)。默认是

基本语法:

log_not_found on | off;
默认值: 
log_not_found on;

配置段: http, server, location作用域。

6、log_subrequest 指令

是否在access_log中记录子请求的访问日志。默认不记录

基本语法

log_subrequest on | off;

默认值: 
log_subrequest off;

配置段:  http, server, location作用域。

7、rewrite_log 指令

ngx_http_rewrite_module模块提供的。用来记录重写日志的。对于调试重写规则建议开启,启用时将在error log中记录notice级别的重写日志。
基本语法:

rewrite_log on | off;

默认值: 
rewrite_log off;

配置段:  http,  server, location,  if作用域。

8、nginx 日志配置总结

Nginx中通过access_logerror_log指令配置访问日志和错误日志,通过log_format我们可以自定义日志格式。如果日志文件路径中使用了变量,我们可以通过open_log_file_cache 指令来设置缓存,提升性能。其他的根据自己的使用场景定义。

详细的日志配置信息可以参考Nginx官方文档

15、nginx 的平滑升级(了解)

1、为什么要对 nginx 平滑升级

随着 nginx 越来越流行,并且 nginx 的优势也越来越明显,nginx 的版本迭代也来时加速模式,1.9.0版本的nginx更新了许多新功能,例如 stream 四层代理功能,伴随着 nginx 的广泛应用,版本升级必然越来越快,线上业务不能停,此时 nginx 的升级就是运维的工作了

nginx 方便地帮助我们实现了平滑升级。其原理简单概括,就是:
(1)在不停掉老进程的情况下,启动新进程。
(2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
(3)新进程接受新请求。
(4)老进程处理完所有请求,关闭所有连接后,停止。
这样就很方便地实现了平滑升级。一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块。

2、nginx 平滑升级原理

多进程模式下的请求分配方式

nginx 默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接(accept)。

信号的接收和处理

nginx 主进程在启动完成后会进入等待状态,负责响应各类系统消息,如SIGCHLD、SIGHUP、SIGUSR2等。

Nginx信号简介

主进程支持的信号

  • TERM, INT: 立刻退出
  • QUIT: 等待工作进程结束后再退出
  • KILL: 强制终止进程
  • HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
  • USR1: 重新打开日志文件
  • USR2: 启动新的主进程,实现热升级
  • WINCH: 逐步关闭工作进程

工作进程支持的信号

  • TERM, INT: 立刻退出
  • QUIT: 等待请求处理结束后再退出
  • USR1: 重新打开日志文件

3、nginx 平滑升级实战

1、查看现有的 nginx 编译参数

[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -V

2、编译

按照原来的编译参数安装 nginx 的方法进行安装,只需要到 make,千万不要 make install 。如果make install 会将原来的配置文件覆盖

[root@nginx-server ~]# cd /usr/local/nginx-1.16.0/
[root@nginx-server nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream --with-http_image_filter_module
[root@nginx-server nginx-1.16.0]# make

3、备份原 nginx 二进制文件

备份二进制文件和 nginx 的配置文件(期间nginx不会停止服务)

[root@nginx-server nginx-1.16.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_$(date +%F)

4、复制新的nginx二进制文件,进入新的nginx源码包

[root@nginx-server nginx-1.16.0]# cp /usr/local/nginx-1.16.0/objs/nginx /usr/local/nginx/sbin/

5、测试新版本的nginx是否正常

[root@nginx-server nginx-1.16.0]# /usr/local/nginx/sbin/nginx -t

6、给nginx发送平滑迁移信号(若不清楚pid路径,请查看nginx配置文件)

[root@nginx-server ~]# kill -USR2 `cat /var/run/nginx.pid`

7、查看nginx pid,会出现一个nginx.pid.oldbin

[root@nginx-server ~]# ll /var/run/nginx.pid*
-rw-r--r-- 1 root root 5 Jul  1 11:29 /var/run/nginx.pid
-rw-r--r-- 1 root root 5 Jul  1 09:54 /var/run/nginx.pid.oldbin

8、从容关闭旧的Nginx进程

[root@nginx-server ~]# kill -WINCH `cat /var/run/nginx.pid.oldbin`

9、此时不重载配置启动旧的工作进程

[root@nginx-server ~]# kill -HUP `cat /var/run/nginx.pid.oldbin`

10、结束工作进程,完成此次升级

[root@nginx-server ~]# kill -QUIT `cat /var/run/nginx.pid.oldbin`

11、验证Nginx是否升级成功

[root@nginx-server ~]# /usr/local/nginx/sbin/nginx -V

4、升级实验

1、安装配置1.6版本的 nginx

[root@localhost ~]# yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel
[root@localhost ~]# tar xzf nginx-1.6.3.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.6.3
[root@localhost nginx-1.6.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.6.3]# make && make install
[root@localhost nginx-1.6.3]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx -t 
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx 
[root@localhost nginx-1.6.3]# netstat -lntp
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13989/nginx: master

2、查看版本和模块

[root@localhost nginx-1.6.3]# /usr/local/nginx/sbin/nginx -V 
nginx version: nginx/1.6.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.6.3]# echo "nginx1.6" > /usr/local/nginx/html/index.html
[root@localhost nginx-1.6.3]# yum install -y elinks

4、访问验证

[root@localhost nginx-1.6.3]# elinks 10.0.105.189

5、升级nginx

将 nginx 版本进行升级 并在不影响业务的情况下添加 SSL 和 pcre 模块

[root@localhost ~]# tar xzf nginx-1.12.2.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=ngiinx --with-http_stub_status_module --with-http_ssl_module --with-pcre
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# cd
[root@localhost ~]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_lod
[root@localhost ~]# cp /usr/local/nginx-1.12.2/objs/nginx /usr/local/nginx/sbin/
[root@localhost ~]# mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@localhost ~]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
[root@localhost ~]# ls /usr/local/nginx/logs/
access.log  error.log  nginx.pid
[root@localhost ~]# ps aux | grep nginx 
root      13989  0.0  0.0  24860   952 ?        Ss   13:55   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     13990  0.0  0.1  25284  1720 ?        S    13:55   0:00 nginx: worker process
root      16525  0.0  0.0 112708   976 pts/2    S+   14:09   0:00 grep --color=auto nginx

总结:

在随堂笔记中

写博客23 24

sql注入

set ts=4 更改tab键

整一个模版机子 ok

break不太懂子 ok

在conf.d子配置文件子 ok

break:直接访问下面子 ok

permanent:301 永久子 ok

redircct:302临时子 ok

很多网站都在让用户强制从http到s,为了安全

6加$host 和不加的区别子 ok

整理一下这三天的知识点的范围

rewrite

image-20240424111732009

1 :写死

image-20240424112646020

灵活

image-20240424113137336

2是1的变种

3

image-20240424113946084

4访问之后没有jd.com/a/1.html 当跳转到京东之后,京东没有这个页面也就是404,他会跳转的主页

image-20240424114320864

变种 里面的rewrite那一行必须是s,否则搜索不出来

image-20240424152454045

5.以后研发和测试会要求咱这样写,如果没有/就要加一个

image-20240424154024053

6没太懂

rewrite ^/login/(.).html$ http://$host/reg/login.html?user=$1;

rewrite ^/login/(.).html$ /reg/login.html?user=$1;的区别

  • 第一个规则会改变URL的协议(可能从https变为http),并且构造了一个完整的URL。

  • 第二个规则仅在服务器的内部路径上进行重写,不会改变URL的协议或其他部分。

image-20240424140822541

7、访问2024*04-24会直接找到2024/04/24,注意把逻辑搞清楚

image-20240424141933155

set

1

#http://alice.testpm.com ==> http://www.testpm.com/alice #http://jack.testpm.com ==> http://www.testpm.com/jack

image-20240424194541130

2、return的用法

image-20240424195750568

周五在测试

image-20240424200115565

image-20240424200157003

location

image-20240424201754705

rewrite-c.conf 优先级

提示:

image-20240424203512949

  • 10
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值