1.问题描述
1.docker容器中的nginx访问路径挂载在外部目录,删除外部目录导致docker内部的目录会被删除,访问nginx页面时报403
2.更新部署时如果是删除直接挂载目录会出同样的问题,而替换挂载目录中的子文件不会出问题
3.解决方法:新增一层目录层级,将最外层作为挂载目录,删除二级目录不会导致docker内部找不到文件问题
2.docker-compose.yaml配置
version: '3'
services:
my-nginx:
image: nginx:latest
hostname: "my-nginx"
container_name: nginx
environment:
- TZ=Asia/Shanghai
ports:
- 8080:8080
volumes:
# 将docker内部的文件夹目录挂载到外部 /opt/myproject上
#- /opt/myproject:/usr/share/nginx/myproject
# 将docker内部的文件夹目录挂载到外部 /opt/html上
- /opt/html:/usr/share/nginx/html
# 挂载配置
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
# 挂载日志
- ./nginx/logs:/var/log/nginx
privileged: true
restart: always
network_mode: "host"
3.nginx配置
user root;
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;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
client_max_body_size 50m;
server {
listen 8080;
server_name web;
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' *;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
#访问8080端口会到docker容器内部的/usr/share/nginx/myproject文件夹中找index.html\index.htm,docker将/usr/share/nginx/myproject挂载到外部的文件夹
#root /usr/share/nginx/myproject;
#访问8080端口会到docker容器内部的/usr/share/nginx/html/myproject文件夹中找index.html\index.htm,docker将/usr/share/nginx/html挂载到外部的文件夹
root /usr/share/nginx/html/myproject;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/92333.html