拉取镜像
docker pull nginx
创建目录
mkdir -p /data/nginx/{conf,conf.d,html,logs}
在conf目录下创建nginx.conf并编辑
编辑nginx.conf文件
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 192.168.241.35;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://myngnix;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
upstream myngnix{
server 192.168.241.35:8080 weight=5;
server 192.168.241.35:8081 weight=5;
}
}
以挂载的方式启动
–name 给你启动的容器起个名字,以后可以使用这个名字启动或者停止容器
-p 映射端口,将docker宿主机的82端口和容器的80端口进行绑定
-d 表示后台启动的是哪个镜像
第一个-v 表示将你本地的nginx.conf覆盖你要起启动的容器的nginx.conf文件,
第二个表示将日志文件进行挂载
docker run --name mynginx -d -p 82:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx
同样以挂载方式启动两个web项目。(注:项目名要相同,即都为SunECM)。部署详情见:https://blog.youkuaiyun.com/weixin_43614067/article/details/106430490
docker run --name tomcat8080 -d -v /opt/docker_file/SunECM:/usr/local/tomcat/webapps/SunECM -p
8080:8080 --restart=always tomcat:latest
docker run --name tomcat8081 -d -v /opt/docker_file2/SunECM:/usr/local/tomcat/webapps/SunECM -p
8081:8080 --restart=always tomcat:latest
nginx访问路径:http://192.168.241.35:82/SunECM/
配置以权重的方式负载均衡。测试4次,结果如图