docker 构建 nginx

docker 构建 nginx

需要使用 docker 拉取 nginx 镜像 docker pull nginx:latest

docker 拉取 nginx 镜像

  • 需要更换 docker 源 vi /etc/docker/daemon.json
1
2
3
4
{
"log-level": "error",
"registry-mirrors": ["https://docker.1panel.live"]
}
  • 更换 snap 版本 docker 源 vi /var/snap/docker/current/config/daemon.json
1
2
3
4
{
"log-level": "error",
"registry-mirrors": ["https://docker.1panel.live"]
}

snap 版本 docker 服务重启命令

1
2
3
4
5
6
7
8
# 重启
systemctl restart snap.docker.dockerd
# 查看状态
systemctl status snap.docker.dockerd
# 停止
systemctl stop snap.docker.dockerd
# 启动
systemctl start snap.docker.dockerd

运行 nginx

  • 运行测试 nginx_t 容器
1
docker run --name nginx_t -d -p 80:80 nginx:latest
  • 进入容器,查看 nginx.conf 文件位置
1
2
3
docker exec -it nginx_t /bin/bash
nginx -t
#/etc/nginx/nginx.conf
  • 拷贝配置文件和路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
mkdir -p nginx
mkdir -p nginx/conf
mkdir -p nginx/html

#拷贝文件
docker cp nginx_t:/etc/nginx/nginx.conf /root/nginx/conf/
#拷贝文件夹
docker cp nginx_t:/etc/nginx/conf.d/ /root/nginx/conf/
#拷贝文件夹
docker cp nginx_t:/usr/share/nginx/html/ /root/nginx/html/

docker stop nginx_t
#删除
docker rm nginx_t
  • 修改 nginx.conf 文件,启动正式容器
1
2
3
sudo docker run -d -e TZ=Asia/Shanghai -p 80:80 -p 443:443 -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /root/nginx/html/:/usr/share/nginx/html/ --name nginx --link demo_pi nginx:latest

# --link 连接其他容器
  • 配置nginx.conf文件,参考:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
user  nginx;
worker_processes 4;

error_log /var/log/nginx/error.log error;
pid /var/run/nginx.pid;

events {
worker_connections 65535;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

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

keepalive_timeout 65;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
client_max_body_size 500m;
client_body_buffer_size 256k;

upstream demo_api_a {
ip_hash;
server demo_api:21021; #连接的其他的容器名称,容器端口
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# 解决跨域问题
proxy_hide_header X-Frame-Options;
# 隐藏nginx版本
server_tokens off;

server {
listen 80;
server_name localhost;
charset utf-8;

root /usr/share/nginx/html/; #前端文件夹路径,容器映射

location /web/ {

# 客户端缓存配置,反应在浏览器上
add_header Cache-Control no-cache;
# expires 1d;
if ($request_filename ~* ^.*?\.(js|css|gif|jpg|jpeg|png|bmp|swf|png|svg|mp4|ogg|ogv|webm|htc|xml|woff|woff2|ico|pdf)$){
expires 1d;
}
# 前端文件
index index.html;
}
location /apis/ {
proxy_pass http://demo_api_a/; ##后端api

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

charset utf-8;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

# 服务器缓存配置
proxy_cache off; # 默认关闭
}
}
}