使用ngx_http_ssl_module模块实现 https
为指定虚拟机启用HTTPS protocol, 建议用listen指令代替
ssl_certificate file;
当前虚拟主机使用PEM格式的证书文件
ssl_certificate_key file;
当前虚拟主机上与其证书匹配的私钥文件
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]; 支持ssl协议版本,默认为后三个
ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
none: 通知客户端支持ssl session cache,但实际不支持
builtin[:size]:使用OpenSSL内建缓存,为每worker进程私有
[shared:name:size]:在各worker之间使用一个共享的缓存
ssl_session_timeout time;
客户端连接可以复用ssl session cache中缓存的ssl参数的有效时长,默认5m
[root@CentOS74 ~]# tree /etc/nginx/conf.d/
/etc/nginx/conf.d/
├── certs
│ ├── centos.crt #使用的证书文件与密钥
│ ├── centos.key
│ ├── linux.crt
│ └── linux.key
└── vhost
├── centos.com.conf #两个虚拟主机的配置文件
└── linux.com.conf
2 directories, 6 files
[root@CentOS74 ~]# cat /etc/nginx/conf.d/vhost/{centos,linux}.com.conf
server{
listen 443 ssl;
server_name www.centos.com;
root /data/vhost/centos;
ssl on; #启用ssl加密
ssl_certificate /etc/nginx/conf.d/certs/centos.crt; #指定证书的存放路径
ssl_certificate_key /etc/nginx/conf.d/certs/centos.key; #指定密钥文件的存放路径
ssl_session_cache shared:sslcache:20m; #启用会话缓存,并多进程共享
ssl_session_timeout 10m; #会话缓存时间
}
server{
listen 443 ssl;
server_name www.linux.com;
root /data/vhost/linux;
ssl on;
ssl_certificate /etc/nginx/conf.d/certs/linux.crt;
ssl_certificate_key /etc/nginx/conf.d/certs/linux.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
}
[root@CentOS69 ~]# curl -k https://www.linux.com
linux
[root@CentOS69 ~]# curl -k https://www.centos.com
centos
[root@CentOS69 ~]# curl -k https://www.centos.com/test/
test
Rewrite 重写
将用户请求的URI基于PCRE regex所描述的模式进行检查,而后完成重定向替换
rewrite regex replacement [flag]
将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI
注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制
如果replacement是以http://或https://开头,则替换结果会直接以重向返回给客户端, 即永久重定向301
[flag]:
last:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URI启动新一轮重写检查;提前重启新一轮循环, 不建议在location中使用break:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location中使用
redirect:临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求,状态码:301
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location /redhat {
rewrite ^/redhat/(.*)$ /centos/$1;
rewrite ^/centos/6/(.*)$ /centos/6.9/$1;
rewrite ^/centos/7/(.*)$ /centos/7.5/$1;
rewrite ^/centos/7.4/(.*)$ /centos/7.5/$1;
}
}
[root@CentOS69 ~]# curl http://www.linux.com/redhat/
centos
[root@CentOS69 ~]# curl http://www.linux.com/redhat/6/
centos6.9
return:停止处理,并返回给客户端指定的响应码
return code [text];
return code URL;
if (condition) { ... }
条件满足时,执行配置块中的配置指令;server, location
比较操作符:= 相同 != 不同
~:模式匹配,区分字符大小写
~*:模式匹配,不区分字符大小写
!~:模式不匹配,区分字符大小写
!~*:模式不匹配,不区分字符大小写
文件及目录存在性判断:
-e, !-e 存在(包括文件,目录,软链接)
-f, !-f 文件 -d, !-d 目录 -x, !-x 执行
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
listen 443 ssl;
server_name www.linux.com;
root /data/vhost/linux;
ssl_certificate /etc/nginx/conf.d/certs/linux.crt;
ssl_certificate_key /etc/nginx/conf.d/certs/linux.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
location / {
if ( $scheme = http ) {
rewrite /(.*) https://www.linux.com/$1 break;
}
}
location /redhat {
rewrite ^/redhat/(.*)$ /centos/$1;
rewrite ^/centos/6/(.*)$ /centos/6.9/$1;
rewrite ^/centos/7/(.*)$ /centos/7.5/$1;
rewrite ^/centos/7.4/(.*)$ /centos/7.5/$1;
}
}
[root@CentOS69 ~]# curl -kL www.linux.com
linux
[root@CentOS69 ~]# curl -kL www.linux.com/redhat/
centos
valid_referers none|blocked|server_names|string ...;
定义referer首部的合法可用值,不能匹配的将是非法值
none:请求报文首部没有referer首部blocked:请求报文有referer首部,但无有效值
server_names:参数,其可以有值作为主机名或主机名模式
arbitrary_string:任意字符串,但可使用*作通配符
regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头
反向代理
proxy_pass URL;
Context:location, if in location, limit_except
代理的同时重写(存疑)
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location / {
proxy_pass http://192.168.30.174/;
}
location /test {
rewrite ^/test/(.*)$ /redhat/$1 redirect;
}
}
[root@CentOS69 ~]# curl -L www.linux.com
192.168.30.174
[root@CentOS69 ~]# curl -L www.linux.com/test
174redhat
注意:主机后是否有"/"决定着代理的结果
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location /redhat {
proxy_pass http://192.168.30.174; #URL后没有/
}
}
[root@CentOS69 ~]# curl www.linux.com/redhat/ #代理结果是在主机后添加请求的uri
174redhat
[root@CentOS69 ~]# curl www.linux.com/redhat/web1/
web1
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location /redhat {
proxy_pass http://192.168.30.174/; #URL后有/
}
}
[root@CentOS69 ~]# curl www.linux.com/redhat #redhat呗替换为/
192.168.30.174
[root@CentOS69 ~]# curl www.linux.com/redhat/redhat/web1/ #uir等同于/redhat/web1/
web1
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location / {
proxy_pass http://192.168.30.174/redhat/;
}
}
[root@CentOS69 ~]# curl www.linux.com/web1/indasd #观察错误页面提示
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /redhat/web1/indasd was not found on this server.</p> #在redhat后加web1,是因为redhat将/替换了
</body></html>
proxy_set_header field value;
设定发往后端主机的请求报文的请求首部的值
Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;累加报文头部,在每台需要记录代理服务器上配置
请求报文的标准格式如下:
X-Forwarded-For: client1, proxy1, proxy2
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location / {
proxy_pass http://192.168.30.174;
proxy_set_header X_Real_IP $remote_addr; #在反向代理服务器向后端服务器的请求报文中添加报文头部
}
}
[root@CentOS174 ~]# cat /etc/httpd/conf/httpd.conf | grep LogFormat #修改日志记录内容格式
LogFormat "%{X_Real_IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
日志文件中源地址IP记录可以修改为真正的客户端地址,而不是反向代理服务器地址。同样也可修改为需要的字符串
192.168.30.74 - - [14/Jul/2018:04:09:46 +0800] "GET / HTTP/1.0" 200 15 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
192.168.30.69 - - [14/Jul/2018:04:10:47 +0800] "GET / HTTP/1.0" 200 15 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
proxy_cache_path;
定义可用于proxy功能的缓存;Context:http
proxy_cache_path path [选项]...
选项:
[levels=levels]:目录分级
[use_temp_path=on|off]:是否启用临时路径
keys_zone=name:size:缓存名称与大小限制(内存)
[inactive=time]:缓存时间
[max_size=size]:最大占用空间(磁盘)
proxycache 指内存中缓存的大小,主要用于存放key和metadata(如:使用次数)
max_size 指磁盘存入文件内容的缓存空间最大值
proxy_cache zone | off; 默认off
指明调用的缓存,或关闭缓存机制;Context:http, server, location
proxy_cache_key string;缓存中用于“键”的内容
默认值:proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid [code ...] time;定义对特定响应码的响应内容的缓存时长
在主配置文件夹中定义缓存路径和相关属性
[root@CentOS74 vhost]# cat /etc/nginx/nginx.conf | grep proxy_cache
proxy_cache_path /var/cache/nginx levels=1:2:1 keys_zone=testcache:20m inactive=120s max_size=1g;
在子配置文件中声明使用缓存方式
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location / {
proxy_pass http://192.168.30.174;
proxy_cache testcache; #使用指定空间的缓存
proxy_cache_key $request_uri; #缓存内存中键记录的内容
proxy_cache_valid 200 301 302 1h; #缓存内容和时间
proxy_cache_valid any 1m;
}
}
访问后端服务器站点后查看缓存文件
[root@CentOS74 vhost]# tree /var/cache/nginx/
/var/cache/nginx/
├── 3
│ └── ac
│ └── 2
│ └── 8568ba40bf40a2f0c1026178eeb32ac3
└── 9
└── 7d
└── c
└── 6666cd76f96956469e7be39d750cc7d9
6 directories, 2 files
proxy_cache_use_stale;
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...
在被代理的后端服务器出现哪种情况下,可以真接使用过期的缓存响应客户端
proxy_cache_methods GET | HEAD | POST ...;对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存
proxy_hide_header field;
默认nginx在响应报文不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel-等,用于隐藏后端服务器特定的响应首部
proxy_connect_timeout time;定义与后端服务器建立连接的超时时长,如超时会出现502错误,默认为60s,一般不建议超出75s
proxy_send_timeout time;将请求发送给后端服务器的超时时长;默认为60s
proxy_read_timeout time;等待后端服务器发送响应报文的超时时长,默认为60s
add_header name value [always];
添加自定义响应报文首部
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;
[root@CentOS74 vhost]# cat linux.com.conf
server{
listen 80;
server_name www.linux.com;
root /data/vhost/linux;
location / {
proxy_pass http://192.168.30.174;
proxy_set_header X_Real_IP "jiangbowen";
proxy_cache testcache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 301 302 1h;
proxy_cache_valid any 1m;
add_header X-Cache $upstream_cache_status; #自定义头部,显示是否命中缓存
}
}
[root@CentOS69 ~]# curl -I -s www.linux.com/test/redhat/ | grep X-Cache
X-Cache: MISS
[root@CentOS69 ~]# curl -I -s www.linux.com/test/redhat/ | grep X-Cache
X-Cache: HIT
add_trailer name value [always];
添加自定义响应信息的尾部尾部