nginx负载均衡程序安装札记

网络 网络优化 网络运维
将nginx负载均衡设定的关键内容作为总结,在本文中进行了介绍。只对nginx的安装和其中的配置文件重点说明,我们可以将此文作为札记,在对程序过程模糊的时候可进行参阅。

对于Nginx负载均衡的设定问题我们说过很多,也总结归纳过很多。在此,我们为了巩固前面的知识,将其中的安装要点再为大家重申一下。对于环境的构建,我们不过多的介绍了,前面已经说的很清楚,现在我们从安装开始说起,直接将配置代码最为重点呈献给大家。

 安装Nginx

1>安装
Nginx发音为[engine x],是由俄罗斯人Igor Sysoev建立的项目,基于BSD许可。据说他当初是F5的成员之一,英文主页:http://nginx.net。俄罗斯的一些大网站已经使用它超过两年多了,一直表现不凡。Nginx的编译参数如下:

  1. [root@localhost]#./configure --prefix=/usr/local/server/nginx --with-openssl=/usr/include \  
  2. --with-pcre=/usr/include/pcre/ --with-http_stub_status_module --without-http_memcached_module \  
  3. --without-http_fastcgi_module --without-http_rewrite_module --without-http_map_module \  
  4. --without-http_geo_module --without-http_autoindex_module 

在这里,需要说明一下,由于Nginx的配置文件中我想用到正则,所以需要pcre模块的支持。我已经安装了pcre及pcre-devel的 rpm包,但是 Ngxin 并不能正确找到 .h/.so/.a/.la 文件,因此我稍微变通了一下:

  1. [root@localhost]#mkdir /usr/include/pcre/.libs/  
  2. [root@localhost]#cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a  
  3. [root@localhost]#cp /usr/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la 

然后,修改objs/Makefile大概在908行的位置上,注释掉以下内容:   ./configure --disable-shared

接下来,就可以正常执行make及make install了。

2>修改配置文件/usr/local/server/nginx/conf/nginx.conf
以下是我的nginx.conf内容,仅供参考:

  1. #运行用户  
  2. usernobody nobody;  
  3. #启动进程  
  4. worker_processes2;  
  5. #全局错误日志及PID文件  
  6. error_loglogs/error.log notice;  
  7. pidlogs/nginx.pid;  
  8. #工作模式及连接数上限  
  9. events {  
  10. use epoll;  
  11. worker_connections1024;}  
  12. #设定http服务器,利用它的反向代理功能提供负载均衡支持  
  13. http {  
  14. #设定mime类型  
  15. include conf/mime.types;  
  16. default_typeapplication/octet-stream; 

#p#

  1. #设定日志格式  
  2. log_format main '$remote_addr - $remote_user [$time_local] ' 
  3. '"$request" $status $bytes_sent ' 
  4. '"$http_referer" "$http_user_agent" ' 
  5. '"$gzip_ratio"';  
  6. log_format download '$remote_addr - $remote_user [$time_local] ' 
  7. '"$request" $status $bytes_sent ' 
  8. '"$http_referer" "$http_user_agent" ' 
  9. '"$http_range" "$sent_http_content_range"';  
  10. #设定请求缓冲  
  11. client_header_buffer_size1k;  
  12. large_client_header_buffers4 4k;  
  13. #开启gzip模块  
  14. gzip on;  
  15. gzip_min_length1100;  
  16. gzip_buffers 4 8k;  
  17. gzip_types text/plain;  
  18. output_buffers 1 32k;  
  19. postpone_output1460;  
  20. #设定access log  
  21. access_loglogs/access.logmain;  
  22. client_header_timeout3m;  
  23. client_body_timeout3m;  
  24. send_timeout 3m;  
  25. sendfileon;  
  26. tcp_nopushon;  
  27. tcp_nodelay on;  
  28. keepalive_timeout65;  
  29. #设定负载均衡的服务器列表  
  30. upstream mysvr {  
  31. #weigth参数表示权值,权值越高被分配到的几率越大  
  32. #本机上的Squid开启3128端口  
  33. server 192.168.8.1:3128 weight=5;  
  34. server 192.168.8.2:80 weight=1;  
  35. server 192.168.8.3:80 weight=6;}  
  36. #设定虚拟主机  
  37. server {  
  38. listen80;  
  39. server_name 192.168.8.1 www.yejr.com;  
  40. charset gb2312;  
  41. #设定本虚拟主机的访问日志  
  42. access_loglogs/www.yejr.com.access.logmain;  
  43. #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid  
  44. #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好  
  45. location ~ ^/(img|js|css)/{  
  46. root/data3/Html;  
  47. expires 24h;} 
  1. #对 "/" 启用负载均衡  
  2. location / {  
  3. proxy_passhttp://mysvr;  
  4. proxy_redirectoff;  
  5. proxy_set_headerHost $host;  
  6. proxy_set_headerX-Real-IP $remote_addr;  
  7. proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;  
  8. client_max_body_size10m;  
  9. client_body_buffer_size 128k;  
  10. proxy_connect_timeout 90;  
  11. proxy_send_timeout90;  
  12. proxy_read_timeout90;  
  13. proxy_buffer_size 4k;  
  14. proxy_buffers 4 32k;  
  15. proxy_busy_buffers_size 64k;  
  16. proxy_temp_file_write_size 64k;}  
  17. #设定查看Nginx状态的地址  
  18. location /NginxStatus {  
  19. stub_status on;  
  20. access_logon;  
  21. auth_basic"NginxStatus";  
  22. auth_basic_user_fileconf/htpasswd;}}} 

3>查看 Nginx 运行状态
输入地址 http://192.168.8.1/NginxStatus/,输入验证帐号密码,即可看到类似如下内容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324

***行表示目前活跃的连接数

第三行的第三个数字表示Nginx运行到当前时间接受到的总请求数,如果快达到了上限,就需要加大上限值了。

责任编辑:佟健 来源: 互联网
相关推荐

2010-05-07 12:27:53

nginx负载均衡

2013-04-22 11:29:14

Nginx

2012-07-31 09:25:42

nginx负载均衡反向代理

2011-12-02 22:51:46

Nginx负载均衡

2010-05-07 12:23:23

nginx负载均衡

2011-01-07 11:14:17

Nginx负载均衡负载均衡

2014-07-28 11:37:49

NginxTomcat

2010-05-05 22:05:32

nginx负载均衡

2011-09-01 10:23:47

Nginx负载均衡器负载均衡

2010-03-30 13:37:14

Nginx负载均衡

2017-12-18 12:04:02

Nginx代理均衡

2012-11-06 16:51:29

nginx负载均衡

2019-11-04 15:35:53

Nginx反向代理负载均衡

2019-06-24 15:58:53

TCPUDPNginx

2010-03-25 18:52:15

Nginx负载均衡

2010-05-04 10:44:18

JBOSS负载均衡

2017-11-09 10:42:11

Nginx负载均衡策略

2019-09-18 10:39:08

负载均衡反向代理TCP

2020-07-28 15:10:34

Nginx反向代理负载均衡

2020-01-14 09:40:00

Nginx负载均衡正向代理
点赞
收藏

51CTO技术栈公众号