負載均衡tomcat


cd /usr/local/tomcat1/webapps/ROOT/

tar -zxvf nginx-1.14.2.tar.gz -C /usr/local

一、Linux配置Nginx

一、下載Nginx

  方式1:從http://nginx.org/en/download.html上下載穩定版,解壓安裝

 

  方式2:直接在Linux上用命令下載: wget http://nginx.org/download/nginx-1.10.2.tar.gz
  -bash: wget: command not found

  安裝wget:
  yum -y install wget

  再執行下載nginx

二、解壓安裝包&重命名   

  tar -zxvf nginx-1.14.2.tar.gz -C /usr/local

  mv nginx-1.14.2 nginx

三、編譯

  1、cd 到nginx目錄下

  2、安裝相關組件

  • yum install -y pcre pcre-devel
  • yum install -y zlib zlib-devel
  • yum install -y openssl openssl-devel

  這是提示缺少c++環境 ,用 yum install gcc-c++ 安裝一下,再執行 ./configure,然后又報錯了:

 

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre= option.

沒裝偽靜態模塊需要pcre庫
解決方法:
yum install -y pcre pcre-devel

還有可能出現:
錯誤提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
–without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
–with-http_ssl_module –with-openssl= options.

解決辦法:
yum -y install openssl openssl-devel

最后 ./configure

 

執行make 編譯:

然后:make install  報錯:

 

 問題原因: 直接把安裝包重命名成nginx了,安裝文件沒有路徑了。

解決辦法如下:

刪除nginx 文件夾
rm -rf nginx

重新解決源碼
tar -zxvf nginx-1.10.2.tar.gz

cd /usr/local/nginx-1.10.2

生成Makefile文件

  ./configure --prefix=/usr/local/nginx

編譯源碼
make

安裝
make install

這里不必要太糾結,只要 /usr/local/ 下出現了 /nagix文件就ok,進入 cd /usr/local/nginx/sbin 下,啟動 ./nginx

問題1:出現端口占用,nginx一般是80端口,要么把其他的kill掉,要么更改nginx的端口

1、kill掉其他的之前,要知道哪個占用了:用 lsof -i:80可以查看,這里是之前的lamp占用了

 

2、我們可以修改nginx自身的監聽端口,vi /usr/local/nginx/conf/nginx.conf  ,將listen 80,改為自己要的就行,我們這里改81

  

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log logs/host.access.log main;
  6. location / {
  7. root html;
  8. index index.html index.htm;
  9. }

之后進入我們的ip:81就可以訪問:

 

 

 

 

二、Linux配置Tomcat

  1、下載

  下載地址:https://tomcat.apache.org/download-90.cgi

  這里我們用tomcat 9 吧,下載后,一樣放到 /usr/local 下,解壓兩份,一份作為 tomcat1 , 一份為 tomcat 2 這兩份,用來做負載均衡

  解壓: tar -zxvf apache-tomcat-9.0.13.tar.gz -C /usr/local

  重命名: mv apache-tomcat-9.0.13 tomcat1

  

  2、配置:

  修改其中一個tomcat2 的端口信息,tomcat1則不需要修改

  cd /usr/local/tomcat2/conf

  vi server.xml

  改以下三個端口為:8006,8081,8099

  1. <Server port="8005" shutdown="SHUTDOWN">
  2. <Connector port="8080" protocol="HTTP/1.1"
  3. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

  3、分別更改兩個 tomcat下默認的jsp頁面 
  cd /usr/local/tomcat1/webapps/ROOT/

  vi index.jsp 
  

  在body中添加一行html 代碼,用於區分是哪個tomcat下的頁面。

  4、啟動兩個tomcat

  

 

   cd /usr/local/tomcat1/bin

  ./startup.sh

  打開本地的ip:8080和ip:8081可以看到,兩個頁面都有提示信息:

但是8080 被占用,我們用8081和8082:8081

8082

 這樣,就是安裝成功啦!

三、Nginx配置Tomcat負載均衡

  1、安裝好nginx的情況下

  2、修改配置文件 
  cd /usr/local/nginx/conf 
  vi nginx.conf 
  添加ngnix分配策略(權重策略)

  

upstream   dangdang.com {
  server 192.168.66.129:8081  weight=1; //tomcat1 的ip和端口
  server 192.168.66.129:8082  weight=1; //tomcat2 的ip和端口
}

 整體文件

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #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  0;
    keepalive_timeout  65;
    #gzip  on;
  upstream dangdang.com { 
    server 192.168.66.129:8081 weight=1; //tomcat1 的ip和端口 
    server 192.168.66.129:8082 weight=1; //tomcat2 的ip和端口 
  }
    server {
        listen       81;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

結果:一直刷新

 
         

結果1:

 
         

 
         

結果2:

 
         

 
         

 

        location / {
            root   html;
            index  index.html index.htm;
       proxy_pass   http://dangdang.com;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM