七、ngx_http_rewrite_module
ngx_http_rewrite_module模块:
将用户请求的URI基于PCRE regex所描述的模式进行检查,而后完成重定向替换
1、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://开头,状态码:302permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求,状态码:301
2、return
return code [text];return code URL;return URL;停止处理,并返回给客户端指定的响应码实例:当访问请求包含admin跳转到百度location / {if ( $uri ~* admin ){return 301 http://www.baidu.com/;}}
3、rewrite_log on | off;
是否开启重写日志, 发送至error_log(notice level)
4、set $variable value;
用户自定义变量注意:变量定义和调用都要以$开头
5、if (condition) { ... }
条件满足时,执行配置块中的配置指令;server, locationcondition:比较操作符:= 相同 != 不同~:模式匹配,区分字符大小写~*:模式匹配,不区分字符大小写!~:模式不匹配,区分字符大小写!~*:模式不匹配,不区分字符大小写文件及目录存在性判断:-e, !-e 存在(包括文件,目录,软链接)-f, !-f 文件 -d, !-d 目录 -x, !-x 执行
举例:当用户访问bbs跳转到forum目录下
vim /etc/nginx/conf.d/vhosts.conf
location /bbs {
rewrite ^/bbs/(.*)$ /forum/$1 last;
}
重启测试
备注:前提forum目录下有文件,last内容跳转,地址不跳转
举例:http跳转到https
方法一:
vim /etc/nginx/conf.d/vhosts.conf
server
。。。
location / {
rewrite / https://www.liuxiaosi.com.cn/ redirect;
}
server {
root /app/website1;
listen 443 ssl;
ssl_certificate /etc/nginx/conf.d/a.com.crt;
ssl_certificate_key /etc/nginx/conf.d/a.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
}
方法二:
vim /etc/nginx/conf.d/vhosts.conf
server {
listen 80 default_server;
root /app/website1;
server_name www.a.com;
access_log /var/log/nginx/a.com.access.log compression;
listen 443 ssl;
ssl_certificate /etc/nginx/conf.d/a.com.crt;
ssl_certificate_key /etc/nginx/conf.d/a.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
location / {
if ( $scheme = http ) {
rewrite / https://www.a.com/ redirect;
}
}
}
八、ngx_http_referer_module
ngx_http_referer_module模块:
用来阻止Referer首部无有效值的请求访问,可防止盗链
1、valid_referers none|blocked|server_names|string ...;
定义referer首部的合法可用值,不能匹配的将是非法值none:请求报文首部没有referer首部blocked:请求报文有referer首部,但无有效值server_names:参数,其可以有值作为主机名或主机名模式arbitrary_string:任意字符串,但可使用*作通配符regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头
举例:防盗链
vim /etc/nginx/conf.d/vhosts.conf
location / {
valid_referers none block server_names *.a.com ~\.baidu\. ~\.goole\. ;
if ($invalid_referer) {
return 403 http://www.baidu.com;
}
}
九、ngx_http_proxy_module
ngx_http_proxy_module模块:
转发请求至另一台主机
1、proxy_pass URL;
Context:location, if in location, limit_except注意:proxy_pass后面路径不带uri时,会将location的uri传递(附加)给后端主机proxy_pass后面的路径是一个uri时,其会将location的uri替换为proxy_pass的uri
2、proxy_set_header field value;
设定发往后端主机的请求报文的请求首部的值Context: http, server, locationproxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;请求报文的标准格式如下:X-Forwarded-For: client1, proxy1, proxy2
3、proxy_cache_path;
定义可用于proxy功能的缓存;Context:http
4、proxy_cache zone | off; 默认off
指明调用的缓存,或关闭缓存机制;Context:http, server, location
5、proxy_cache_key string;
缓存中用于“键”的内容
6、proxy_cache_valid [code ...] time;
定义对特定响应码的响应内容的缓存时长定义在http{...}中
7、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 ...在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端
8、proxy_cache_methods GET | HEAD | POST ...;
对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存
9、proxy_hide_header field;
默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad,X-Accel-等,用于隐藏后端服务器特定的响应首部
10、proxy_connect_timeout time;
定义与后端服务器建立连接的超时时长,如超时会出现502错误,默认为60s,一般不建议超出75s
11、proxy_send_timeout time;
将请求发送给后端服务器的超时时长;默认为60s
12、proxy_read_timeout time;
等待后端服务器发送响应报文的超时时长,默认为60s
举例:充当简单的调度器配置(反向代理)
1、vim /etc/nginx/conf.d/vhosts.conf
location / {
proxy_pass http://192.168.43.67/ ;
}
注意:如果访问www.a.com/bbs跳转到192.168.43.67,上面写法地址后面一定要加“/”,不加意思就是,跳转到192.168.43.67/bbs
实例:
如果用户访问的是html我调度到192.168.43.67上,如何访问的是txt调度到192.168.43.57上
2、vim /etc/nginx/conf.d/vhosts.conf
location ~ \.html$ {
proxy_pass http://192.168.43.67 ;
}
location ~ \.txt$ {
proxy_pass http://192.168.43.57 ;
}
举例:静态分离
location ~ \.(html|txt|css|jpg)$ {
proxy_pass http://192.168.43.67 ;
}
location ~ \.php$ {
proxy_pass http://192.168.43.57 ;
}
备注:这样后端服务器得不到用户地址的,它显示的是调度器的地址,下面解决这个问题
1)编辑调度器配置文件
vim /etc/nginx/conf.d/vhosts.conf
proxy_set_header client-ip $remote_addr;
2)编辑web服务器配置文件
vim /etc/httpd/conf/httpd.conf
定义
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{client-ip}i\"" combined
调用
CustomLog "logs/access_log" testlog
重启测试
备注:多增代理用proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
服务器端日志格式改为
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\"" combined
实例:启动代理缓存
1)定义缓存信息(只能在http里)
vim /etc/nginx/nginx.conf
proxy_cache_path /var/cache/nginx/proxy_cache
levels=1:1:1 keys_zone=proxycache:20m
inactive=120s max_size=1g;
2)把上面定义的目录创建出来
mkdir /var/cache/nginx
3)引用缓存,修改虚拟主机配置文件
vim /etc/nginx/conf.d/vhosts.conf
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
4)生效测试
十、ngx_http_headers_module
ngx_http_headers_module模块
向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值
1、add_header name value [always];
添加自定义首部add_header X-Via $server_addr;add_header X-Cache $upstream_cache_status;add_header X-Accel $server_name;
2、add_trailer name value [always];
添加自定义响应信息的尾部