nginx负载均衡简单配置
准备三台虚拟机来做这个实验:
192.168.232.132 web服务器 192.168.232.133 web服务器 192.168.232.134 负载均衡服务器
首先三台电脑预装nginx软件:
1、导入外部软件库 rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
以下添加注释 mirrorlist=http://dmirr.iuscommunity.org/mirrorlist?repo=ius-el6&arch=$basearch
以下删除注释 #baseurl=http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/$basearch
2、yum安装nginx
yum install nginx 3、启动nginx
chkconfig nginx on service nginx start
向web服务器中放入测试文件:
Welcome to nginx!配置负载均衡服务器:
vi /etc/nginx/nginx.conf 内容如下:
user nginx; worker_processes 1;
error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { worker_connections 1024; }
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; upstream test.miaohr.com { server 192.168.232.132:80; server 192.168.232.133:80; } server { listen 80; server_name test.miaohr.com; charset utf-8; location / { root html; index index.html index.htm; proxy_pass http://test.miaohr.com; proxy_set_header X-Real-IP $remote_addr; client_max_body_size 100m; } location ~ ^/(WEB-INF)/ { deny all; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html/; } }}
下面浏览器打开:192.168.232.134,如果132、133交替显示则表明试验成功。