准备三台服务器

1.192.168.100.20   nginx

2.192.168.100.30   Java环境server1

3.192.168.100.40   Java环境server2


配置Java环境

解压压缩包

Nginx负载均衡_java

Nginx负载均衡_tomcat_02

Nginx负载均衡_java_03

Nginx负载均衡_java_04

Nginx负载均衡_tomcat_05

java.sh脚本导入到环境变量,使其生效并查看版本

Nginx负载均衡_tomcat_06

写段Java小脚本,测试是否可以正常工作

Nginx负载均衡_tomcat_07

Nginx负载均衡_java_08

执行:javac abc.java 编译后生成可执行文件

测试:java abc

原样输出则为成功!

Nginx负载均衡_java_09

下面安装tomcat:

Nginx负载均衡_java_10

Nginx负载均衡_nginx_11

Nginx负载均衡_java_12

启动tomcat ,监听8080端口

Nginx负载均衡_nginx_13


加入测试页面

创建web目录

Nginx负载均衡_nginx_14

Nginx负载均衡_java_15

Nginx负载均衡_tomcat_16

Nginx负载均衡_java_17

Nginx负载均衡_nginx_18

优化启动时间

Nginx负载均衡_java_19

Nginx负载均衡_tomcat_20



nginx配置

yum install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make -y

groupadd www

useradd -g www www -s /bin/false

tar zxvf nginx-1.6.0.tar.gz -C /opt

cd /opt/nginx-1.6.0/

./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-file-aio \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module

make && make install

vi /usr/local/nginx/conf/nginx.conf

#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcat_server {
                server 192.168.100.30:8080 weight=1;
                server 192.168.100.40:8080 weight=1;
               }
server {
             listen 80;
.....省略
location / {
                 root html;
                 index index.html index.htm;
                 proxy_pass http://tomcat_server;
     }

/usr/local/nginx/sbin/nginx -t                   //检测是否成功//
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

nginx        //启用nginx服务//

netstat -ntap | grep nginx

systemctl restart nginx        // 重启nginx//