我正在尝试在Docker容器中使用这个动态模块配置NGINX,并且在这篇文章中我找到了一种方法(也许,但我不确定)使用内部的reverse_proxy来实现mod_zip
nginx.conf
文件。
但是我在尝试通过 端口 80 访问文件服务器时遇到问题,它返回以下错误消息:
[error] 30#30: *1 mod_zip: invalid file list from upstream while sending to client, client: 172.28.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "127.0.0.1"
有人可以帮我解决这个问题吗?也许可以为我提供一个可以在 Docker 容器中使用 NGINX
的示例我将分享我的配置文件,以便更好地理解这个过程:
您有
Dockerfile
文件,它使用两个阶段进行构建,一个用于编译 hello_world_nginx
模块(仅用于测试目的),另一个用于编译 mod_zip
模块。
并且还将
files
目录中的一些文件添加到之前创建的目录中,该目录称为 /var/www
,需要将文件提供给 NGINX,作为文件服务器,并且文件可以是任何 uncompressed 格式,然后使用 mod_zip模块选择并下载为
zip
格式。
Dockerfile
:
FROM nginx:1.25.2 AS build
RUN apt-get update
RUN apt-get install -y build-essential \
wget \
git \
libpcre3 libpcre3-dev \
zlib1g zlib1g-dev \
openssl libssl-dev \
procps
RUN mkdir src
WORKDIR /src/
RUN wget http://nginx.org/download/nginx-1.25.2.tar.gz
RUN tar -xzvf nginx-1.25.2.tar.gz
RUN git clone https://github.com/perusio/nginx-hello-world-module.git
RUN git clone https://github.com/evanmiller/mod_zip.git
RUN git clone --branch=patch-1 https://github.com/dvershinin/nginx-unzip-module.git
# * Hello world module.
RUN cd nginx-1.25.2/ && ./configure --with-compat --add-dynamic-module=../nginx-hello-world-module
RUN cd nginx-1.25.2 && make modules
RUN cd nginx-1.25.2 && cp objs/ngx_http_hello_world_module.so /etc/nginx/modules
# * Zip module.
RUN cd nginx-1.25.2/ && ./configure --with-compat --add-dynamic-module=../mod_zip
RUN cd nginx-1.25.2 && make modules
RUN cd nginx-1.25.2 && cp objs/ngx_http_zip_module.so /etc/nginx/modules
#* Development stage.
FROM nginx:1.25.2 AS development-stage
COPY --from=build /etc/nginx/modules/ngx_http_hello_world_module.so /etc/nginx/modules
COPY --from=build /etc/nginx/modules/ngx_http_zip_module.so /etc/nginx/modules
RUN mkdir -p /var/www/
COPY ./files /var/www
RUN chown -R www-data:www-data /var/www/
COPY nginx.conf /etc/nginx
然后,在
nginx.conf
文件的配置中,我有一条加载 hello_world_module
(仅用于测试目的)和 mod_zip
模块的语句。
当我使用这个
nginx.conf
文件时,它不会返回有关这些模块加载的任何错误,这似乎是正确的。
然后,在
http
块内,启用 gzip
模块,并定义一个将侦听 server
端口的 8080
指令,以及将映射 /
路径的位置指令包含 /var/www
目录中的内容,该目录包含我需要在文件服务器内显示的 uncompressed 文件,然后我需要使用 compressed
模块下载带有 .zip
扩展名的 mod_zip
。
最后,我定义了另一个
server
指令,它将侦听 80
端口并映射第一个 server
指令的响应。但是,正如我所说,这似乎不起作用。
nginx.conf
:
load_module /etc/nginx/modules/ngx_http_hello_world_module.so;
load_module /etc/nginx/modules/ngx_http_zip_module.so;
worker_processes auto;
events {
worker_connections 1024;
}
http {
gzip on;
gzip_types text/plain text/css
application/json
application/javascript
text/xml
application/xml
application/xml+rss
text/javascript;
server {
listen 8080;
server_name 127.0.0.1;
location / {
# hello_world;
root /var/www;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
add_header X-Archive-Files 'zip';
# this line sets the name of the zip that the user gets
add_header Content-Disposition 'attachment; filename=example.zip';
add_header Content-Type application/zip;
}
}
server {
listen 80 default_server;
location / {
root /var/www;
proxy_hide_header X-Archive-Files;
proxy_set_header Accept-Encoding "";
proxy_pass_request_headers off;
proxy_pass http://127.0.0.1:8080;
}
}
}
我最近写了一篇博客文章描述了如何使用 mod_zip,并在 Docker 中运行了一个工作示例。与您的示例不同,代理/上游是 FastAPI 应用程序,但原理是相同的。它可以作为一个很好的起点,您可以将某些逻辑从代理移动到 nginx 配置中。