nginx再docker中运行,访问宿主机一个较高级别的文件 报错 failed (13: Permission denied)

原因两种:

1.目标文件文件权限太高了,导致nginx访问全权限不够。

2.文件权限不应该跟高,使得nginx无法访问

 

第一种:提升nginx运行程序的权限

user  root; ##这个位置
worker_processes  64;

error_log  /vled (13: Permission denied)ar/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中运行的时候注意nginx.conf和default.conf不能再同一个目录,不然回报配置文件加载错误。因为重新加载了nginx.conf配置文件

version: '3.1'
services:
  nginx:
    restart: always
    image: nginx:latest
    container_name: nginx
    privileged: true
    user: root
    networks:
       etone_work:
          ipv4_address: 192.168.32.13
    ports:
      - 80:80
      - 8888:8888
    volumes:
      - /opt/docker_nginx/conf.d:/etc/nginx/conf.d
      - /opt/docker_nginx/img:/data/img
      - /opt/docker_nginx/html:/data/html
      - /opt/docker_HttpServer/api/jar/images:/data/shtest/picture/images
      - /home/static:/data/home/static:rw
      - /opt/docker_nginx/nginx.conf:/etc/nginx/nginx.conf:rw





networks:
    etone_work:
       external:  true

第二种:使用程序更改文件的权限

 

//这个方法我还没测试
String command = "chmod 770 " + dirPath; 
Process process = runtime.exec(command);

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐