Nignx反向代理负载均衡

本文详细介绍如何通过Nginx实现反向代理负载均衡,包括web服务器安装配置、Nginx编译安装及优化、系统内核参数调整等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nignx反向代理负载均衡


   1.web1和web2配置安装
   
   
  1. yum install httpd -y #安装httpd
  2. echo 192.168.1.188 >/var/www/html/index.html #输入网站显示各自IP
  3. systemctl restart httpd
  4. firewall-cmd --permanent --add-port=80/tcp #开启防火墙80端口
  5. firewall-cmd --reload
  6. #测试,浏览器登录IP查看,是否正常启动。
   2.Nginx负载均衡器配置安装
       1.nginx编译安装
   
   
  1. #添加不可登录的nginx用户
  2. useradd -s /sbin/nologin nginx
  3. #安装nginx编译工具和必要的库
  4. yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel gcc-c++
  5. #下载nginx源码包,并解压
  6. wget http://nginx.org/download/nginx-1.10.3.tar.gz
  7. tar -zxvf nginx-1.10.3.tar.gz
  8. cd nginx-1.10.3
  9. #取消debug模式(优化)
  10. vim auto/cc/gcc
  11. #注释以下内容
  12. # debug
  13. #CFLAGS="$CFLAGS -g"
  14. #设置参数 参数具体参考《Nginx编译参数》
  15. ./configure \
  16. --prefix=/usr/local/nginx \
  17. --with-http_ssl_module
  18. #编译并安装 
  19. make && make install
       2.利用TCMalloc优化Nginx性能(可选)
           1.安装libunwind
   
   
  1. wget http://ftp.twaren.net/Unix/NonGNU//libunwind/libunwind-1.1.tar.gz
  2. tar -zxf libunwind-1.1.tar.gz
  3. cd libunwind-1.1
  4. ./configure CFLAGS=-fPIC
  5. make CFLAGS=-fPIC
  6. make CFLAGS=-fPIC install
           2.安装gperftools
    
    
  1. wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.1.90/gperftools-2.1.90.tar.gz
  2. tar -zxf gperftools-2.1.90.tar.gz
  3. cd gperftools-2.1.90
  4. ./configure #提前安装gcc+(yum install gcc-c++ -y)
  5. make && make install
  6. echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf

  7. ldconfig

           3.编译nginx从而加载google_perftools_module
   
   
  1. ./configure \
  2. --prefix=/usr/local/nginx \
  3. --with-http_ssl_module \
  4. --with-google_perftools_module
  5. make
  6. #替换二进制文件
  7. cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
  8. cp ./objs/nginx /usr/local/nginx/sbin
           4.为 google_perftools创建线程目录
    
    
  1. mkdir /tmp/tcmalloc
  2. chmod 777 /tmp/tcmalloc/
           5.nginx配置文件加入后启动
   
   
  1. vim /usr/local/nginx/conf/nginx.conf
  2. google_perftools_profiles /tmp/tcmalloc;
           6.查看安装是否成功(yum install lsof -y)
   
   
  1. lsof -n | grep tcmalloc
       3.编辑nginx配置文件
   
   
  1. vim /usr/local/nginx/conf/nginx.conf
  2. #参数意思参考《nginx简介与配置》
  3. user nginx nginx;
  4. worker_processes 4;
  5. worker_cpu_affinity 0001 0010 0100 1000;
  6. error_log logs/error.log info;
  7. pid logs/nginx.pid;
  8. google_perftools_profiles /tmp/tcmalloc;
  9. worker_rlimit_nofile 65535;
  10. events {
  11. use epoll;
  12. worker_connections 65535;
  13. }
  14. http {
  15. include mime.types;
  16. default_type application/octet-stream;
  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. access_log logs/access.log main;
  21. error_log logs/error.log crit;
  22. charset utf-8;
  23. server_names_hash_bucket_size 128;
  24. client_max_body_size 8m;
  25. client_header_buffer_size 32k;
  26. large_client_header_buffers 4 64k;
  27. sendfile on;
  28. tcp_nopush on;
  29. tcp_nodelay on;
  30. keepalive_timeout 60;
  31. client_header_timeout 10;
  32. client_body_timeout 10;
  33. send_timeout 10;
  34. gzip on;
  35. gzip_min_length 1k;
  36. gzip_buffers 4 16k;
  37. gzip_http_version 1.1;
  38. gzip_comp_level 2;
  39. gzip_types text/plain application/x-javascript text/css application/xml;
  40. gzip_vary on;
  41. proxy_buffering on;
  42. proxy_connect_timeout 5;
  43. proxy_send_timeout 5;
  44. proxy_read_timeout 60;
  45. proxy_buffer_size 16k;
  46. proxy_buffers 4 32k;
  47. proxy_busy_buffers_size 64k;
  48. proxy_temp_file_write_size 64k;
  49. proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;
  50. proxy_temp_path /data/proxy/temp;
  51. upstream my_server_pool {
  52. ip_hash;
  53. server 192.168.1.186:80 weight=2 max_fails=2 fail_timeout=30;
  54. server 192.168.1.188:80 weight=2 max_fails=2 fail_timeout=30;
  55. }
  56. server {
  57. listen 80;
  58. server_name localhost;
  59. location / {
  60. proxy_cache cache_one;
  61. proxy_cache_valid 200 304 12h;
  62. proxy_cache_key $host$uri$is_args$args;
  63. proxy_set_header Host $host;
  64. proxy_set_header X-Real-IP $remote_addr;
  65. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  66. proxy_pass http://my_server_pool;
  67. expires 1d;
  68. }
  69. location ~.*\.(jsp|php|jspx)?$ {
  70. proxy_set_header Host $host;
  71. proxy_set_header X-Real-IP $remote_addr;
  72. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  73. proxy_pass http://my_server_pool;
  74. }
  75. }
  76. }
    
    
  1. #创建缓存目录
  2. mkdir -p /data/proxy/cache
  3. mkdir -p /data/proxy/temp
  4. chown -R nginx:nginx /data/
       4.启动nginx
     
     
  1. /usr/local/nginx/sbin/nginx
       5.开启防火墙
      
      
  1. firewall-cmd --permanent --add-port=80/tcp #开启防火墙80端口
  2. firewall-cmd --reload
   3.系统优化
       
       
  1. #内核优化
  2. vim /etc/sysctl.conf
  3. net.ipv4.tcp_max_tw_buckets = 6000
  4. net.ipv4.ip_local_port_range = 1024 65000
  5. net.ipv4.tcp_tw_recycle = 1
  6. net.ipv4.tcp_tw_reuse = 1
  7. net.ipv4.tcp_syncookies = 1
  8. net.core.somaxconn = 65535
  9. net.core.netdev_max_backlog = 262144
  10. net.ipv4.tcp_max_orphans = 262144
  11. net.ipv4.tcp_max_syn_backlog = 262144
  12. net.ipv4.tcp_synack_retries = 1
  13. net.ipv4.tcp_syn_retries = 1
  14. net.ipv4.tcp_fin_timeout = 1
  15. net.ipv4.tcp_keepalive_time = 30
  16. sysctl -p #立即生效
        
        
  1. #nginx配置改后,但系统默认进程最大打开文件1024,所以需要改变
  2. #立即生效
  3. ulimit -n 65535
  4. ulimit -u 65535
  5. #永久生效
  6. #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
  7. vim /etc/security/limits.conf
  8. * soft nproc 65535
  9. * hard nproc 65535
  10. * soft nofile 65535
  11. * hard nofile 65535
   4.测试(访问192.168.1.166)
   

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值