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
1
| docker run --name nginx_t -d -p 80:80 nginx:latest
|
1 2 3
| docker exec -it nginx_t /bin/bash nginx -t
|
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
|
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
|
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; 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; 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/;
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; } } }
|