安装 Nginx 配置负载均衡

安装 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就好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来解答你的问题。 首先,你需要在Ubuntu20.04上安装Nginx。如果你的机器无法连接互联网,需要先在有互联网连接的机器上下载Nginx安装包及其依赖,然后将这些文件拷贝到离线机器上进行安装。具体步骤如下: 1. 在有互联网连接的机器上下载Nginx和其依赖库,可以使用命令:`sudo apt-get download nginx`,该命令会下载Nginx及其依赖库的deb包到当前目录下。 2. 将下载好的deb包和依赖库拷贝到离线机器上,使用命令:`sudo dpkg -i xxx.deb`进行安装,xxx代表对应的deb包名称。 3. 安装完成后,使用命令:`sudo systemctl start nginx`启动Nginx服务。 接下来,你需要配置负载均衡。可以通过以下步骤实现: 1. 编辑Nginx配置文件:`sudo vi /etc/nginx/nginx.conf`。 2. 在http块中添加upstream模块的配置,如下所示: ``` http { upstream backend { server backend1.example.com weight=5; server backend2.example.com; server backend3.example.com; server backend4.example.com; } ... } ``` 其中,backend1.example.com、backend2.example.com等为你要进行负载均衡的服务器地址,weight=5代表backend1.example.com的权重为5,权重越高,被选中的概率越大。 3. 在server块中添加location模块的配置,如下所示: ``` http { ... server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } } ``` 其中,example.com为你的域名,http://backend为upstream模块中定义的负载均衡后端服务器地址,$host和$remote_addr为Nginx的内置变量,用于设置HTTP头信息。 4. 保存配置文件并重新加载Nginx配置:`sudo nginx -t && sudo nginx -s reload`。 至此,你已经成功配置Nginx负载均衡。当用户访问example.com时,Nginx会根据upstream模块中定义的负载均衡算法,将请求转发到后端服务器进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值