How to build a tomcat cluster with nginx in Docker (illustrated in detail)
First of all, create a folder for tomcat. In order to facilitate the configuration of docker, I create it directly in the root directory. First step: create folder: publish folder.
Mkdir-p / docker/tomcat/webapp8081mkdir-p / docker/tomcat/webapp8082mkdir-p / docker/tomcat/webapp8083
Step 2: create a Tomcat container (the port can be changed according to your own reality)
Docker run-d-name tomcat8081-p8081: 8080-v / docker/tomcat/webapp8081:/usr/local/tomcat/webapps/ tomcatdocker run-d-name tomcat8082-p8082: 8080-v / docker/tomcat/webapp8082:/usr/local/tomcat/webapps/ tomcatdocker run-d-name tomcat8083-p8083: 8080-v / docker/tomcat/webapp8083:/usr/local/tomcat/webapps/ tomcat
After the creation is completed, use the docker ps command to check whether the creation is successful and use the
Step 3: check the IP of tomcat and use the command to query in turn. Only the first example is used here.
Docker inspect tomcat8081
Step 4: in order to facilitate testing, I will not upload the war package here, but create a hello/index.html file directly in it.
Note: if Nginx is a Docker container, you must use the Tomact container IP, otherwise you will not be connected
First download the official version of nginx on the official website.
Official website: http://nginx.org/en/
Click download in the navigation bar on the right, go to the download interface and select the corresponding version to download. I will use nginx-1.6.2.tar here.
When the download is complete, put the files in the custom folder, and I'll put them in / usr/local/tools/nginx-1.6.2 here.
Use this command to extract the nginx:
Tar vxf nginx-1.6.2.tar.gz
After unzipping, I am going back to the root directory and creating a host folder in the root directory in order to create files so that nginx can be mounted (you can also customize it)
Create a host folder here
Mkdir-p / docker/nginx/vim / docker/nginx/nginx.confmkdir-p / docker/nginx/html
Copy the page from the html folder in the negix you unzipped to the / docker/nginx/html folder
A conf file of negix is provided here, so you may change the format by adding comments. Remember to delete the comments.
Nginx.conf:
User root
Worker_processes 2; # set your number of threads here
# 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; # maximum number of connections} http {include mime.types;default_type application/octet-stream;upstream mytomcat {server 172.17.0.3 server 8080 weight=10;#. In addition, the name here is the same as the name below the mytomcat. It needs to be consistent with your tomcat IP. Server 172.17.0.4 weight=50. Server 172.17.0.5 log_format main 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 0keepaliveroomtimeout 65 boot 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 endurance timeout 10 ~ (th) redirect server error pages to the static page / 50x.html#error_page 502 503 504 / 50x.htmlboot 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 php$ 9000position # 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 8000X # 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 ~ (th) # ssl_ciphers HIGH _ ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;#} #}
Start using docker
Create and run the container
81: it is the port accessed by the public network. It can be modified according to the actual situation.
/ docker/nginx/nginx.conf local host file
/ etc/nginx/nginx.conf unzipped directory (may not be changed)
/ docker/nginx/html local host file
/ usr/share/nginx/html unzipped directory
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
test
Http://39.106.147.162:8085/hello/index.html, port 8085 is configured here.
Direct access
Summary
The above is the tutorial of Docker using nginx to build tomcat cluster, which is introduced by Xiaobian. I hope it will be helpful to you!