现在的位置: 首页 > 综合 > 正文

安装 Nginx 配置负载均衡

2013年10月03日 ⁄ 综合 ⁄ 共 3894字 ⁄ 字号 评论关闭

安装 Nginx 配置负载均衡

据说 Nginx 做负载均衡不错,恩拿来学习配置下。

先安装:

  1. wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gz  
  2. tar zxvf nginx-0.6.35.tar.gz  
  3. cd nginx-0.6.35  
  4. ./configure  
  5. make  
  6. make install  

安装时出现下面的错误:

Configuration summary
+ PCRE library is not found
+ OpenSSL library is not used
+ md5 library is not used
+ sha1 library is not used
+ using system zlib library

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.

说没有 PCRE 库,那安装它:

  1. yum install pcre-devel  

安装完后,修改配置vi conf/nginx.conf,修改后看起来像:

  1. user  chenlb;  
  2. worker_processes  10;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10. events {  
  11.     use epoll;  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15. http {  
  16.     include       mime.types;  
  17.     default_type  application/octet-stream;  
  18.   
  19.     #log_format  main  '$remote_addr - $remote_user [$time_local] $request '  
  20.     #                  '"$status" $body_bytes_sent "$http_referer" '  
  21.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  22.   
  23.     #access_log  logs/access.log  main;  
  24.   
  25.     sendfile        on;  
  26.     #tcp_nopush     on;  
  27.   
  28.     #keepalive_timeout  0;  
  29.     keepalive_timeout  65;  
  30.   
  31.     #gzip  on;  
  32.   
  33.     upstream demo {  
  34.         server 172.0.0.1:8080 weight=1;  
  35.         server 192.168.1.100:8080 weight=1;  
  36.   
  37.     }  
  38.   
  39.     server {  
  40.         listen       80;  
  41.         server_name  nobody.chenlb.com;  
  42.   
  43.         #charset koi8-r;  
  44.   
  45.         log_format  nobody_chenlb_com  '$remote_addr - $remote_user [$time_local] $request '  
  46.                       '"$status" $body_bytes_sent "$http_referer" "$http_x_forwarded_for"';  
  47.   
  48.         access_log  logs/access.log  nobody_chenlb_com;  
  49.   
  50.         location / {  
  51.             proxy_pass http://demo;  
  52.             proxy_set_header   Host             $host;  
  53.             proxy_set_header   X-Real-IP        $remote_addr;  
  54.             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
  55.         }  
  56.     }  
  57.   
  58. }  

修改浏览器所在机子hosts,nobody.chenlb.com 指向到nginx所在机子。

jsp的测试页面,nginx-test.jsp:

  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ page import="java.util.LinkedHashMap" %>  
  3. <%@ page import="java.util.Enumeration" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>nginx demo</title>  
  9. </head>  
  10. <body>  
  11. <%  
  12. LinkedHashMap map = new LinkedHashMap();  
  13. map.put("getServerName", request.getServerName());  
  14. map.put("getServerPort", request.getServerPort());  
  15. map.put("getRemoteAddr", request.getRemoteAddr());  
  16. map.put("getRemoteHost", request.getRemoteHost());  
  17. map.put("getRemotePort", request.getRemotePort());  
  18.   
  19. map.put("getLocalName", request.getLocalName());  
  20. map.put("getLocalAddr", request.getLocalAddr());  
  21. map.put("getLocalPort", request.getLocalPort());  
  22. //HttpServletRequest req = (HttpServletRequest)request;  
  23. for (Enumeration e = request.getHeaderNames() ; e.hasMoreElements() ;) {  
  24.     Object obj = e.nextElement();  
  25.     map.put(obj.toString(), request.getHeader(obj.toString()));  
  26. }  
  27. %>  
  28. <table border=1>  
  29. <tr><th>name</th><th>value</th></tr>  
  30. <%  
  31.   
  32. for(Object key : map.keySet()) {  
  33.     out.println("<tr><th>");  
  34.     out.println(key+"</th><th>"+map.get(key)+"</th></tr>");  
  35. }  
  36.   
  37. String str = request.getContextPath()+request.getServletPath()+(request.getPathInfo()==null?"":request.getPathInfo())+(request.getQueryString() == null ? "" : "?"+request.getQueryString());  
  38. System.out.println(str);  
  39. %>  
  40. </table>  
  41. </body>  
  42. </html>  

分别在172.0.0.1:8080、192.168.1.100:8080的web实例(webapps/demo)下放入nginx-test.jsp。

启动nginx:

  1. sbin/nginx  

在浏览器中访问:http://nobody.chenlb.com/demo/nginx-test.jsp,出现了一个问题,一时快,一时很慢,看到nginx在域名查找,后来把upstream的demo在nginx那台机子的hosts配上IP就好。

抱歉!评论已关闭.