docker pull nginx:latest
docker run --name my-nginx -p 80:80 -d nginx
# 进入容器
docker exec -it my-nginx /bin/bash
# 安装编辑器(容器内)
apt-get update && apt-get install -y vim
# 编辑配置文件
vim /etc/nginx/nginx.conf
vim /etc/nginx/conf.d/default.conf
# 退出容器后重启
docker restart my-nginx
docker cp my-nginx:/etc/nginx/nginx.conf ./nginx.conf
docker cp my-nginx:/etc/nginx/conf.d/default.conf ./default.conf
mkdir -p ~/nginx-config
mv nginx.conf default.conf ~/nginx-config/
docker run --name my-nginx \
-p 80:80 \
-v ~/nginx-config/nginx.conf:/etc/nginx/nginx.conf \
-v ~/nginx-config/default.conf:/etc/nginx/conf.d/default.conf \
-v ~/nginx-config/html:/usr/share/nginx/html \
-d nginx
FROM nginx:latest
COPY custom.conf /etc/nginx/conf.d/
COPY html/ /usr/share/nginx/html/
docker build -t custom-nginx .
docker run --name my-custom-nginx -p 80:80 -d custom-nginx
编辑default.conf
:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
location /api/ {
proxy_pass http://backend:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
docker exec my-nginx nginx -t
docker logs my-nginx
docker exec my-nginx tail -f /var/log/nginx/access.log
bash
docker exec my-nginx nginx -s reload
通过以上方法,您可以灵活地在Docker中部署和管理Nginx服务,并根据需要修改其配置文件。