如何在nginx alpine docker镜像中启用本地模块

3

我想使用http://nginx.org/en/docs/http/ngx_http_realip_module.html,该模块可以通过启用--with-http_realip_module来使用。

我正在使用Docker来使用这个Nginx。

FROM nginx:alpine

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

现在该如何在这个Docker中启用此模块?我需要在docker run期间传递一些参数吗?还是需要修改Dockerfile文件?
1个回答

4

默认情况下,nginx 使用--with-http_realip_module构建

您可以通过检查nginx:alpine docker镜像来验证是否已构建该模块。

$ docker run --rm nginx:alpine nginx -V
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
nginx version: nginx/1.19.6
built by gcc 9.3.0 (Alpine 9.3.0)
built with OpenSSL 1.1.1g  21 Apr 2020 (running with OpenSSL 1.1.1i  8 Dec 2020)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer' --with-ld-opt=-Wl,--as-needed

正如你所看到的,--with-http_realip_module 已经存在。

你需要做的是,适当地配置 nginx(在 nginx.conf 中)

例如:

set_real_ip_from  192.168.1.0/24;
set_real_ip_from  192.168.2.1;
set_real_ip_from  2001:0db8::/32;
real_ip_header    X-Forwarded-For;
real_ip_recursive on;

2
好的,明白了。但是为什么在官方文档中写道此模块不是默认构建的呢? https://nginx.org/en/docs/http/ngx_http_realip_module.html - coderelliot
@coderelliot:抱歉,我打错了。谢谢你指出来,我已经改正了。很高兴它对你有帮助 :) - Mr.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接