http_proxy_module 模块介绍:
proxy_pass 指令属于ngx_http_proxy_module 模块,此模块可以将请求的转发到另一个服务器中,在实际的反向代理中,会通过location区块匹配制定的URL,然后将符合请求的URL通过proxy_pass抛给定义好的upstream节点池。
下面为proxy_pass 使用案例:
1.将匹配URL为name的请求抛给http://127.0.0.1/remote
location /name/ {
proxy_pass http://127.0.0.1/remote
}
2.将匹配URL为some/path的请求抛给http://127.0.0.1
location /some/path/ {
proxy_pass http://127.0.0.1;
}
3.将匹配URL为name的请求应用指定的rewirte 规则 ,然后抛给http://127.0.0.1
location /name/ {
rewrite /name/ ([^/]+) /user?name=$1 break;
proxy_pass http://127.0.0.1 ;
}
nginx负载均衡配置实战
配置基于域名虚拟主机的web节点
192.168.1.23 后端真实服务器
192.168.1.107 后端真实服务器
192.168.1.106 负载均衡服务器
192.168.1.103 客户端
真实服务器23的配置(添加虚拟多个虚拟主机)
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" " $remote_addr"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80 ;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main ;
}
}
后端真实服务器107的配置:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main 'http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" " $remote_addr"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.logs main ;
}
}
负载均衡服务器的配置106
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
upstream proxy {
server 192.168.1.23:80 weight=2;
server 192.168.1.107:80 weight=1 max_fails=2 fail_timeout=20;
}
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.yy.com;
location / {
proxy_pass http://proxy;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
======================================================================
划重点:
1.通过客户端配置hosts解析到负载均衡服务器ip,进行负载均衡,由于配置多个虚拟主机负载均衡,导致:访问www.etiantian.org 会解析到bbs,eitantian,org 因此,我们要在负载均衡服务器上添加个参数: proxy_set_header Host $host
proxy_set_header Host $host :在代理向后端服务器发送的http请求中加入host字段信息,妈的,就是当后端服务器配置多个虚拟主机时,可以识别出代理的是那个虚拟主机,这是配置节点服务器多虚拟主机的关键配置,没鸡儿他怎么区分找的是那台虚拟主机。
2.虽然通过客户端访问制定的域名,可以找到对应的虚拟主机了,但是发现另一个问题,就是真实服务器的访问日志文件,记录的信息:
192.168.1.23 - - [12/Apr/2018:09:56:48 +0800] "GET / HTTP/1.1" 200 20 "-" "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" "-"
并没有记录客户端访问的IP,导致无法显示是那台客户端主机进行的访问。
首先fast:
在负载均衡服务器中配置文件进行更改,添加:
proxy_set_header X-Forwarded-For $remote_addr;
参数介绍:在代理向后端真实服务器发送的http请求中,加入X-forwarded-For 字段信息,用于后端服务器记录客户端真是ip,而不是代理服务器的ip,在反向代理中,节点服务器获取真实ip这是必要的功能。
接下来需要在真实服务器的nginx配置文件中进行更改:
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" " $remote_addr"';
将$http_x_forwarded_for 字段加到log_format main 的参数池中,添加记录客户端真实ip的功能,字段放置位置可以根据业务需求放置。
更改完成nginx -t 检查配置文件,平滑重启 nginx -s reload
真实服务器查看日志: cat logs/access_www.log
192.168.1.103 - - [12/Apr/2018:10:17:53 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" " 192.168.1.106"
192.168.1.103 - - [12/Apr/2018:10:17:54 +0800] "GET / HTTP/1.0" 200 20 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" " 192.168.1.106"
192.168.1.103 - - [12/Apr/2018:10:17:54 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" " 192.168.1.106"
192.168.1.103 - - [12/Apr/2018:10:17:54 +0800] "GET / HTTP/1.0" 200 20 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" " 192.168.1.106"
192.168.1.103 为客户端真实ip
192.168.1.106 为负载均衡服务器ip
include 指定proxy 配置文件,简化nginx配置文件,将一些常用的proxy配置文件,写到指定的配置文件中。
在负载均衡配置文件中:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
upstream proxy {
server 192.168.1.23:80 weight=2;
server 192.168.1.107:80 weight=1 max_fails=2 fail_timeout=20;
}
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.yy.com;
location / {
proxy_pass http://proxy;
include proxy.conf;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
#cat /usr/local/nginx/conf/proxy.conf
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
同指定虚拟主机的include。