目录html
Nginx服务器利用ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perl compatible regularex pression),所以编译以前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是很是有用的功能,好比它能够在咱们改变网站结构以后,不须要客户端修改原来的书签,也无需其余网站修改咱们的连接,就能够设置为访问,另外还能够在必定程度上提升网站的安全性。前端
用于条件匹配判断,并根据条件判断结果选择不一样的Nginx配置,能够配置在server或location块中进行配置,Nginx的if语法仅能使用if作单次判断,不支持使用if else或者if elif这样的多重判断。
if (条件匹配) { action }
使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,不然认为false,变量与表达式之间使用如下符号连接:nginx
=: #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false。 !=: #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false。 ~: #表示在匹配过程当中区分大小写字符,(能够经过正则表达式匹配),知足匹配条件为真,不知足为假。 !~:#为区分大小写字符且匹配结果不匹配,不知足为真,知足为假。 ~*: #表示在匹配过程当中不区分大小写字符,(能够经过正则表达式匹配),知足匹配条件为真,不知足为假。 !~*: #为不区分大小字符且匹配结果不匹配,知足为假,不知足为真。 -f 和 ! -f: #判断请求的文件是否存在和是否不存在 -d 和 ! -d: #判断请求的目录是否存在和是否不存在。 -x 和 ! -x: #判断文件是否可执行和是否不可执行。 -e 和 ! -e: #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软连接)。
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf server { listen 80; server_name www.OpengSD.net; charset utf-8; error_page 500 502 503 504 404 =1000 /error.html; #access_log /data/nginx/logs/www-taotaobao-net_access.log; error_log /data/nginx/logs/www-taotaobao-net_error.log; location = /error.html { root html; } # if配置段 location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( $scheme = http ){ echo "if-----> $scheme"; } } } [root@ubuntu ~]#systemctl reload nginx.service
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( $scheme != http ){ echo "if-----> $scheme"; } } [root@ubuntu ~]#systemctl reload nginx.service
# 区分大小写 [root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( $scheme ~ http ){ echo "if-----> $scheme"; } } [root@ubuntu ~]#systemctl reload nginx.service # 不区分大小写 [root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( $scheme ~* http ){ echo "if-----> $scheme"; } } [root@ubuntu ~]#systemctl reload nginx.service
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( $scheme !~ http ){ echo "if-----> $scheme"; } } [root@ubuntu ~]#systemctl reload nginx.service
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( -f /data/nginx/html/pc/index.html ){ echo "index.html page is in the /apps/nginx/html/index.html"; } } [root@ubuntu ~]#systemctl reload nginx.service
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( !-f /data/nginx/html/pc/index.html ){ echo "index.html page is in the /apps/nginx/html/index.html"; } } [root@ubuntu ~]#systemctl reload nginx.service
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( -d /data/xxxx ){ echo "nginx working dir is /data/xxxx"; } } [root@ubuntu ~]#systemctl reload nginx.service # /data下是没有xxxx目录的 [root@ubuntu ~]#ll /data/ total 1040 drwxr-xr-x 4 root root 4096 Jan 10 20:36 ./ drwxr-xr-x 25 root root 4096 Jan 5 17:17 ../ drwx------ 2 root root 16384 Jan 3 20:02 lost+found/ drwxr-xr-x 6 root root 4096 Jan 9 17:57 nginx/ -rw-r--r-- 1 root root 1032630 Dec 30 18:49 nginx-1.16.1.tar.gz
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( -d /data/nginx ){ echo "nginx working dir is /data/nginx"; } } [root@ubuntu ~]#systemctl reload nginx.service # /data下是没有xxxx目录的 [root@ubuntu ~]#ll /data/ total 1040 drwxr-xr-x 4 root root 4096 Jan 10 20:36 ./ drwxr-xr-x 25 root root 4096 Jan 5 17:17 ../ drwx------ 2 root root 16384 Jan 3 20:02 lost+found/ drwxr-xr-x 6 root root 4096 Jan 9 17:57 nginx/ -rw-r--r-- 1 root root 1032630 Dec 30 18:49 nginx-1.16.1.tar.gz
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( -x /data/nginx/html/pc/index.html ){ echo "index.html can not exec"; } } [root@ubuntu ~]#systemctl reload nginx.service # 没有执行权限 [root@ubuntu ~]#ll /data/nginx/html/pc/index.html -rw-r--r-- 1 root root 176 Jan 5 18:51 /data/nginx/html/pc/index.html
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( !-x /data/nginx/html/pc/index.html ){ echo "index.html can not exec"; } } [root@ubuntu ~]#systemctl reload nginx.service
# 判断存在的目录 [root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( -e /bin ){ echo "/bin is a symbolic"; } } [root@ubuntu ~]#systemctl reload nginx.service # 判断不存在的目录 [root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( -e /xiaobawang ){ echo "/xiaobawang is a symbolic"; } } [root@ubuntu ~]#systemctl reload nginx.service # 取反判断不存在的目录 [root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; if ( !-e /xiaobawang ){ echo "/xiaobawang is a symbolic"; } } [root@ubuntu ~]#systemctl reload nginx.service
# 字符串非空 [root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; set $xiaobawang "xiaobawang"; if ( $xiaobawang ){ echo "string is true"; } } [root@ubuntu ~]#systemctl reload nginx.service # 字符串为空 [root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; set $xiaobawang ""; if ( $xiaobawang ){ echo "string is true"; } } [root@ubuntu ~]#systemctl reload nginx.service
指定key并给其定义一个变量,变量能够调用Nginx内置变量赋值给key,另外set定义格式为set $key $value,及不管是key仍是value都要加$符号。
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /main { root /data/nginx/html/pc; index index.html; default_type text/html; set $name magedu; echo $name; set $my_port $server_port; # $server_port 是当前端口。 echo $my_port; } [root@ubuntu ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx//conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx//conf/nginx.conf test is successful [root@ubuntu ~]#systemctl reload nginx.service
用于中断当前相同做用域(location)中的其余Nginx配置,与该指令处于同一做用域的Nginx配置中,位于它前面的配置生效,位于后面的指令配置就再也不生效了,Nginx服务器在根据配置处理请求的过程当中遇到该指令的时候,回到上一层做用域继续向下读取配置,该指令能够在server块和location块以及if块中使用。git
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; default_type text/html; set $name magedu; echo $name; break; # 中断指令 set $my_port $server_port; echo $my_port; } [root@ubuntu ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx//conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx//conf/nginx.conf test is successful [root@ubuntu ~]#systemctl reload nginx.service
从nginx版本0.8.2开始支持,return用于完成对请求的处理,并直接向客户端返回响应状态码,好比其能够指定重定向URL(对于特殊重定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的全部配置都将不被执行,return能够在server、if和location块进行配置。web
return code; #返回给客户端指定的HTTP状态码 return code (text); #返回给客户端的状态码及响应体内容,能够调用变量 return code URL; #返回给客户端的URL地址
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; default_type text/html; index index.html; if ( $scheme = http ){ #return 666; #return 666 "not allow http"; # 指定提示文本内容 #return 301 http://www.baidu.com; # 指定重定向URL return 500 "service error"; echo "if-----> $scheme"; #return后面的将再也不执行 } if ( $scheme = https ){ echo "if ----> $scheme"; } } [root@ubuntu ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx//conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx//conf/nginx.conf test is successful [root@ubuntu ~]#systemctl reload nginx.service
设置是否开启记录ngx_http_rewrite_module模块日志记录到error_log日志文件当中,能够配置在http、server、location或if当中,须要日志级别为notice 。正则表达式
# 修改主配置文件日志级别 [root@ubuntu ~]#vim /apps/nginx/conf/nginx.conf error_log logs/error.log notice; # 这条默认是禁用的打开就能够了。只能这个级别其余级别不行。 # 修改子配置文件 [root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /main { index index.html; default_type text/html; set $name magedu; echo $name; rewrite_log on; break; set $my_port $server_port; echo $my_port; } # 测试访问 [root@ubuntu data]#tail -f /apps/nginx/logs/error.log 2019/02/27 15:10:02 [warn] 5815#0: *3 using uninitialized "my_port" variable, client: 192.168.39.1, server: www.opengsd.net, request: "GET /main HTTP/1.1", host: "www.magedu.net"
经过正则表达式的匹配来改变URI,能够同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI作具体处理,如下是URL和URI的具体介绍:express
URI(universal resource identifier):通用资源标识符,标识一个资源的路径,能够不带协议。 URL(uniform resource location):统一资源定位符,是用于在Internet中描述资源的字符串,是URI的子集,主要包括传输协议(scheme)、主机(IP、端口号或者域名)和资源具体地址(目录和文件名)等三部分,通常格式为 scheme://主机名[:端口号][/资源路径],如:http://www.a.com:8080/path/file/index.html就是一个URL路径,URL必须带访问协议。 每一个URL都是一个URI,可是URI不都是URL。 例如: http://example.org:8080/path/to/resource.txt #URI/URL ftp://example.org/resource.txt #URI/URL /absolute/path/to/resource.txt #URI
rewrite的官方介绍地址:
rewrite能够配置在server、location、if,其具体使用方式为:json
rewrite regex replacement [flag];
rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI。 注意:若是在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会从新一轮的替换检查,隐含有循环机制,但不超过10次;若是超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制,若是替换后的URL是以http://或https://开头,则替换结果会直接以重向返回给客户端, 即永久重定向301。ubuntu
利用nginx的rewrite的指令,能够实现url的从新跳转,rewrtie有四种不一样的flag,分别是redirect(临时重定向)、permanent(永久重定向)、break和last。其中前两种是跳转型的flag,后两种是代理型,跳转型是指有客户端浏览器从新对新地址进行请求,代理型是在WEB服务器内部实现跳转的。
Syntax: rewrite regex replacement [flag]; #经过正则表达式处理用户请求并返回替换后的数据包。 Default: —
Context: server, location, if
redirect; #临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端从新发起请求;使用相 对路径,或者http://或https://开头,状态码:302 permanent; #重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端从新发起请求,状态码:301 last; #重写完成后中止对当前URI在当前location中后续的其它重写操做,然后对新的URL启动新一轮重写检查,不建议 在location中使用 break; #重写完成后中止对当前URL在当前location中后续的其它重写操做,然后直接将匹配结果返还给客户端即结束循环 并返回数据给客户端,建议在location中使用
要求:因业务须要,将访问源域名 www.opengsd.net 的请求永久重定向到www.opengsd.com
临时重定向不会缓存域名解析记录(A记录),可是永久重定向会缓存。
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; rewrite / http://www.jd.com permanent; # 永久重定向 状态码301 #rewrite / http://www.jd.com redirect; # 临时重定向 状态码302 } # 语法检查 [root@ubuntu ~]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx//conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx//conf/nginx.conf test is successful # 从新加载配置 [root@ubuntu ~]#/apps/nginx/sbin/nginx -s reload
域名永久重定向,京东早期的域名 www.360buy.com 因为与360公司相似,因而后期永久重定向到了 www.jd.com,永久重定向会缓存DNS解析记录。
域名临时重定向,告诉浏览器域名不是固定重定向到当前目标域名,后期可能随时会更改,所以浏览器不会缓存当前域名的解析记录,而浏览器会缓存永久重定向的DNS解析记录,这也是临时重定向与永久重定向最大的本质区别。
要求:访问about的请求被转发至images,而访问images传递请求再次被转发至images1,以此测试last和break分别有什么区别:
break测试案例:当客户端访问break的时候,测试经过rewrite将URL重写为test1,而后再经过rewrite将test1重写为test2测试两条write规则最终哪一条生效,而且测试重写后的URL会不会到其余location从新匹配。
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /break { #return 666 "break"; root /data/nginx; index index.html; rewrite ^/break/(.*) /test1/$1 break; # break匹配成功后再也不向下匹配,也不会跳转到其余的location,即直接结束匹配并给客户端返回结果数据。 rewrite ^/test1/(.*) /test2/$1 break; # break不会匹配后面的rewrite规则也不匹配其余location } location = /test1 { return 999 "new test1"; #index index.html; #root /data/nginx; } location = /test2 { return 666 "new test2"; #root /opt/nginx; #index index.html; } [root@ubuntu ~]#systemctl reload nginx.service # 测试下面两个location的显示 [root@centos7 ~]#curl -L http://www.opengsd.net/test1 new test1[root@centos7 ~]#curl -L http://www.opengsd.net/test2 new test2[root@centos7 ~]# # 建立break目录测试页面 [root@ubuntu ~]#mkdir /data/nginx/break [root@ubuntu ~]#echo "break html" > /data/nginx/break/index.html # 建立连个test资源目录并写入内容(测试方便) [root@ubuntu ~]#mkdir /data/nginx/test1 [root@ubuntu ~]#mkdir /data/nginx/test2 [root@ubuntu ~]#echo "test1" > /data/nginx/test1/index.html [root@ubuntu ~]#echo "test2" > /data/nginx/test2/index.html
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /break { #return 666 "break"; root /data/nginx; index index.html; #rewrite ^/break/(.*) /test1/$1 break; #rewrite ^/test1/(.*) /test2/$1 break; # 这两行注释掉 } location = /test1 { return 999 "new test1"; #index index.html; #root /data/nginx; } location = /test2 { return 666 "new test2"; #root /opt/nginx; #index index.html; } [root@ubuntu ~]#systemctl reload nginx.service # 测试 ## 这是没建立目录以前找不到资源 [root@centos7 ~]#curl -L http://www.opengsd.net/break <html> <head><title>404 Not Found</title></head> <body> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.16.1</center> </body> </html> ## 建立完目录和测试页面 [root@centos7 ~]#curl -L http://www.opengsd.net/break break html
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /break { #return 666 "break"; root /data/nginx; index index.html; rewrite ^/break/(.*) /test1/$1 break; #rewrite ^/test1/(.*) /test2/$1 break; } location = /test1 { return 999 "new test1"; #index index.html; #root /data/nginx; } location = /test2 { return 666 "new test2"; #root /opt/nginx; #index index.html; } [root@ubuntu ~]#systemctl reload nginx.service # 测试(访问的试break下的index.html可是显示的倒是test1的index.html内容,说明rewrite重写成功) [root@centos7 ~]#curl -L http://www.opengsd.net/break/index.html test1 [root@centos7 ~]#curl --head http://www.opengsd.net/break/index.html HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Wed, 15 Jan 2020 09:04:54 GMT Content-Type: text/html; charset=utf-8 Content-Length: 6 Last-Modified: Wed, 15 Jan 2020 08:57:19 GMT Connection: keep-alive Keep-Alive: timeout=60 ETag: "5e1ed3ef-6" Accept-Ranges: bytes
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /break { #return 666 "break"; root /data/nginx; index index.html; rewrite ^/break/(.*) /test1/$1 break; rewrite ^/test1/(.*) /test2/$1 break; } location = /test1 { return 999 "new test1"; #index index.html; #root /data/nginx; } location = /test2 { return 666 "new test2"; #root /opt/nginx; #index index.html; } [root@ubuntu ~]#systemctl reload nginx.service # 测试访问 [root@centos7 ~]$curl -L http://www.opengsd.net/break/index.html test1 #最终的结果不会超出break的location并且不会继续匹配当前location后续的write规则,并且直接返回数据给客户端。
location /statics { root /data/nginx; index index.html; rewrite ^/statics/(.*) /static/$1 break; }
last:对某个location的URL匹配成功后会中止当前location的后续rewrite规则,并结束当前location,而后将匹配生成的新URL跳转至其余location继续匹配,直到没有location可匹配后将最后一次location的数据返回给客户端。
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /last { root /data/nginx; index index.html; #rewrite ^/last/(.*) /test1/$1 last; #rewrite ^/test1/(.*) /test2/$1 last; } location /test1 { #return 999 "new test1"; root /data/nginx; index index.html; rewrite ^/test1/(.*) /test2/$1 last; } location /test2 { return 666 "new test2"; #root /opt/nginx; #index index.html; } [root@ubuntu ~]#systemctl reload nginx.service # 建立资源页面 [root@ubuntu ~]#mkdir /data/nginx/last [root@ubuntu ~]#echo "last" > /data/nginx/last/index.html # 测试在访问last资源时打开test1的rewrite规则会匹配到哪里 [root@centos7 ~]$curl -L http://www.opengsd.net/last/index.html # 由于上面规则没有打开因此访问last资源后面的这个rewrite不生效 last # 访问test1测试(这里的规则会生效) [root@centos7 ~]$curl -L http://www.opengsd.net/test1/index.html new test2[root@centos7 ~]$
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /last { root /data/nginx; index index.html; rewrite ^/last/(.*) /test1/$1 last; #rewrite ^/test1/(.*) /test2/$1 last; } location /test1 { #return 999 "new test1"; root /data/nginx; index index.html; rewrite ^/test1/(.*) /test2/$1 last; } location /test2 { return 666 "new test2"; #root /opt/nginx; #index index.html; } [root@ubuntu ~]#systemctl reload nginx.service # 测试结果上一个locatin 的rewrite生效重写到test1,test1的rewrite生效重写到test2。 [root@centos7 ~]$curl -L http://www.opengsd.net/last/index.html new test2[root@centos7 ~]$
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /last { root /data/nginx; index index.html; rewrite ^/last/(.*) /test1/$1 last; rewrite ^/test1/(.*) /test2/$1 last; # www.opengsd.net/last/index.html ---> www.opengsd.net/test1/index.html # 这里是把访问的URL重写,而后到下面的location去匹配,若是下面的location还有rewrite规则,则再次去匹配直到没有能够匹配的。 } location /test1 { #return 999 "new test1"; root /data/nginx; index index.html; #rewrite ^/test1/(.*) /test2/$1 last; } location /test2 { return 666 "new test2"; #root /opt/nginx; #index index.html; # 这里能够接着定义rewrite规则,可是要有对应的location。 } [root@ubuntu ~]#systemctl reload nginx.service # 测试第二条的rewrite并无生效 [root@centos7 ~]$curl -L http://www.opengsd.net/last/index.html test1 # 单个location里面多个rewrite不会循环,可是会跳出当前location去匹配其余的location,直到没有可匹配,而后返回结果到用户。
[root@ubuntu ~]#vim /apps/nginx/conf/conf.d/pc.conf location /last { root /data/nginx; index index.html; rewrite ^/last/(.*) /test1/$1 last; #rewrite ^/test1/(.*) /test2/$1 last; } location /test1 { #return 999 "new test1"; root /data/nginx; index index.html; rewrite ^/test1/(.*) /test2/$1 break; } location /test2 { return 666 "new test2"; #root /opt/nginx; #index index.html; } [root@ubuntu ~]#systemctl reload nginx.service # 测试(这里显示test2 的内容的缘由是test2下面有这个文件因此会显示,并且他的匹配规则并无跳出当前的location) [root@centos7 ~]$curl -L http://www.opengsd.net/last/index.html test2 # 若是没有这个目录或文件则会报错(说明到上一个rewrite定义的break哪里就结束了匹配) [root@ubuntu ~]#rm -f /data/nginx/test2/index.html [root@centos7 ~]$curl -L http://www.opengsd.net/last/index.html <html> <head><title>404 Not Found</title></head> <body> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.16.1</center> </body> </html>
last适用于要不改变客户端访问方式可是需作屡次目的URL重写的场景,场景不是不少。
要求:基于通讯安全考虑公司网站要求全站https,所以要求将在不影响用户请求的状况下将http请求所有自动跳转至https,另外也能够实现部分location跳转。
# 自签名CA证书 [root@ubuntu certs]#openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt Can't load /root/.rnd into RNG 140148192780736:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:88:Filename=/root/.rnd Generating a RSA private key .............................................++++ ...........................++++ writing new private key to 'ca.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:BeiJing Locality Name (eg, city) []:BeiJing Organization Name (eg, company) [Internet Widgits Pty Ltd]:opengsd.net Organizational Unit Name (eg, section) []:opengsd.net Common Name (e.g. server FQDN or YOUR name) []:www.opengsd.net Email Address []:1060351846@qq.com # 生成证书申请文件 [root@ubuntu certs]#openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.opengsd.net.key -out www.opengsd.net.csr Can't load /root/.rnd into RNG 140607801209280:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:88:Filename=/root/.rnd Generating a RSA private key ...........++++ ....................++++ writing new private key to 'www.opengsd.net.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:BeiJing Locality Name (eg, city) []:BeiJing Organization Name (eg, company) [Internet Widgits Pty Ltd]:opengsd.net Organizational Unit Name (eg, section) []:opengsd.net Common Name (e.g. server FQDN or YOUR name) []:www.opengsd.net Email Address []:1060351846@qq.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: # 这个是要给私钥加密吗?(为了方便我没设置) An optional company name []: # 查看生成的证书申请文件 [root@ubuntu certs]#ll total 24 drwxr-xr-x 2 root root 4096 Jan 15 18:42 ./ drwxr-xr-x 12 nginx nginx 4096 Jan 15 18:33 ../ -rw-r--r-- 1 root root 2171 Jan 15 18:36 ca.crt -rw------- 1 root root 3272 Jan 15 18:35 ca.key -rw-r--r-- 1 root root 1765 Jan 15 18:42 www.opengsd.net.csr -rw------- 1 root root 3272 Jan 15 18:40 www.opengsd.net.key # 申请签发证书 [root@ubuntu certs]#openssl x509 -req -days 3650 -in www.opengsd.net.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.opengsd.net.crt Signature ok subject=C = CN, ST = BeiJing, L = BeiJing, O = opengsd.net, OU = opengsd.net, CN = www.opengsd.net, emailAddress = 1060351846@qq.com Getting CA Private Key # 查看证书内容 [root@ubuntu certs]#openssl x509 -in www.opengsd.net.crt -noout -text Certificate: Data: Version: 1 (0x0) Serial Number: 78:e8:d1:d9:e5:6f:37:14:23:61:00:d8:cd:82:fa:b3:23:d8:33:1e Signature Algorithm: sha256WithRSAEncryption Issuer: C = CN, ST = BeiJing, L = BeiJing, O = opengsd.net, OU = opengsd.net, CN = www.opengsd.net, emailAddress = 1060351846@qq.com Validity Not Before: Jan 15 10:42:57 2020 GMT Not After : Jan 12 10:42:57 2030 GMT Subject: C = CN, ST = BeiJing, L = BeiJing, O = opengsd.net, OU = opengsd.net, CN = www.opengsd.net, emailAddress = 1060351846@qq.com Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (4096 bit)
以上的生成和配置证书能够参考上一篇博客。
[root@ubuntu certs]#vim /apps/nginx/conf/conf.d/pc.conf server { listen 443 ssl; listen 80; ssl_certificate /apps/nginx/certs/www.opengsd.net.crt; ssl_certificate_key /apps/nginx/certs/www.opengsd.net.key; ssl_session_cache shared:sslcache:20m; ssl_session_timeout 10m; server_name www.OpengSD.net; charset utf-8; location / { root /data/nginx/html/pc; index index.html; if ($scheme = http ){ #未加条件判断,会致使死循环 rewrite / https://www.opengsd.net permanent; } } } [root@ubuntu certs]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx//conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx//conf/nginx.conf test is successful [root@ubuntu certs]#systemctl reload nginx.service # 测试直接访问https [root@centos7 ~]$curl -L -k -i https://www.opengsd.net/ HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Wed, 15 Jan 2020 10:53:33 GMT Content-Type: text/html; charset=utf-8 Content-Length: 15 Last-Modified: Tue, 14 Jan 2020 11:39:27 GMT Connection: keep-alive Keep-Alive: timeout=60 ETag: "5e1da86f-f" Accept-Ranges: bytes 192.168.39.184 # 访问http自动跳转为https [root@centos7 ~]$curl -L -k -i http://www.opengsd.net/ HTTP/1.1 301 Moved Permanently Server: nginx/1.16.1 Date: Wed, 15 Jan 2020 10:56:47 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive Keep-Alive: timeout=60 Location: https://www.opengsd.net HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Wed, 15 Jan 2020 10:56:48 GMT Content-Type: text/html; charset=utf-8 Content-Length: 15 Last-Modified: Tue, 14 Jan 2020 11:39:27 GMT Connection: keep-alive Keep-Alive: timeout=60 ETag: "5e1da86f-f" Accept-Ranges: bytes 192.168.39.184
若是是由于规则匹配问题致使的陷入死循环,则报错以下:
[root@ubuntu certs]#vim /apps/nginx/conf/conf.d/pc.conf location / { root /data/nginx/html/pc; index index.html; if (!-f $request_filename ){ rewrite (.*) http://www.opengsd.net/index.html; } } [root@ubuntu certs]#/apps/nginx/sbin/nginx -t nginx: the configuration file /apps/nginx//conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx//conf/nginx.conf test is successful [root@ubuntu certs]#systemctl reload nginx.service
none:请求报文首部没有referer首部,好比用户直接在浏览器输入域名访问web网站,就没有referer信息。 blocked:请求报文有referer首部,但无有效值,好比为空。 server_names:referer首部中包含本主机名及即nginx 监听的server_name。 arbitrary_string:自定义指定字符串,但可以使用*做通配符。 regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头,例如: ~.*\.opengsd\.com。
#经过搜索引擎访问web网站的referer信息: ==> /apps/nginx/logs/access_json.log <== {"@timestamp":"2019-02- 28T13:58:46+08:00","host":"192.168.39.184","clientip":"192.168.39.1","siz e":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"- ","http_host":"www.opengsd.net","uri":"/index.html","domain":"www.opengsd.net","xff":"- ","referer":"https://www.baidu.com/s?ie=utf- 8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=www.opengsd.net&oq=www.opengsd.net&rsv_pq=d630606800 02eb69&rsv_t=de01TWnmyTdcJqph7SfI1hXgXLJxSSfUPcQ3QkWdJk%2FLNrN95ih3XOhbRs4&rqlang=cn&rsv _enter=1&inputT=321&rsv_sug3=41&rsv_sug2=0&rsv_sug4=1626","tcp_xff":"","http_user_agent" :"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36","status":"304"}
在一个web 站点盗链另外一个站点的资源信息,好比图片、视频等。
[root@s2 conf.d]# pwd /apps/nginx/conf/conf.d [root@s2 conf.d]# cat mageedu.net.conf server { listen 80; server_name www.mageedu.net; location / { index index.html; root "/data/nginx/html/mageedu"; access_log /apps/nginx/logs/www.mageedu.net.log access_json; } } #准备盗链web页面: [root@s2 conf.d]# mkdir /data/nginx/html/mageedu [root@s2 conf.d]# cat /data/nginx/html/mageedu/index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>盗链页面</title> </head> <body> <a href="http://www.magedu.net">测试盗链</a> <img src="http://www.magedu.net/images/1.jpg"> </body> </html> #重启Nginx并访问http://www.mageedu.net/测试 #验证两个域名的日志,是否会在被盗连的web站点的日志中出现如下盗链日志信息: ==> /apps/nginx/logs/access_json.log <== {"@timestamp":"2019-02- 28T13:27:37+08:00","host":"192.168.7.102","clientip":"192.168.0.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.magedu.net","uri":"/images/1.jpg","domain":"www.magedu.net","xff":"-","referer":"http://www.mageedu.net/","tcp_xff":"","http_user_agent":"Mozilla/5.0(Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0","status":"304"}
基于访问安全考虑,nginx支持经过ungx_http_referer_module模块 官网文档 检查访问请求的referer信息是否有效实现防盗链功能。
[root@s2 ~]# vim /apps/nginx/conf/conf.d/pc.conf location /images { root /data/nginx/html/pc; index index.html; valid_referers none blocked server_names *.example.com example.* www.example.org/galleries/ ~\.google\.; if ($invalid_referer) { return 403; } # 定义防盗链: location ^~ /images { root /data/nginx; index index.html; valid_referers none blocked server_names *.magedu.com www.magedu.* api.online.test/v1/hostlist ~\.google\. ~\.baidu\.; #定义有效的referer if ($invalid_referer) { #假如是使用其余的无效的referer访问: return 403; #返回状态码403 } } #重启Nginx并访问测试