nginx 安全加固

Nginx 官方容器的安全加固主要包括以下几个方面:

  1. 最小化容器:尽量只包含运行应用程序所需的最小文件和依赖项。这可以减少攻击面,提高安全性。

  2. 使用非root用户运行容器:默认情况下,Docker 容器以 root 用户身份运行,这可能会带来安全风险。你可以在 Dockerfile 中使用 USER 指令来指定一个非root用户运行容器。

  3. 限制容器的资源使用:你可以使用 Docker 的资源限制功能来限制容器的 CPU、内存等资源使用,防止容器消耗过多的系统资源。

  4. 更新和打补丁:定期更新容器的基础镜像和应用程序,以获取最新的安全更新和补丁。

  5. 使用安全的 Docker 配置:例如,禁用容器的 root 权限,限制容器的网络访问等。

以下是一个加固的 Dockerfile 示例:

# 使用官方的 nginx 镜像作为基础镜像
FROM nginx:latest

# 创建一个新的用户和用户组,用于运行 nginx
RUN groupadd -r nginx && useradd -r -g nginx nginx

# 更改 nginx 的配置文件和内容目录的所有权
RUN chown -R nginx:nginx /etc/nginx /var/cache/nginx

# 切换到新创建的用户
USER nginx

# 指定 nginx 配置文件的路径
CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]

这只是一个基本的示例,你可能需要根据你的具体需求进行更多的定制和优化。
6. nginx 隐藏server: nginx信息

在 Nginx 中,你可以通过修改配置文件来隐藏或修改 Server: nginx 响应头。这可以通过在 http、server 或 location 块中添加 server_tokens 指令来实现。

以下是如何在全局范围内禁用 server_tokens 的示例:
http {
server_tokens off;

...

}

这将删除 Server: nginx 响应头中的版本号,但仍然会显示 nginx。如果你想完全删除 Server 响应头,你需要使用第三方模块,如 headers-more-nginx-module。

nginx -V | echo - > test.txt
nginx version: nginx/1.22.1
built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4)
built with OpenSSL 3.0.7 1 Nov 2022 (running with OpenSSL 3.0.8 7 Feb 2023)
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 -g' --with-ld-opt=-Wl,--as-needed,-O1,--sort-common

–add-module=…/headers-more-nginx-module-0.33

如果你已经安装了 headers-more-nginx-module,你可以使用 more_set_headers 指令来删除或修改 Server 响应头,如下所示:
http {
more_set_headers ‘Server: My Custom Server’;

...

}
请注意,隐藏或修改 Server 响应头只能提供有限的安全性,因为它不能防止所有类型的攻击。你应该始终保持你的 Nginx 服务器和所有其他软件的更新,以获取最新的安全补丁。

多阶段编译示例:
https://www.coder.work/article/7364368
https://stackoverflow.com/questions/59189311/edit-hide-nginx-server-header-under-alpine-linux
https://gist.github.com/hermanbanken/96f0ff298c162a522ddbba44cad31081

ARG VERSION=alpine
FROM nginx:${VERSION} as builder

ENV MORE_HEADERS_VERSION=0.33
ENV MORE_HEADERS_GITREPO=openresty/headers-more-nginx-module

# Download sources
RUN wget "http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz" -O nginx.tar.gz && \
    wget "https://github.com/${MORE_HEADERS_GITREPO}/archive/v${MORE_HEADERS_VERSION}.tar.gz" -O extra_module.tar.gz

# For latest build deps, see https://github.com/nginxinc/docker-nginx/blob/master/mainline/alpine/Dockerfile
RUN  apk add --no-cache --virtual .build-deps \
    gcc \
    libc-dev \
    make \
    openssl-dev \
    pcre-dev \
    zlib-dev \
    linux-headers \
    libxslt-dev \
    gd-dev \
    geoip-dev \
    perl-dev \
    libedit-dev \
    mercurial \
    bash \
    alpine-sdk \
    findutils

SHELL ["/bin/ash", "-eo", "pipefail", "-c"]

RUN rm -rf /usr/src/nginx /usr/src/extra_module && mkdir -p /usr/src/nginx /usr/src/extra_module && \
    tar -zxC /usr/src/nginx -f nginx.tar.gz && \
    tar -xzC /usr/src/extra_module -f extra_module.tar.gz

WORKDIR /usr/src/nginx/nginx-${NGINX_VERSION}

# Reuse same cli arguments as the nginx:alpine image used to build
RUN CONFARGS=$(nginx -V 2>&1 | sed -n -e 's/^.*arguments: //p') && \
    sh -c "./configure --with-compat $CONFARGS --add-dynamic-module=/usr/src/extra_module/*" && make modules
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值