【问题标题】:How to configure a non dockerized nginx to serve static files from a docker container?如何配置非 dockerized nginx 以提供来自 docker 容器的静态文件?
【发布时间】:2022-01-18 02:53:01
【问题描述】:

说明

我已经在 ubuntu 服务器上安装了 NGINX。它可以工作,我可以使用来自同一服务器的静态文件运行不同的网站。 现在安装 docker 并构建一个镜像,将其推送到这个 ubuntu 服务器并使用 docker 运行它。当我调用它server-ip:port 时,我可以使用静态文件访问该站点。

问题

我不知道,如何说 nginx 的静态文件在哪里。因为它们在 docker 容器内。

Nginx 配置

server {
        
        index index.php index.html index.htm index.nginx-debian.html;

        server_name sub.domain.com www.sub.domain.com;
        
        root <------ what here?
        
        location / {
            proxy_pass http://localhost:7000;
        }

        proxy_set_header Host $http_host;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";

        charset utf-8;

         # php fpm
         location ~ \.php$ {
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
        }

        location = /robots.txt {
          allow all;
          log_not_found off;
          access_log off;
        }

        location = /favicon.ico {
          allow all;
          log_not_found off;
          access_log off;
        } 

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

        # basic cache control
        location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
            expires 30d;
            add_header Cache-Control "public,max-age=31536000";
            access_log off;
        }

        # svg, fonts
        location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
            expires 30d;
            add_header Access-Control-Allow-Origin "*";
            add_header Cache-Control "public,max-age=31536000";
            access_log off;
        }

        # gzip
        gzip on;
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

}

问题

安装在服务器上(不在 docker 容器内)的 Nginx 能否从 Docker 容器内的公共文件夹中提供静态文件?

【问题讨论】:

    标签: docker nginx proxy


    【解决方案1】:

    容器内的文件当然只是简单地存储在主机文件系统中,因此您可以使其工作。假设 Docker 使用的是overlay2 存储驱动程序(现在通常是这种情况),您可以像这样获取容器根文件系统的路径:

    $ docker inspect mycontainer -f '{{ .GraphDriver.Data.MergedDir }}'
    

    这将输出如下内容:

    /var/lib/docker/overlay2/ec046389c3a9e0cfb2a0ab2875ad8f660556fa086de9062f3231a0c6ea0a4e93/merged
    

    只需将容器内的路径附加到该路径即可。例如,如果您知道容器中的文件位于 /var/www/htdocs,那么您需要:

    root /var/lib/docker/overlay2/ec046389c3a9e0cfb2a0ab2875ad8f660556fa086de9062f3231a0c6ea0a4e93/merged/var/www/htdocs
    

    但是有很多原因说明这是一个坏主意,其中包括:

    • 这很脆弱,因为它取决于您的 Docker 守护程序正在使用的存储驱动程序
    • 每次启动新容器时路径都会有所不同
    • 如果您感兴趣的文件存储在 Docker 卷上,这将不起作用

    如果您从主机的角度将容器视为黑匣子,通常情况会好得多。

    【讨论】:

    • 感谢您的回答 :) 因此,更好的解决方案是创建第二个容器并将其设为 NGINX 容器,并将其与我的应用程序容器放在同一个网络中。然后服务器上的 NGINX 简单地转发到可以提供静态文件的 NGINX 容器。
    【解决方案2】:

    我认为我一直在寻找的解决方案是坐骑。

    在主机上我创建一个目录:

    path/to/hostdirectory

    然后我用 docker 创建一个存储

    docker volume create --driver local \
        --opt type=none \
        --opt device=path/to/hostdirectory \
        --opt o=bind \
        <volume-name>
        
    

    然后我用这个命令运行图像:

    docker run -it -d -p 127.0.0.1:7000:7000 --name &lt;containr-name&gt; -v &lt;volume-name&gt;:path/inside/container &lt;image-name&gt;

    对于-it-d-p 的描述,您可以运行docker run --help。 我更喜欢使图像的端口仅在本地可用。因此我把 127.0.0.1 放在7000:7000 前面。

    在 nginx 配置文件中它看起来像这样:
    root path/to/hostdirectory
    例如,NGINX 可以通过这种方式从该文件夹中提供静态文件。

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 2020-06-17
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      相关资源
      最近更新 更多