如何防止 nginx 在启动时创建 index.html

如何防止 nginx 在启动时创建 index.html

我使用的是官方最新的nginx docker 容器

每当我启动容器时,都会创建两个文件/usr/share/nginx/html

root@ba65db04a18f:/# ls -la /usr/share/nginx/html   
total 16
drwxr-xr-x 2 root root 4096 Jun 28 15:38 .
drwxr-xr-x 3 root root 4096 Jun 23 00:55 ..
-rw-r--r-- 1 root root  537 May 30 13:03 50x.html
-rw-r--r-- 1 root root  612 May 30 13:03 index.html

我该如何阻止这些文件被创建?

我用来启动容器的命令是:

docker volume create static
docker run -it --rm --name nginx -v static:/usr/share/nginx/html:ro nginx

默认配置文件(/etc/nginx/nginx.conf)为:

user  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;
}

其中有一个/etc/nginx/conf.d名为 default.conf 的文件,其内容如下:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

我尝试default.conf通过使用以下命令启动容器来用空白文件覆盖此文件:

docker volume rm static
docker volume create static
docker run -it --rm --name nginx -v static:/usr/share/nginx/html:ro -v /path/on/host/to/blank/default.conf:/etc/nginx/conf.d/default.conf nginx

但 nginx 仍然会创建index.html50x.html。我该如何让它停止?

答案1

将以下内容添加到 docker-compose.yml

  nginx-afterstart:
    image: nginx
    volumes:
      - folder:/folder
    depends_on:
      - nginx
    restart: "no"
    entrypoint: [ "bash", "-c", "ls -al /folder/ && sleep 10 && rm /folder/*.html && ls -al /folder/"]

我知道这个帖子很旧了,但它是一个顶级搜索结果。:P

答案2

nginx docker 镜像负责index.html50x.html文件:

https://github.com/nginxinc/docker-nginx/blob/48a4c531fc4bfa80d4270f20a67e2e958856860c/stable/alpine/Dockerfile#L101-L102

为了防止创建它们,有必要扩展官方 nginx docker 镜像并删除这些文件。

或者,也应该可以改变CMD用于启动容器的方法,以便它在启动 nginx 进程之前删除文件。

相关内容