2、配置Nginx服务器做为负载平衡(使用root用户操作)
2.1、修改配置文件 /etc/nginx/nginx.conf
user nginx;
worker_processes 8; # 工作的子进程数量,默认为1(通常设置为等于CPU数量或者2倍于CPU)
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
# add by guorui
worker_rlimit_nofile 8192; # 在nginx级别上提高打开的文件句柄限制,必须大于 worker_connections 的值
events {
# add by guorui
use epoll; # 使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue
worker_connections 2048; # 每个进程允许的最多连接数,必须小于 worker_rlimit_nofile 的值
# 服务器的最大访问客户数 max clients = worker_processes * worker_connections
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# add by guorui # 添加 upstream 模块,参考PS1
upstream mtserver { # mtserver 是服务器集群名称,后面会用到
server localhost:8081 fail_timeout=5; # 添加多个负载服务器的地址、端口及其他负载参数
server localhost:8080;
}
include /etc/nginx/conf.d/*.conf;
}
2.2、修改配置文件 /etc/nginx/conf.d/default.conf
location / {
proxy_pass http://mtserver;# 添加这一行,引用上面定义的 服务器集群 mtserver
#root /usr/share/nginx/html;# 注释掉
#index index.html index.htm;# 注释掉
}
