目录:
关于Reserve Proxy;
ngx_http_proxy_module模块;
ngx_http_upstream_module模块;
正文:
一、关于Reserve Proxy.
Proxy请求过程分为两个阶段:
首先是请求报文从Client到Proxy的过程,包含Client向Proxy请求数据报文时的HTTP请求首部,nginx proxy接收到请求报文后,会将报文进行拆除,以了解报文内部请求的具体内容;并且去匹配本机的location;一旦匹配到,发现该报文请求内容是要发往后端服务器去请求的,此时proxy_server需要自己重新构建请求报文送往后端服务器。
二、ngx_http_proxy_module模块
作用: The ngx_http_proxy_module
module allows passing requests to another server.
格式:
1
2
3
|
location
/uri
{
proxy_pass http:
//back_server
:port
/newuri
;
}
|
/uri --> /newuri //上面示例中/uri对应的就是/newuri.
如果此处location后跟的/uri是通过模式匹配定义的,该种情形,实际请求的url路径为 http://back_server:port/newuri/uri
如果location中使用了url重定向(rewrite)时,nignx将使用重定向之后的uri进行处理,而不是重定向之前的.
实验一. 通过直接匹配与正则匹配的方式设置反向代理.
实验环境:
系统版本 |
用途 |
主机名 |
IP |
CentOS7.3 |
Nginx_Proxy |
nginx |
10.68.7.222 |
CentOS6.8 |
node_Server |
node1 |
10.68.7.200 |
CentOS6.8 |
node_Server |
node2 |
10.68.7.201 |
Windows |
test |
Windows |
10.68.7.167 |
首先确认后端node服务器访问正常:
1
2
3
4
5
|
[root@nginx conf]
# curl 10.68.7.201
<h1> node1 <
/h1
>
[root@nginx conf]
# curl 10.68.7.202
<h1> node2 <
/h1
>
[root@nginx conf]
#
|
然后确保nginx_proxy代理服务器正常可用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@nginx conf]
# vim server1.conf
server {
listen 8080;
server_name yangbin.com;
charset utf-8;
access_log
/data/nginx/logs/access
.log;
location / {
root
/web/html/www
;
index www.html index.html index.htm;
}
location
/images/
{
root
/web/html/
;
index qq.jpg;
}
}
|
然后开始配置proxy_pass:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@nginx conf]
# vim server1.conf
server {
listen 8080;
server_name yangbin.com;
charset utf-8;
access_log
/data/nginx/logs/access
.log;
location / {
proxy_pass http:
//10
.68.7.201/
root
/web/html/www
;
index www.html index.html index.htm;
}
location
/images/
{
proxy_pass http:
//10
.68.7.202/
root
/web/html/
;
index qq.jpg;
}
}
[root@nginx conf]
# ../sbin/nginx -s reload
|
浏览器访问测试:
从访问的结果得出这样的结论:访问代理服务器ip,将是proxy_pass模块进行响应!
此时查看node服务器的web访问日志,可以发现来源IP是nginx代理服务器的ip.
1
2
3
|
[root@node1 ~]
# tail -1 /etc/httpd/logs/access_log
10.68.7.222 - - [16
/Jan/2017
:23:55:04 +0800]
"GET / HTTP/1.0"
200 17
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
[root@node1 ~]
#
|
1
2
3
|
[root@node2 ~]
# tail -1 /etc/httpd/logs/access_log
10.68.7.222 - - [17
/Jan/2017
:00:03:33 +0800]
"GET / HTTP/1.0"
200 17
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
[root@node2 ~]
#
|
关于location匹配的其他情况:
第一种情况:
1
2
3
4
5
|
location
/images
{
proxy_pass http:
//10
.68.7.202
/images
;
root
/web/html/
;
index qq.jpg;
}
|
注:image/qq.jpg是node2服务器站点存放网页的根目录下的子目录,该子目录中包含qq.jpg图片.
修改node2 httpd服务配置文件:
1
2
3
4
|
[root@node2 html]
# vim /etc/httpd/conf/httpd.conf
...
402 DirectoryIndex qq.jpg index.html index.html.var
...
|
浏览器访问:
查看node2服务器的access日志文件:
1
2
3
|
[root@node2 html]
# tail -1 /etc/httpd/logs/access_log
10.68.7.222 - - [17
/Jan/2017
:00:29:43 +0800]
"GET /images/ HTTP/1.0"
200 8863
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
[root@node2 html]
#
|
第二种情况:
1
2
3
4
|
location
/frome/
{
proxy_pass http:
//10
.68.7.202
/bbs/
;
...
}
//
此时访问yangbin.com:8080
/frome
时,返回的是
/var/www/html/bbs/index
.html中的内容,因为
/frome
映射到了
/bbs
。
|
第三种情况:
1
2
3
4
|
location
/js/
{
proxy_pass
...
}
//
此时访问yangbin.com
/js
时,返回的是
/var/www/html/js/index
.html的内容.
|
查看node2服务器/js目录下的网页文件内容:
1
2
|
[root@node2 html]
# vim js/index.html
<h1>
/var/www/html/js/index
.html <
/h1
>
|
浏览器访问:
第四种情况:
1
2
3
|
location ~* \.(jpg|png|gif)$ {
//
~*表示不区分大小写; 后面表示以.jpg, .png, .gif结尾的uri.
proxy_pass http:
//10
.68.7.202;
}
//
该种情况表示所有以.jpg, .png, .gif结尾的uri请求,都会代理至10.68.7.202.
|
如果采用的正则表达式匹配,则location部分会直接补到待访问的IP地址后边,如果ip地址后边有其他路径,也会将该路径替换.
第五种情况:
1
2
3
|
location ~* \.(jpg|jpeg|png)$ {
proxy_pass http:
//10
.68.7.202;
}
//
此处如果是 http:
//10
.68.7.202/, 即后面加个斜线是错误的,因为对于模式匹配的proxy不允许有url路径存在.
|
此情况下,在node1服务器下的/var/www/html/bbs/路径下也应该有.jpg结尾的文件才行
浏览器访问http://yangbin.com:8080/images/images.png,也会进行映射,实际的访问路径为http://10.68.7.202/images/images.png.
浏览器验证过程略.
注:关于以上各种情况带来的访问ip的问题:
实验一的情况,后端的node服务器上access_logs记录的Client_ip是nginx反向代理服务的ip;而真实环境中,很多时候为了分析用户行为,需要获取访问用户的真实ip,因此以上情况的配置就有问题,因为它将访问用户的ip统统转化为了代理服务器的ip.该问题的解决办法,具体配置详见"实验2".
实验2:向客户端发送特定首部,记录客户端的ip.
官网文档:
http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
首先配置代理服务器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
location / {
proxy_pass http:
//10
.68.7.201/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
root
/web/html/www
;
index www.html index.html index.htm;
}
location ~* \.(jpg|png|jpeg|gif)$ {
proxy_pass http:
//10
.68.7.202;
proxy_set_header X-Real-IP $remote_addr;
root
/web/html/
;
index qq.jpg;
}
|
然后修改node服务器配置文件的log格式:
1
2
3
4
5
6
7
8
9
|
[root@node1 ~]
# vim /etc/httpd/conf/httpd.conf
...
497
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
498 LogFormat
"%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
combined
////
该行开头
"%h"
改为
"%{X-Real-IP}i"
.
499 LogFormat
"%h %l %u %t \"%r\" %>s %b"
common
500 LogFormat
"%{Referer}i -> %U"
referer
501 LogFormat
"%{User-agent}i"
agent
...
[root@node1 ~]
# service httpd restart
|
1
2
3
4
5
6
7
8
9
|
[root@node2 ~]
# vim /etc/httpd/conf/httpd.conf
...
497
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
498 LogFormat
"%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
combined
499 LogFormat
"%h %l %u %t \"%r\" %>s %b"
common
500 LogFormat
"%{Referer}i -> %U"
referer
501 LogFormat
"%{User-agent}i"
agent
...
[root@node2 ~]
# service httpd restart
|
分别在浏览器访问两个location,然后到node服务器端查看访问日志:
1
2
3
4
5
|
[root@node1 ~]
# tail -3 /etc/httpd/logs/access_log
10.68.7.167 - - [17
/Jan/2017
:01:52:09 +0800]
"GET / HTTP/1.0"
304 -
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
10.68.7.167 - - [17
/Jan/2017
:01:52:10 +0800]
"GET / HTTP/1.0"
304 -
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
10.68.7.167 - - [17
/Jan/2017
:01:52:10 +0800]
"GET / HTTP/1.0"
304 -
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
[root@node1 ~]
#
|
1
2
3
4
5
|
[root@node2 ~]
# tail -3 /etc/httpd/logs/access_log
10.68.7.167 - - [17
/Jan/2017
:01:52:08 +0800]
"GET /images/images.png HTTP/1.0"
304 -
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
10.68.7.167 - - [17
/Jan/2017
:01:52:08 +0800]
"GET /images/images.png HTTP/1.0"
304 -
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
10.68.7.167 - - [17
/Jan/2017
:01:52:08 +0800]
"GET /images/images.png HTTP/1.0"
304 -
"-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"
[root@node2 ~]
#
|
10.68.7.167为访问客户端的ip.
实验3:nginx的代理缓存(proxy_cache)功能,加速用户访问站点的速度;
官网文档:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache
此处可用压测命令ab来测试缓存前后的效果.
ab命令的使用方法: http://024mj.blog.51cto.com/10252785/1684794
语法:
1
2
|
Syntax: proxy_cache_path PATH [levels=levels] [use_temp_path=on|off]
e.g: proxy_cache_path
/data/nginx/cache
levels=1:2 keys_zone=one:10m;
|
/data/nginx/cache: 表示缓存存储的具体路径;
levels=1:2 表示缓存的级别,1:2表示共有两级缓存目录,第一级缓存目录共有1个字符表示,第二级缓存目录共有2个字符表示,如果levels=1:2:3,表示共有3三级缓存目录,第三级缓存目录用三个字符表示;
keys_zone=one:10m表示在内存中找10兆大小的位置,取名为one来存储键值;
结果示例:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
目录c:表示为一级缓存目录,共一个字符;
目录29:为二级缓存目录,共两个字符;后面的一长串字符为缓存的具体文件;
其他proxy_cache模块配置语法说明:
1
2
3
|
Syntax: proxy_cache_methods GET | HEAD | POST ...;
Default: proxy_cache_methods GET HEAD;
Context: http, server, location
|
作用:定义客户端只有使用GET,HEAD或POST时才会缓存;一般都是使用GET或HEAD时才会缓存;
POST: 提交.
PUT:上传.
1
2
3
|
Syntax: proxy_cache_min_uses number;
Default: proxy_cache_min_uses 1;
Context: http, server, location
|
作用:Sets the number of requests after which the response will be cached.
即设定用户请求的内容被响应多少次之后才会被缓存.
1
2
3
|
Syntax: proxy_cache_purge string ...;
Context: http, server, location
This directive appeared
in
version 1.5.7.
|
作用:purge表示修剪,净化,清除; 即缓存管理;
使用场景:
1)缓存都有有效期限;在有效期限到来之前,缓存会一直有效;而在这期间,当后端服务器内容发生改变,用户请求到仍然是旧的资源;
2)使用缓存修剪,网站管理人员可以限定自动将请求的缓存从本地缓存中删除,这样下次请求时将从后端服务器请求新的内容并缓存到本地,保证缓存与后端服务器资源的同步一致;
3)使用时注意权限问题,不能随便授权;
1
2
3
4
|
Syntax: proxy_cache_revalidate on | off;
Default: proxy_cache_revalidate off;
Context: http, server, location
This directive appeared
in
version 1.5.7.
|
作用:重新有效期验证;即过期后重新校验,就是当缓存过期后,它自动向后端服务器询问资源是否有改动;如果没有改动,就延长本地缓存过期时间继续使用,可以避免占据后端网络带宽;
1
2
3
|
Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;
Default: proxy_cache_use_stale off;
Context: http, server, location
|
作用:当用户访问某个资源,正好代理服务器缓存已过期,然后代理服务器向后端请求资源,正好后端服务器宕机了;此时代理服务器只能向客户端返回请求的资源不存在;
而使用此选项,可以在这种情况下自定义给客户端返回的错误信息,如timeout,updating,500,404等委婉信息;
1
2
|
Syntax: proxy_cache_valid [code ...]
time
;
Context: http, server, location
|
作用:Sets caching time for different response codes. For example, the following directives:
例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
按照响应码来进行缓存;自定义缓存时长,而不用默认的;
三、ngx_http_upstream_module模块;
语法:
1
2
3
|
Syntax: upstream name { ... }
Default: —
Context: http
|
配置:
1
2
3
4
5
6
7
8
9
10
|
[root@nginx conf]
# vim nginx.conf
http {
...
upstream user-defined {
server 10.68.7.201;
server 10.68.7.202;
}
...
include server1.conf;
}
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@nginx conf]
# vim server1.conf
...
location / {
proxy_pass http:
//user-defined/
;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
root
/web/html/www
;
index www.html index.html index.htm;
}
...
|
浏览器访问即会出现轮询的情况;
注:
upstream和proxy_pass的区别:proxy_pass后面只能定义一个ip站点;upstream则可以把多个ip站点做成一个组(只能defined在http中),放在proxy_pass模块后面以特定的方式来调用;
还要注意:
The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives.
upstream模块默认是加权轮询的,只不过权重都等于1.
其他upstream模块选项:
1
2
3
|
Syntax: server address [parameters];
Default: —
Context: upstream
|
The following parameters can be defined:
1) weight=number
2) max_conns=number
3) fail_timeout=time
4) max_conns=number
5) max_fails=number //用来做健康状态检查,尝试多少次连接后还连接不上,就将其标记为失效,并将其移除;
6) fail_timeout=time //定义超时时间;
7) backup //标记为备用服务器,当另一台服务器可用时,被标记的这台服务器不会提供服务,只有当另一外服务器不可用时,被标记的这台服务器才会主动提供服务。一般把proxy服务器自己当做备用服务器;
8)down //标记为永久下线,使用场景:后台服务器需要升级且不支持平滑升级的情况下;
例如:
1
2
3
|
upstream user-defined {
server 10.68.7.200 weight=3;
server 10.68.7.201;
|
//这样定义完的效果是7.200站点的资源刷新三下后才会轮询到7.201上面;
1
2
3
|
Syntax: ip_hash;
Default: —
Context: upstream
|
作用:算法指令,指客户端访问到哪个站点后,刷新不会跳转到其他站点,指session绑定,原地址绑定;
ip_hash可能有损负载均衡效果;
e.g:
1
2
3
4
5
|
upstream user-defined {
ip_hash;
server 10.68.7.201;
server 10.68.7.202;
}
|
1
2
3
4
|
upstream user-defined {
server 10.68.7.201 max_fails=2 fail_timeout=2;
server 10.68.7.202 max_fails=2 fail_timeout=2;
}
|
Nginx的基本配置已完成,其实以上总结的凤毛菱角,nginx强大功能背后所涉及的配置复杂程度远非于此,而官网文档,非常有参考及学习价值,值得经常查阅!
附:
Nginx rpm包下载地址:https://pkgs.org/download/nginx
Tengine官网学习地址:http://tengine.taobao.org/
--- 第三部分完成!
本文转自 羽丰1995 51CTO博客,原文链接:http://blog.51cto.com/13683137989/1892711