首页 > 编程知识 正文

docker安装nginx配置反向代理,docker安装nginx部署vue

时间:2023-05-06 09:23:25 阅读:203162 作者:2840

docker运行nginx

镜像地址:https://hub.docker.com/_/nginx

快速启动nginx容器

docker run -d --name nginx -p 80:80 nginx

持久化配置

mkdir -p /etc/nginx/docker run -d --name tmp-nginx-container nginxdocker cp tmp-nginx-container:/etc/nginx/nginx.conf /etc/nginx/docker rm -f tmp-nginx-containerdocker run -d --name nginx -p 80:80 -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro nginx

持久化数据

mkdir -p /usr/share/nginxdocker run -d --name tmp-nginx-container nginxdocker cp tmp-nginx-container:/usr/share/nginx/html /usr/share/nginxdocker rm -f tmp-nginx-containerdocker run -d --name nginx -p 80:80 -v /usr/share/nginx/html:/usr/share/nginx/html:ro nginx

dockerfile持久化配置和数据

FROM nginxCOPY static-html-directory /usr/share/nginx/htmlFROM nginxCOPY nginx.conf /etc/nginx/nginx.conf

完整示例

#取出nginx容器默认配置文件mkdir -p /data/nginx/docker run -d --name tmp-nginx-container nginxdocker cp tmp-nginx-container:/etc/nginx/nginx.conf /data/nginx/docker cp tmp-nginx-container:/etc/nginx/conf.d /data/nginx/docker cp tmp-nginx-container:/usr/share/nginx/html /data/nginx/docker rm -f tmp-nginx-container#运行nginx容器docker run -d --name nginx -p 80:80 -e TZ=Asia/Shanghai -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d:/etc/nginx/conf.d -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/logs:/var/log/nginx nginx

浏览器访问服务器80端口验证:

docker中/etc/nginx/nginx.conf默认配置文件

# cat /etc/nginx/nginx.confuser nginx;worker_processes 1;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { worker_connections 1024;}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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf;}

docker中/etc/nginx/conf.d/default.conf配置

# cat /etc/nginx/conf.d/default.confserver { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }} 配置示例

创建自定义html文件

mkdir -p /data/nginx/html/docker/cat > /data/nginx/html/docker/index.html <<EOF<!DOCTYPE html><html><head><title>Docker Nginx</title></head><body>Hello Nginx Docker!</body></html>EOF

创建对应配置文件

cat > /data/nginx/conf.d/docker.conf <<EOFserver { listen 80; server_name localhost; location / { root /usr/share/nginx/html/docker; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}EOF

检查配置文件是否正确

docker exec -it nginx nginx -t

配置热更新

docker exec -it nginx nginx -s reload

或重启nginx容器

docker restart nginx

浏览器访问验证

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。