前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS 6.5系统下Nginx反向代理实现Tomcat负载均衡

CentOS 6.5系统下Nginx反向代理实现Tomcat负载均衡

作者头像
星哥玩云
发布2022-07-04 13:15:48
1310
发布2022-07-04 13:15:48
举报
文章被收录于专栏:开源部署开源部署

CentOS 6.5系统下Nginx反向代理实现Tomcat负载均衡

1.查看当前系统的内核和系统参数以及版本。

[root@node1 ~]# uname -a Linux node1 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux [root@node1 ~]# cat /etc/issue CentOS release 6.5 (Final) Kernel \r on an \m

2.安装nginx。 1)安装gcc编译器及相关工具和依赖库。 [root@node1 ~]# yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers pcre

2)编译安装pcre库。 PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式。Pcre的作用主要是使nginx支持HTTP rewrite模块。 创建一个指定放置压缩包的目录。 [root@node1 ~]# mkdir -p /linuxidc/tools [root@node1 ~]# cd /linuxidc/tools/ [root@node1 tools]# tar -zxf pcre-8.33.tar.gz [root@node1 pcre-8.33]# ./configure [root@node1 pcre-8.33]# make && make install [root@node1 nginx-1.5.8]# ./configure --with-http_stub_status_module --with-http_ssl_module --prefix=/data/nginx [root@node1 nginx-1.5.8]# make && make install

注意:安装成功启动nginx,有可能会遇到下面错误, [root@node1 nginx-1.5.8]# /data/nginx/sbin/nginx -t /data/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

解决办法: [root@node1 ~]# cd /lib64/ [root@node1 lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1

3)此事再启动nginx,查看下进程和端口。 [root@node1 lib64]# /data/nginx/sbin/nginx -t nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok nginx: configuration file /data/nginx/conf/nginx.conf test is successful [root@node1 lib64]# /data/nginx/sbin/nginx  [root@node1 lib64]# ps -ef | grep nginx root      8991    1  0 16:43 ?        00:00:00 nginx: master process /data/nginx/sbin/nginx nobody    8992  8991  0 16:43 ?        00:00:00 nginx: worker process  root      8994  1907  0 16:44 pts/1    00:00:00 grep nginx [root@node1 lib64]# netstat -anpt | grep nginx tcp        0      0 0.0.0.0:80                  0.0.0.0:*                  LISTEN      8991/nginx

3.配置nginx web反向代理,实现两个tomcat负载均衡:

nginx配置文件如下:

[root@node1 ~]# cat /data/nginx/conf/nginx.conf

user  root;

worker_processes  1;

#error_log  logs/error.log  info;

pid  /data/nginx/logs/nginx.pid;

worker_rlimit_nofile 65535;

events {

    use epoll;

    worker_connections 65535;

    multi_accept on;

}

http {

    include      mime.types;

    default_type  application/octet-stream;

    charset  utf-8;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile      on;

    tcp_nopush    on;

    keepalive_timeout  60;

    server_tokens  off;

    limit_rate_after 3m;

    limit_rate 512k; 

    tcp_nodelay on;

    client_header_buffer_size 256k;

    large_client_header_buffers 4 256k;

    # Define nginx proxy module

        proxy_http_version 1.1;

        proxy_connect_timeout 60;

        proxy_read_timeout 60;

        proxy_send_timeout 60;

        proxy_buffer_size  16k;

        proxy_buffers      4 64k;

        proxy_busy_buffers_size    128k;

        proxy_temp_file_write_size 128k;

        proxy_headers_hash_max_size 51200;

        proxy_headers_hash_bucket_size 6400; 

    # Define Gzip compression module

    gzip on;

    gzip_vary on;

    gzip_min_length  1k;

    gzip_buffers    4 16k;

    gzip_http_version 1.0;

    gzip_comp_level 2;

    gzip_types      text/plain application/x-Javascript text/css application/xml;

    # Define realserver pool

    upstream  linuxidc.com {

        ip_hash;

        server 192.168.1.15:8080  max_fails=0  weight=5;

        server 192.168.1.19:8080  max_fails=0  weight=5;

  }

    server {

        listen      80;

        server_name  linuxidc.net www.linuxidc.net;

        location / {

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP      $remote_addr;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                proxy_pass  http://linuxidc.com;

                expires 1d; 

                access_log  logs/host.access.log  main;

        }

        # Define 404 502 503 504 error page

        error_page  404              /404.html;

        error_page  500 502 503 504  /50x.html;

        location = /50x.html {

            root  html;

        }

    }

}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档