【问题标题】:404 while trying to make dockerized nginx serve static files mounted in a volume尝试使 dockerized nginx 为安装在卷中的静态文件提供 404
【发布时间】:2018-05-18 02:23:21
【问题描述】:

我正在努力在 docker 容器中设置 nginx。我基本上有两个容器:

  • 为动态网站提供服务的 php:7-apache 容器,包括其静态内容。

  • 一个 nginx 容器,其中安装了一个卷作为 /home/www-data/static-content 文件夹(我在 docker-compose.yml 中执行此操作),以尝试提供静态网站(与 apache 容器服务的网站无关)。

我想使用域 dynamic.localhost 为我的动态网站提供服务,并使用 static.localhost 为我的静态网站提供仅由静态文件组成的网站。 p>

我的 nginx 容器有以下 Dockerfile:

########## BASE #########

FROM nginx:stable

########## CONFIGURATION ##########

ARG DEBIAN_FRONTEND=noninteractive

ENV user www-data

COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./site.conf /etc/nginx/conf.d/default.conf

RUN touch /var/run/nginx.pid && \
    chown -R ${user}:${user} /var/run/nginx.pid && \
    chown -R www-data:www-data /var/cache/nginx

RUN chown -R ${user} /home/${user}/ && \
    chgrp -R ${user} /home/${user}/

USER ${user}

如您所见,我为 nginx 使用了两个配置文件:nginx.conf 和 site.conf。 这是 nginx.conf(它并不重要,因为它没有什么特别之处,但如果我做错了什么,请告诉我):

worker_processes auto;
error_log /var/log/nginx/error.log debug;
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;
    keepalive_timeout 65;
    include /etc/nginx/conf.d/*.conf;
}

这是我几天来一直无法正确编写的 site.conf 文件:

server {
    listen 8080;
    server_name static.localhost;
    root /home/www-data/static-content;

    location / {
        try_files $uri =404;
    }
}

server {
    listen 8080;
    server_name dynamic.localhost;

    location / {
        proxy_pass http://dynamic;
        proxy_redirect off;
        proxy_set_header Host $host:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Port 8080;
        proxy_set_header X-Forwarded-Host $host:8080;
    }
}

http://dynamic 将请求传递给我命名为“动态”的 apache 容器)。

所以基本上我在我的静态内容目录中尝试访问的任何文件都会得到 404。例如:

  • static.localhost:8080/index.html 应该服务于 /home/www-data/static-content/index.html 但我得到的是 404。
  • static.localhost:8080/css/style.css 应该服务于 /home/www-data/static-content/css/style.css 但我也得到 404。

我尝试了各种方法,例如编写 try_files /home/www-data/static-content/$uri,但没有得到任何结果。我阅读了 nginx 文档的某些部分并在 Stack Overflow 上进行了搜索,但没有发现对我有帮助。如果我犯了一个愚蠢的错误,我深表歉意,但我现在唯一关心的是让它发挥作用,并了解我做错了什么。

谢谢

【问题讨论】:

  • 首先,您是否连接到正在运行的 docker 映像并查看 /home/www-data/static-content 中的内容?可能是一个简单的卷安装问题。
  • 是的,我做到了。卷已正确安装在正确的位置。唯一的问题是它的所有者与在容器中运行 nginx 的所有者(www-data)不同。但如果这是我的问题的原因,我想我会得到 403 而不是 404。
  • nginx 日志中的任何内容都指向有用的内容?
  • 日志不是很详细。你觉得我的 site.conf 没问题吗?

标签: docker nginx static


【解决方案1】:

我通过简单地不为静态文件使用卷而是将它们复制到容器中来解决了我的问题。我怀疑这是由 docker-compose 挂载卷的方式以及以非 root 身份运行的 nginx 进程的权限问题。 这不是一个完美的解决方案,因为我不得不放弃使用音量,但它会做到。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我必须更改用户组对挂载卷文件的权限。

    关于 dockerfile RUN useradd -G root,www-data -u 1000 ${user}

    并更改了主机文件的权限 chmod 775 /home/www-data/static-content -R

    【讨论】:

      猜你喜欢
      • 2017-01-06
      • 2015-11-30
      • 1970-01-01
      • 2021-10-03
      • 2018-10-12
      • 2021-05-27
      • 2019-08-21
      • 2019-04-12
      • 2016-01-28
      相关资源
      最近更新 更多