www.greatwqs.com 和 blog.greatwqs.com 域名均指向 Nginx 所在的服务器IP。
用户访问http://www.greatwqs.com,将其负载均衡到192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.168.1.5:80四台服务器。
用户访问http://blog.greatwqs.com,将其负载均衡到192.168.1.7服务器的8080、8081、8082端口。
一、以下为配置文件nginx.conf:
user nginx nginx;
worker_processes 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#最大文件描述符
worker_rlimit_nofile 512;
events
{
use epoll;
worker_connections 51200;
}
http
{
include conf/mime.types;
default_type application/octet-stream;
keepalive_timeout 120;
tcp_nodelay on;
# 1.配置以IP为负载
upstream www.greatwqs.com {
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
}
# 2.配置以端口为负载
upstream blog.greatwqs.com {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}
# 1.配置以IP为负载的配置
server
{
listen 80;
server_name www.greatwqs.com;
location / {
proxy_pass http://www.greatwqs.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format www_greatwqs_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data1/logs/www.log www_greatwqs_com;
}
# 2.配置以端口为负载的配置
server
{
listen 80;
server_name blog.greatwqs.com;
location / {
proxy_pass http://blog.greatwqs.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format blog_greatwqs_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data1/logs/blog.log blog_greatwqs_com;
}
}
二、安装前奏
1、创建www用户和组,以及其使用的目录:
三、安装Nginx 0.5.31
1、安装Nginx所需的pcre库:
2、安装Nginx
3、创建Nginx日志目录
5、启动Nginx
附问题释疑:
1、如果均衡的域名中有泛域名,或有几百个域名的话可以配置吗?
server_name .greatwqs.com; 即可支持***.greatwqs.com泛域名
2、ngnix在做均衡的时候是否已经包含了cache功能?
nginx负载均衡只做反向代理,有简单的缓冲,但不像Squid那样将cache存在本机。
3、ngnix---squid----apache取的的HTTP_X_FORWARDED_FOR是squid服务器的ip地址,如果想取真实客户端地址可以做到吗?
在nginx.conf配置文件中增加:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
编译squid时加上--enable-follow-x-forwarded-for
然后在squid.conf中输入一行:
follow_x_forwarded_for allow all
后端的Apache取日志(httpd.conf):
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %h %T" combined
取到的就是用户真实IP