Docker如何使用nginx搭建tomcat集羣

首先創建tomcat的文件夾 ,

爲了方便docker的配置 我這裏直接在根目錄中創建

第一步:創建文件夾:發佈文件夾

mkdir -p /docker/tomcat/webapp8081

mkdir -p /docker/tomcat/webapp8082

mkdir -p /docker/tomcat/webapp8083

 

 

 

 

第二步:創建Tomcat容器(端口 可以根據自己的實際更換)

docker run -d --name tomcat8081 -p 8081:8080 -v /docker/tomcat/webapp8081:/usr/local/tomcat/webapps/ tomcat

 

docker run -d --name tomcat8082 -p 8082:8080 -v /docker/tomcat/webapp8082:/usr/local/tomcat/webapps/ tomcat

 

 

docker run -d --name tomcat8083 -p 8083:8080 -v /docker/tomcat/webapp8083:/usr/local/tomcat/webapps/ tomcat

 

創建完成後使用 docker ps  命令進行查看是否創建成功 並且使用

 

 

 

 

第三步:查看tomcat的IP 使用命令依次查詢 這裏只使用第一個舉例

docker inspect tomcat8081

 

 

 

第四步:爲了方便測試 我這裏就不上傳war包了,直接 在裏面創建了一個hello/index.html 文件

 

 

 

 

注意:如果NginxDocker容器,必須使用Tomact容器IP,否則連不上

 

首先在官網上下載nginx的官方版本

官網:http://nginx.org/en/

點擊右邊導航欄的download,進入下載界面 選擇對應的版本 進行下載,我這裏就使用nginx-1.6.2.tar

 

 

 

下載完成後,將文件放到自定義的文件夾,我這裏放到/usr/local/tools/nginx-1.6.2

 

 

 

使用 這個命令將nginx 解壓:

tar vxf nginx-1.6.2.tar.gz

解壓完成後,我這裏是返回根目錄,在根目錄創建一個宿主文件夾,目的是爲了創建文件,使得nginx可以掛載(你也可以自定義)

創建宿主文件夾 這裏

mkdir -p /docker/nginx/

vim /docker/nginx/nginx.conf

mkdir -p /docker/nginx/html

拷貝頁面你解壓的negix中的html文件夾中的index.html 50x.html到/docker/nginx/html文件夾中

 

這裏提供一種negix的conf文件,以爲加上註解 所以格式可能會發生改變 記得把註解刪了

Nginx.conf:

user  root;

worker_processes  2;  #這裏設置你的線程數

 

#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;

upstream mytomcat{

server 172.17.0.3:8080 weight=10;          

# 另外mytomcat 這裏名字和下方的名字保持一致 這裏需要和你的tomcat IP保持一致

server 172.17.0.4:8080 weight=50;

server 172.17.0.5:8080 weight=10;

}

#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;

 

server {

listen       80;

server_name  mytomcat;

 

#charset koi8-r;

 

#access_log  logs/host.access.log  main;

 

location / {

# root   html;

# index  index.html index.htm;

proxy_connect_timeout 50;

proxy_read_timeout 10;

proxy_send_timeout 20;

proxy_pass http://mytomcat;

}

 

#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;

#    }

#}

 

}

 

使用docker 啓動

創建並運行容器

81:是外網訪問的端口 這裏可以根據實際做修改

/docker/nginx/nginx.conf 本地的宿主文件

/etc/nginx/nginx.conf 解壓的目錄(也可以不更改)

/docker/nginx/html 本地的宿主文件

/usr/share/nginx/html 解壓的目錄

 

docker run -d --name nginx81 -p 81:80 -v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /docker/nginx/html:/usr/share/nginx/html nginx

 

測試

http://39.106.147.162:8085/hello/index.html 我這裏配置的是8085端口 直接訪問

原文出處:https://www.cnblogs.com/anonymityning/p/12119055.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章