• Nginx之搭建反向代理实现tomcat分布式集群


    参考博文: Nginx反向代理实现Tomcat分布式集群

    1. jdk 安装

    jdk 下载网址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    执行如下:

    # cd /usr
    # mkdir java
    # cp /xx/jdk-8u171-linux-x64.tar.gz /usr/java/
    # cd /usr/java
    # tar -zxvf jdk-8u171-linux-x64.tar.gz
    # ln -s /usr/java/jdk1.8.0_171/ /usr/jdk
    

    这样就安装好了,接下来配置环境变量:vim /etc/profile,添加如下内容:

    JAVA_HOME=/usr/jdk
    CLASSPATH=$JAVA_HOME/lib/
    PATH=$PATH:$JAVA_HOME/bin
    export PATH JAVA_HOME CLASSPATH
    

    执行命令:source /etc/profile

    然后查看安装情况:java -version:

    java version "1.7.0_181"
    OpenJDK Runtime Environment (IcedTea 2.6.14) (7u181-2.6.14-0ubuntu0.1)
    OpenJDK 64-Bit Server VM (build 24.181-b01, mixed mode)
    

    2. 安装 tomcat

    源码下载: http://tomcat.apache.org/download-80.cgi

    执行如下:

    # tar -zxvf apache-tomcat-8.5.31.tar.gz
    # mkdir /usr/local/tomcat-cluster
    # cp -r apache-tomcat-8.5.31 /usr/local/tomcat-cluster/tomcat01
    # cp -r apache-tomcat-8.5.31 /usr/local/tomcat-cluster/tomcat02
    # cd /usr/local/tomcat-cluster/
    

    tomcat01 仍然使用默认的 8080 端口,修改 tomcat02 的端口号:

    cd tomcat02
    vim conf/server.xml
    

    将 Connector port 由 8080 改为 8081,同时将 Server Port 由 8005 改为 8006。

    为了区分不同的 tomcat 更改 tomcat02 中的 index.jsp:

    vi tomcat02/webapps/ROOT/index.jsp
    

    可修改 "If you're seeing this,..." 这条语句。

    启动 tomcat01 和 tomcat02:

    # tomcat01/bin/startup.sh
    # tomcat02/bin/startup.sh
    

    使用浏览器访问 tomcat01:

    使用浏览器访问 tomcat02:

    3. 配置 nginx

    配置、编译:

    ./configure --prefix=/home/rong/samba/nginx-1.13.2/tmp --pid-path=/home/rong/samba/nginx-1.13.2/tmp/var/nginx.pid --lock-path=/home/rong/samba/nginx-1.13.2/tmp/var/lock/nginx.lock --error-log-path=/home/rong/samba/nginx-1.13.2/tmp/var/log/error.log --http-log-path=/home/rong/samba/nginx-1.13.2/tmp/var/log/access.log --with-http_gzip_static_module --http-client-body-temp-path=/home/rong/samba/nginx-1.13.2/tmp/var/temp/client --http-proxy-temp-path=/home/rong/samba/nginx-1.13.2/tmp/var/temp/proxy --http-fastcgi-temp-path=/home/rong/samba/nginx-1.13.2/tmp/var/temp/fastcgi --http-uwsgi-temp-path=/home/rong/samba/nginx-1.13.2/tmp/var/temp/uwsgi --http-scgi-temp-path=/home/rong/samba/nginx-1.13.2/tmp/var/temp/scgi
    
    make
    
    make install
    

    配置 nginx.conf 实现反向代理 comcat 集群:

    worker_processes 1;
    
    events {
    	use epoll;
        worker_connections  1024;
    }
    
    daemon off;
    
    master_process off;
    
    error_log stderr debug;
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        
        sendfile        on;
        
        keepalive_timeout  65;
        
        upstream rong {
    		server 192.168.56.101:8080;
    		server 192.168.56.101:8081;
    	}
    	
    	server {
    	    listen       80;
    	    server_name  localhost;
    	    
    	    location / {
    			proxy_pass http://rong;
                root   html;
                index  index.html index.htm;
            }
            
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    	}
    }
    

    该配置仅使用 upstream 的轮询机制。

    由于没有域名,直接使用本机的 ip 进行访问:

    这里的访问是随机的,当前访问到的是 tomcat01,刷新后可能访问到 tomcat02。

  • 相关阅读:
    Codeforces 385C
    Codeforces 496C
    HDU 6114 Chess
    Codeforces 839B
    Codeforces 483B
    Codeforces 352B
    Codeforces 768B
    Codeforces 38B
    Codeforces 735B
    Codeforces 534B
  • 原文地址:https://www.cnblogs.com/jimodetiantang/p/9220550.html
Copyright © 2020-2023  润新知