Nginx 简单的负载均衡配置示例
程序员文章站
2022-06-03 10:43:57
用户访问http://www.net-hb.com.cn,将其负载均衡到192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.1...
用户访问http://www.net-hb.com.cn,将其负载均衡到192.168.1.2:80、192.168.1.3:80、192.168.1.4:80、192.168.1.5:80四台服务器。
用户访问//www.jb51.net,将其负载均衡到192.168.1.7服务器的8080、8081、8082端口。
以下为配置文件nginx.conf:
引用
[table=95%][tr][td]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.net-hb.com.cn {
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
}
upstream blog.s135.com {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}
server
{
listen 80;
server_name www.net-hb.com.cn;
location / {
proxy_pass http://www.net-hb.com.cn;
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_s135_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_s135_com;
}
server
{
listen 80;
server_name blog.s135.com;
location / {
proxy_pass //www.jb51.net;
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_s135_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_s135_com;
}
}[/td][/tr][/table] 附:nginx 的安装方法可参照《nginx 0.5.31 + php 5.2.4(fastcgi)搭建可承受3万以上并发连接数,胜过apache 10倍的web服务器》文章的以下段落(仅做负载均衡,无需支持php的安装方法):
二、安装php 5.2.4(fastcgi模式)
4、创建www用户和组,以及其使用的目录:
三、安装nginx 0.5.31
1、安装nginx所需的pcre库:
2、安装nginx
3、创建nginx日志目录
5、启动nginx
用户访问//www.jb51.net,将其负载均衡到192.168.1.7服务器的8080、8081、8082端口。
以下为配置文件nginx.conf:
引用
[table=95%][tr][td]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.net-hb.com.cn {
server 192.168.1.2:80;
server 192.168.1.3:80;
server 192.168.1.4:80;
server 192.168.1.5:80;
}
upstream blog.s135.com {
server 192.168.1.7:8080;
server 192.168.1.7:8081;
server 192.168.1.7:8082;
}
server
{
listen 80;
server_name www.net-hb.com.cn;
location / {
proxy_pass http://www.net-hb.com.cn;
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_s135_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_s135_com;
}
server
{
listen 80;
server_name blog.s135.com;
location / {
proxy_pass //www.jb51.net;
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_s135_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_s135_com;
}
}[/td][/tr][/table] 附:nginx 的安装方法可参照《nginx 0.5.31 + php 5.2.4(fastcgi)搭建可承受3万以上并发连接数,胜过apache 10倍的web服务器》文章的以下段落(仅做负载均衡,无需支持php的安装方法):
二、安装php 5.2.4(fastcgi模式)
4、创建www用户和组,以及其使用的目录:
三、安装nginx 0.5.31
1、安装nginx所需的pcre库:
2、安装nginx
3、创建nginx日志目录
5、启动nginx
上一篇: 新站如何发布外链?发布外链的六个技巧!