nginx负载均衡初探(一)

nginx [engine x] 是由 Igor Sysoev开发的一个HTTP 服务器和mail 代理服务器软件.虽然刚刚发布两年多, Nginx 因其稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。Nginx 超越Apache的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中有新浪、网易、腾讯等门户网站,六间房、酷6等视频分享网站,Discuz!、水木社区等知名论坛,豆瓣、YUPOO等新兴Web 2.0网站。Nginx 在国内的应用正在不断发展壮大!新近发现Nginx 应用在国内越发火热了,很多网站都开始转向Nginx 了。

     一,首先下载nginx 0.7.52的windows版本,下载地址:http://sysoev.ru/nginx/nginx-0.7.52.zip,并到PHP 官方下载php。
  然后,解压nginx-0.7.52.zip到C盘的根目录,并将目录名改为nginx。执行下列操作
   1, cd nginx          
   2, start nginx 
   这样,nginx就启动了。打开浏览器,输入http://127.0.0.1/ 就可以看到nginx的欢迎页面了。nginx的其他命令 
   nginx -s stop   //停止nginx 
   nginx -s reload //重新加载配置文件 
   nginx -s quit //退出nginx

 

#网站的图片较多,更改较少,将它们在浏览器本地缓存15天
     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
     {
expires      15d;
     }
     #网站会加载很多JS、CSS,将它们在浏览器本地缓存1天
     location ~ .*\.(js|css)?$
     {
expires      1d;
     }
     location /(WEB-INF)/ {
     deny all;
     }
#设定本虚拟主机的访问日志
access_log  /host/nginx/logs/down/access.log  access;  

#日志的路径,每个虚拟机一个,不能相同
server_name_in_redirect  off;

 

实验环境

win7+ubuntu(vmware)

win7 tomcat(ip and por:192.168.0.108:8080)

linux tomcat(ip and por:192.168.110.129:8080)

 

在ubuntu中安装nginx(sudo apt-get install nginx),默认安装在etc/nginx

在两台tomcat的webapps下的ROOT中,为了表示出访问的是那一台tomcat,替换index.html以及index.jsp,其中分别写入linux和windows。

nginx的启动关闭和测试命令如下:

  1. 关闭nginx  
  2. zhengx@zhengx-virtual-machine:~$ sudo pkill -9 nginx  
  3. 测试nginx是否成功配置  
  4. zhengx@zhengx-virtual-machine:~$ sudo nginx -t  
  5. nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored  
  6. nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful  
  7. 开启nginx  
  8. zhengx@zhengx-virtual-machine:~$ sudo nginx   
  9. nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored  
  10. zhengx@zhengx-virtual-machine:~$   

其中nginx.conf的配置文件如下所示

  1. user www-data;  
  2. worker_processes 4;  
  3. pid /var/run/nginx.pid;  
  4.   
  5. events {  
  6.     worker_connections 768;  
  7.     # multi_accept on;  
  8. }  
  9.   
  10. http {  
  11.   
  12.     sendfile on;  
  13.     tcp_nopush on;  
  14.     tcp_nodelay on;  
  15.     keepalive_timeout 65;  
  16.     types_hash_max_size 1024;  
  17.       
  18.   
  19.     include /etc/nginx/mime.types;  
  20.     default_type application/octet-stream;  
  21.   
  22.   
  23.     access_log /var/log/nginx/access.log;  
  24.     error_log /var/log/nginx/error.log;  
  25.   
  26.       
  27.     gzip on;  
  28.     upstream   localhost {    
  29.                 
  30.   
  31.               #win7的tomcat访问url  
  32.               server   192.168.0.108:8080;    
  33.   
  34.               #linux的tomcat访问url  
  35.               server   192.168.110.129:8080;    
  36.         }    
  37.       
  38.     server {    
  39.         listen 80;    
  40.         server_name  localhost;    
  41.         charset utf-8;    
  42.         location / {    
  43.             root   html;    
  44.             index  index.html index.htm;    
  45.             #跳转到win7上的tomcat  
  46.             proxy_pass         http://192.168.0.108:8080;  
  47.             proxy_set_header  X-Real-IP  $remote_addr;    
  48.             client_max_body_size  100m;    
  49.         }    
  50.         
  51.         
  52.         location ~ ^/(WEB-INF)/ {     
  53.             deny all;     
  54.         }     
  55.         
  56.         error_page   500 502 503 504  /50x.html;    
  57.         location = /50x.html {    
  58.             root   html;    
  59.         }    
  60.         
  61.         }    
  62.     include /etc/nginx/server.conf;  
  63.     include /etc/nginx/conf.d/*.conf;  
  64.     include /etc/nginx/sites-enabled/*;  
  65. }  

为了便于维护把nginx.conf拆分一部分代码出来分别是upstream.conf和location.conf

nginx.conf的配置文件如下:

  1. user www-data;  
  2. worker_processes 4;  
  3. pid /var/run/nginx.pid;  
  4.   
  5. events {  
  6.     worker_connections 768;  
  7.     # multi_accept on;  
  8. }  
  9.   
  10. http {  
  11.   
  12.     sendfile on;  
  13.     tcp_nopush on;  
  14.     tcp_nodelay on;  
  15.     keepalive_timeout 65;  
  16.     types_hash_max_size 1024;  
  17.       
  18.   
  19.     include /etc/nginx/mime.types;  
  20.     default_type application/octet-stream;  
  21.   
  22.   
  23.     access_log /var/log/nginx/access.log;  
  24.     error_log /var/log/nginx/error.log;  
  25.   
  26.       
  27.     gzip on;  
  28.     include /etc/nginx/upstream.conf;  
  29.       
  30.     server {    
  31.         listen 80;    
  32.         server_name  localhost;    
  33.         charset utf-8;    
  34.         include /etc/nginx/location.conf;  
  35.         }    
  36.       
  37.     include /etc/nginx/conf.d/*.conf;  
  38.     include /etc/nginx/sites-enabled/*;  
  39. }  

upstream.conf的配置文件如下:

  1. upstream   localhost {    
  2.               server   192.168.0.108:8080;    
  3.               server   192.168.110.129:8080;    
  4. }    

location.conf的配置文件如下:

  1. location / {    
  2.             root   html;    
  3.             index  index.html index.htm;    
  4.             #去找域名为localhost的upstream  
  5.             proxy_pass         http://localhost;  
  6.             proxy_set_header  X-Real-IP  $remote_addr;    
  7.             client_max_body_size  100m;    
  8.         }    
  9.         
  10.         
  11.         location ~ ^/(WEB-INF)/ {     
  12.             deny all;     
  13.         }     
  14.         
  15.         error_page   500 502 503 504  /50x.html;    
  16.         location = /50x.html {    
  17.             root   html;    
  18.         }    
  19.         

猜你喜欢

转载自starbhhc.iteye.com/blog/2030807