首頁 > 運維 > Nginx > 主體

Docker怎麼用nginx來搭建tomcat集群

WBOY
發布: 2023-05-28 15:47:35
轉載
1226 人瀏覽過

先建立tomcat的資料夾,為了方便docker的配置我這裡直接在根目錄中建立第一步:建立資料夾:發布資料夾

mkdir -p /docker/tomcat/webapp8081

mkdir -p /docker/tomcat/webapp8082

mkdir -p /docker/tomcat/webapp8083
登入後複製

Docker怎麼用nginx來搭建tomcat集群

第二步:建立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 指令進行查看是否建立成功並且使用

Docker怎麼用nginx來搭建tomcat集群

第三步:查看tomcat的ip 使用指令依序查詢這裡只使用第一個舉例

docker inspect tomcat8081

Docker怎麼用nginx來搭建tomcat集群

Docker怎麼用nginx來搭建tomcat集群

# #第四步:為了方便測試我這裡就不上傳war包了,直接在裡面創建了一個hello/index.html 檔案Docker怎麼用nginx來搭建tomcat集群

注意:如果nginx為docker容器,必須使用tomact容器ip,否則連不上Docker怎麼用nginx來搭建tomcat集群

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

官網:

點擊右邊導覽列的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 本地的宿主檔案

Docker怎麼用nginx來搭建tomcat集群/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埠######直接存取############

以上是Docker怎麼用nginx來搭建tomcat集群的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!