以下是用于配置nginx负载平衡的简单示例文件。 主要功能是:
用户访问http://www.linuxidc.com并将其负载均衡到四台服务器:192.168.5.2:80、92.168.1.3:80、192.168.5.4:80、192.168.1.5:80
用户访问http://m.linuxidc.com并将其负载均衡到192.168.5.7服务器的端口8080、8081和8082。
user www www;
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 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include conf/mime.types;
default_type application/octet-stream;
keepalive_timeout 120;
tcp_nodelay on;
upstream www.linuxidc.com {
server 192.168.5.2:80;
server 192.168.5.3:80;
server 192.168.5.4:80;
server 192.168.5.5:80;
}
upstream m.linuxidc.com {
server 192.168.5.7:8080;
server 192.168.5.7:8081;
server 192.168.5.7:8082;
}
server
{
listen 80;
server_name www.linuxidc.com;
location / {
proxy_pass http://www.linuxidc.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_linuxidc_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_linuxidc_com;
}
server
{
listen 80;
server_name m.linuxidc.com;
location / {
proxy_pass http://m.linuxidc.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 linuxidc_linuxidc_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/linuxidc.log blog_linuxidc_com;
}
}
这里使用两个主要模块:
1. HTTP负载平衡模块(HTTP上游),解释了一些字段:
2. HTTP代理模块(HTTP Proxy)
该模块可以将请求转发到其他服务器。