一、nginx负载均衡docker部署
在home用户目录下 创建redis的相关文件夹
mkdir -p /home/jun/docker/nginx
cd /home/jun/docker/nginx
创建三个目录
mkdir shell #构建、启动、停止等脚本
mkdir dockerfile #dockerfile目录
mkdir volumes #挂载配置、日志、数据文件
cd /home/hd/docker/nginx/dockerfile
vim Dockerfile
Dockerfile内容
FROM nginx:1.21.3-alpine
USER hd
WORKDIR /home/hd
#CMD
创建shell的中脚本文件
build脚本 :构建docker镜像
vi build
#!/bin/bash
DOCKER_PATH=$PWD/../
sudo docker build -f $DOCKER_PATH/dockerfile/Dockerfile -t nginx:1.21.3-alpine $DOCKER_PATH/dockerfile/
run脚本
vi run :构建并启动docker容器
#!/bin/bash
DOCKER_PATH=$PWD/../
NAME=nginx
PORT=80
VERSION=1.21.3-alpine
docker run -d \
-p ${PORT}:80 \
-p 443:443 \
-p 8080:8080 \
--name ${NAME} \
-v /etc/localtime:/etc/localtime \
-v $DOCKER_PATH/volumes/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $DOCKER_PATH/volumes/log:/var/log/nginx \
-v $DOCKER_PATH/volumes/conf/ssl.crt:/etc/nginx/ssl.crt \
-v $DOCKER_PATH/volumes/conf/ssl_nopass.key:/etc/nginx/ssl_nopass.key \
--restart=unless-stopped nginx:${VERSION}
exec脚本
vi exec :进入已启动成功的docker容器
#!/bin/sh
sudo docker exec -it nginx /bin/bash
logs脚本
vi logs :docker容器日志 启动容器失败时,可执行查询日志
#!/bin/bash
sudo docker logs nginx
restart
vi restart :docker容器重启
#!/bin/sh
sudo docker restart nginx
rm
vi rm :删除docker容器
#!/bin/sh
sudo docker rm nginx
rmi
vi rmi:删除docker镜像 dockerfile文件修改时 才需要执行
#!/bin/sh
sudo docker rmi nginx:1.21.3-alpine
start
vi start:启动docker容器
#!/bin/sh
sudo docker start nginx
stop
vi stop:停止docker容器
#!/bin/sh
sudo docker stop nginx
文件权限
chown -R hd:hd /home/hd/docker-files/nginx
可执行文件
chmod +x /home/hd/docker-files/nginx/shell/*
user root;
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log warn;
events {
use epoll;
worker_connections 16384;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
client_max_body_size 50m;
gzip_static always;
gunzip on;
gzip on;
gzip_types
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
keepalive_timeout 65;
client_body_timeout 10s;
client_header_timeout 10s;
send_timeout 10s;
proxy_read_timeout 180s;
proxy_connect_timeout 10s;
access_log off;
upstream upstreamAPP {
server 192.212.8.117:443;
server 192.212.8.116:443;
}
server {
listen 443 ssl;
server_name baidu.com;
ssl_certificate /etc/nginx/ssl.crt;
ssl_certificate_key /etc/nginx/ssl_nopass.key;
location / {
proxy_pass https://upstreamAPP;
proxy_set_header Host $host:443;
# proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
proxy_ssl_session_reuse off;
proxy_ssl_protocols TLSv1.2;
}
}
}