目录

  • https模块
  • 重定向
  • 防盗链技术


https模块

ngx_http_ssl_module模块

  1. ssl
    为指定虚拟机启用HTTPS protocol, 建议用listen指令代替
Syntax:	    ssl on | off;
Default:    ssl off;
Context:    http, server
  1. ssl_certificate
    当前虚拟主机使用PEM格式的证书文件
Syntax:	    ssl_certificate file;
Default:    —
Context:    http, server
  1. ssl_certificate_key
    当前虚拟主机上与其证书匹配的私钥文件
Syntax:	    ssl_certificate_key file;
Default:    —
Context:    http, server
  1. ssl_protocols
    ssl支持的协议
Syntax:	    ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2] [TLSv1.3];
Default:    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Context:    http, server
  1. ssl_session_cache
    设置存储会话参数的缓存的类型和大小。
Syntax:	    ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
Default:    ssl_session_cache none;
Context:    http, server

#none: 通知客户端支持ssl session cache,但实际不支持
#builtin[:size]:使用OpenSSL内建缓存,为每worker进程私有
#[shared:name:size]:在各worker之间使用一个共享的缓存
  1. ssl_session_timeout
    客户端连接可以复用ssl session cache中缓存的有效时长,默认5m
Syntax:	    ssl_session_timeout time;
Default:    ssl_session_timeout 5m;
Context:    http, server

示例:

server {
    listen 443 ssl;
    server_name www.chen.com;
    root /vhosts/ssl/htdocs;
    ssl on;
    #添加证书文件
    ssl_certificate /apps/nginx/certs/www.mylinuxops.com.crt;
    #添加私钥文件
    ssl_certificate_key /apps/nginx/certs/www.mylinuxops.com.key;   
    #设置ssl会话的超时时间
    ssl_session_timeout 10m;     
    #设置ssl会话的缓存大小,shared为共享缓存,可以给多个worker进程使用,需要设置缓存名字,还需要设置大小,官方1M可以存储4000个会话   
    ssl_session_cache shared:SSL:20m;        
    location / {
        root /data/www;
        index index.html;
 }
}

重定向

ngx_http_rewrite_module模块
将用户请求的URI基于PCRE regex所描述的模式进行检查,而后完成重定向替换

示例:

http://www.chen.com/hn --> http://www.chen.com/henan
http://www.chen.com --> https://www.chen.com/
  1. if
Syntax:	    if (condition) { ... }
Default:    —
Context:    server, location

/*
condition:
    比较操作符:
    =   相同   
    !=  不同
    ~   模式匹配,区分字符大小写
    ~*  模式匹配,不区分字符大小写
    !~  模式不匹配,区分字符大小写
    !~* 模式不匹配,不区分字符大小写
    
    文件及目录存在性判断:
    -e,!-e     存在与否(包括文件,目录,软链接)
    -f,!-f     文件 
    -d,!-d     目录 
    -x,!-x     执行
*/

注意: if (condition) { ... } 语句中,如果$变量的值为空字符串或是以0开头的任意字符串,则 if 指令认为该条件为false,其它条件为true
示例:

location /test {
    index index.html;
    default_type text/html;
    if ( $scheme = http ){
        return 301 https://www.chen.net/;
    }
    if ( $scheme = https ){
        echo "if ----> $scheme";
    }
#
    if (-f $request_filename) {
        echo "file is exist";
    }
    if (!-f $request_filename) {
        echo "file is not exist";
        return 409;
    }
}
  1. return
    停止处理,并返回给客户端指定的响应码,对 301, 302, 303, 307, 308跳转到URL
Syntax:	    return code [text];  #返回客户端指定的状态码和文本说明
            return code URL;
            return URL;
Default:    —
Context:    server, location, if
  1. rewrite_log
    是否开启重写日志, 发送至error_log(notice level)
Syntax:     rewrite_log on | off;
Default:    rewrite_log off;
Context:    http, server, location, if
  1. set
    用户自定义变量
Syntax:	    set $variable value;
Default:    —
Context:    server, location, if
#注意:变量定义和调用都要以$开头

示例:

location /test {
    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;
        return 500 "service error";
        echo "if-----> $scheme"; #return后面的将不再执行
    }

    if ( $scheme = https ){
        echo "if ----> $scheme";
    }
}
  1. rewrite
    将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI
Syntax:	    rewrite regex replacement [flag];
Default:    —
Context:    server, location, if

注意:
  如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查
  隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制
  如果replacement是以http://https://开头,则替换结果会直接以重向返回给客户端, 即永久重定向301

[flag]: (1) last:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URI启动新一轮重写检查;提前重启新一轮循环,不建议在location中使用
(2) break:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块后的其它配置;结束循环,建议在location中使用
(3) redirect:临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;可使用相对路径,或http://或https://开头,此重定向信息不可缓存,状态码:302
(4) permanent: 重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求,此重定向信息可缓存,状态码301,302,307

  1. break
    匹配成功后不再向下匹配,也不会跳转到其他的location,即直接结束匹配并给客户端返回结果数据
  2. last
    对某个location的URL匹配成功后会停止当前location的后续rewrite规则,并结束当前location,然后将匹配生成的新URL跳转至其他location继续匹配,直到没有location可匹配后将最后一次location的数据返回给客户端

last 和 break 示例:

location /break {
    rewrite ^/break/(.*) /test/$1 break; 
    #break不会跳转到其他的location
    return 666 "break";
}

location /last {
    rewrite ^/last/(.*) /test/$1 last; 
    #last会跳转到其他的location继续匹配新的URI
    return 888 "last";
}
location /test {
    return 999 "test";
    index index.html;
    root /data/nginx;
}

mkdir /data/nginx/test/
echo test Page > /data/nginx/test/index.html

案例1:将 http:// 请求跳转到 https://

location / {
    if ($scheme = http ) {
       rewrite / https://www.chen.net/ redirect;
    }
}

案例2:当用户访问到公司网站的时输入了一个错误的URL,可以将用户重定向至官网首页

location / {
    root /data/nginx/html/pc;
    index index.html;
    if (!-f $request_filename) {
        #return 404 "linux35";
        rewrite (.*) http://www.chen.net/index.html;
    }
}

防盗链技术

ngx_http_referer_module模块
用来阻止Referer首部无有效值的请求访问,可防止盗链

  1. valid_referers
    定义referer首部的合法可用值,不能匹配的将是非法值
Syntax:	    valid_referers none | blocked | server_names | string ...;
Default:    —
Context:    server, location

# none:请求报文首部没有referer首部
# blocked:请求报文有referer首部,但无有效值
# server_names:referer首部中包含本主机名
# arbitrary_string:任意字符串,但可使用*作通配符
# regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头,例如: ~.*\.chen\.com

防止盗链生产案例:

valid_referers none block server_names
*.chen.com *.cheen.com chen.* chenn.* ~\.chen\. ~\.google\. ~\.baidu.com\.;

if ($invalid_referer) {
    return 403 “Forbidden Access”;
}