1、proxy_set_header Host $host;
作用:当后端单台web服务器上也配置有多个虚拟主机时,需要使用该Header来区分反向代理哪个主机名。
实验测试:
A、未加入proxy_set_header Host $host;
①、前端用作方向代理的Nginx服务器nginx.conf的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream 91rhca_real_servers {
server 10.0.0.23;
#server 10.0.0.24;
}
server {
listen 80;
server_name blog.91rhca.com bbs.91rhca.com;
location / {
proxy_pass http://91rhca_real_servers;
}
}
}
|
②、后端用作web服务器的Nginx服务器的nginx.conf的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.91rhca.com;
root html/blog;
location /blog/ {
index index.html;
}
}
server {
listen 80;
server_name bbs.91rhca.com;
root html/bbs;
location /bbs/ {
index index.html;
}
}
}
|
③、测试结果
|
[root@91rhca.com ]# curl http://blog.91rhca.com
blog.91rhca.com
10.0.0.23
[root@91rhca.com ]# curl http://bbs.91rhca.com
blog.91rhca.com
10.0.0.23
|
B、加入proxy_set_header Host $host;
①、加入位置为前端用作方向代理的Nginx服务器:
|
server {
listen 80;
server_name blog.91rhca.com bbs.91rhca.com;
location / {
proxy_set_header Host $host;
proxy_pass http://91rhca_real_servers;
}
}
|
其他的不变
②、测试结果
|
[root@91rhca.com ]# curl http://blog.91rhca.com
blog.91rhca.com
10.0.0.23
[root@91rhca.com ]# curl http://bbs.91rhca.com
bbs.91rhca.com
10.0.0.23
|
2、proxy_set_header X-Forwarded-For $remote_addr;
作用:如果后端Web服务器上的程序需要获取用户IP,从该Header头中获取
(默认Nginx在前端做反向代理时,如果不添加改参数,用户的访问IP是记录在前端的Nginx反向代理服务器的日志中,而后端的服务器只能记录前端nginx服务器的IP访问情况,不利于以后的日志访问分析)
注:要想在后端对应的服务器上记录用户的访问IP,需要对后端的日志格式进行调整以便进行配合
A、Apache的日志格式设置:
|
LogFormat “\”%{X-Forwarded-For}i\” %l %u %t \”%r\” %>s %b” common
|
B、Nginx的日志格式设置:
|
log_format commonlog '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" "$http_x_forwarded_for"';
|
Nginx设置的前后日志对比:
10.0.0.21 – – [31/Aug/2014:22:58:26 +0800] “GET / HTTP/1.0″200 26 “-””check_http/v1.4.16 (nagios-plugins 1.4.16)” “-”
10.0.0.21 – – [31/Aug/2014:23:01:43 +0800] “GET / HTTP/1.0″304 0 “-””Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)” “10.0.0.1″
(责任编辑:IT) |