nginx负载均衡简单配置

来源:互联网 发布:新路由器有信号没网络 编辑:程序博客网 时间:2024/04/30 07:43
nginx负载均衡简单配置

准备三台虚拟机来做这个实验:

192.168.232.132        web服务器
192.168.232.133        web服务器
192.168.232.134        负载均衡服务器

首先三台电脑预装nginx软件:


1、导入外部软件库
[plain] view plain copy
 print?
  1. rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/epel-release-6-5.noarch.rpm  
  2. rpm -Uvh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/i386/ius-release-1.0-10.ius.el6.noarch.rpm  
  3. rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm  


以下添加注释
[plain] view plain copy
 print?
  1. mirrorlist=http://dmirr.iuscommunity.org/mirrorlist?repo=ius-el6&arch=$basearch  


以下删除注释
[plain] view plain copy
 print?
  1. #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服务器中放入测试文件:
[html] view plain copy
 print?
  1. <html>    
  2. <head>    
  3. <title>Welcome to nginx!</title>    
  4. </head>    
  5. <body bgcolor="white" text="black">    
  6. <center><h1>Welcome to nginx! 192.168.232.132</h1></center>    
  7. </body>    
  8. </html>  



配置负载均衡服务器:

vi /etc/nginx/nginx.conf

内容如下:

[plain] view plain copy
 print?
  1. user  nginx;  
  2. worker_processes  1;  
  3.   
  4. error_log  /var/log/nginx/error.log warn;  
  5. pid        /var/run/nginx.pid;  
  6.   
  7.   
  8. events {  
  9.     worker_connections  1024;  
  10. }  
  11.   
  12.   
  13. http {  
  14.     include       /etc/nginx/mime.types;  
  15.     default_type  application/octet-stream;  
  16.   
  17.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  18.                       '$status $body_bytes_sent "$http_referer" '  
  19.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  20.   
  21.     access_log  /var/log/nginx/access.log  main;  
  22.   
  23.     sendfile        on;  
  24.     #tcp_nopush     on;  
  25.   
  26.     keepalive_timeout  65;  
  27.   
  28.     #gzip  on;  
  29.     upstream test.miaohr.com {  
  30.     server 192.168.232.132:80;  
  31.     server 192.168.232.133:80;  
  32.     }  
  33.       
  34.   
  35.     server {     
  36.         listen       80;     
  37.         server_name  test.miaohr.com;     
  38.         charset utf-8;     
  39.         location / {     
  40.             root   html;     
  41.             index  index.html index.htm;     
  42.             proxy_pass        http://test.miaohr.com;     
  43.             proxy_set_header  X-Real-IP  $remote_addr;     
  44.             client_max_body_size  100m;  
  45.         }     
  46.     
  47.     
  48.         location ~ ^/(WEB-INF)/ {      
  49.         deny all;      
  50.         }      
  51.     
  52.         error_page   500 502 503 504  /50x.html;     
  53.         location = /50x.html {     
  54.             root   /var/www/html/;     
  55.         }     
  56.     }     
  57. }  

原创粉丝点击