nginx负载均衡J2EE项目

Posted ygkeke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx负载均衡J2EE项目相关的知识,希望对你有一定的参考价值。

一、安装nginx-1.10.1.tar.gz

     1、下载:nginx-1.10.1.tar.gz    放到/usr/local目录下;提供官网地址:http://nginx.org/ 
                      a、命令下载:wget http://nginx.org/download/nginx-1.10.1.tar.gz
                      b、自己百度找;
     2、解压:tar -zxvf nginx-1.10.1.tar.gz 
     3、安装:[[email protected] nginx-1.10.1]# ./configure --prefix=/usr/local/nginx
                    安装一般会报错,缺少PCRE库和zlib库
                    a、安装PCRE库和zlib库
                          [[email protected] nginx-1.10.1]#: yum -y install pcre
                          [[email protected] nginx-1.10.1]#: yum -y install pcre-devel
                          [[email protected] nginx-1.10.1]# yum -y  install zlib
                          [[email protected] nginx-1.10.1]# yum -y  install zlib-devel
                    b、如果安装PCRE和zlib库失败,linux自带系统缺少库,自己上网下载
                          pcre-8.41.tar.bz2和zlib-1.2.11.tar.gz;
        4、安装好PCRE和Zlib后,执行
              [[email protected] nginx-1.10.1]# ./configure --prefix=/usr/local/nginx
              [[email protected] nginx-1.10.1]# make
              [[email protected] nginx-1.10.1]# make install
         5、安装完成:nginx目录说明:4个目录
                            -conf 配置文件
                            -html 网页文件
                            -logs 日志文件
                           -sbin 主要二进制文件
 
二、启动nginx,默认启动的是80端口,
            [[email protected] nginx]# ./sbin/nginx -c ./conf/nginx.conf        //等同于[[email protected] nginx]# ./sbin/nginx
               如果80被占用、把占用80端口的软件或者服务关闭即可
               1)查看 netstat --help
                      netstat -antp
                 [[email protected] nginx]# netstat -antp
                  Active Internet connections (servers and established)
                   Proto Recv-Q Send-Q Local Address  Foreign Address    State   PID/Program name    
                   tcp      0       0 0.0.0.0:80              0.0.0.0:*  LISTEN      11129/nginx: master 
                 [[email protected] nginx]# kill -9 11129   //杀进程
 
三、关闭nginx 
              [[email protected] nginx]# ps aux|grep nginx    //查看进程
               root     12879  0.0  0.0  20480   612 ?        Ss   21:31   0:00 nginx: master process ./sbin/nginx
               nobody   12881  0.0  0.0  23008  1376 ?        S    21:31   0:00 nginx: worker process
               root     13236  0.0  0.0 112664   972 pts/0    R+   21:47   0:00 grep --color=auto nginx
               [[email protected] nginx]# kill -QUIT 12879
                          
四、反向代理和负载均衡
         1、为了不影响nginx.conf,在/usr/local/nginx/conf文件夹下新建一个配置文件,
             ps:用命令来新建
            [[email protected] conf]# touch fzjh.conf
 
          2、在fzjh.conf文件里面写,注意绿色字体是注释,不要写到文件里面;
user nobody;
worker_processes 4;
events{
     worker_connections 1024;
     }
http{
     upstream ems {
         ip_hash;                          #避免系统session导致的问题,同一个客户端只连接一个服务器
         server 10.152.234.72:8080;
         server 10.152.234.73:80;
     }
     server {
         listen 80;
         location / {
             proxy_pass http://ems;           #ems和上面的ems要保持一致
             proxy_set_header Host $host;     #这是解决访问的时候DNS的问题,总是导致ip地址为ems
         }
     }
}
             
         3、启动负载均衡,注意使用的是80端口,把nginx.conf关闭掉,再测试这个
         [[email protected] nginx]# ./sbin/nginx -c ./conf/fzjh.conf

 

以上是关于nginx负载均衡J2EE项目的主要内容,如果未能解决你的问题,请参考以下文章

nginx负载均衡配置

利用nginx实现Redis的负载均衡,应该怎么配置?

关于Nginx负载均衡目录同步问题

负载均衡之Ribbon与LoadBalance

.Net Core+Nginx实现项目负载均衡

Nginx负载均衡