HUB
Sign inSign up
NGINX S3 Gateway

dhi.io/nginx-s3-gateway

NGINX S3 Gateway

CIS
FIPS
STIG
linux/amd64
linux/arm64

NGINX configured as an authenticating and caching gateway for S3 and S3-compatible object storage backends.

How to use this image

All examples in this guide use the public image. If you've mirrored the repository for your own use (for example, to your Docker Hub namespace), update your commands to reference the mirrored image instead of the public one.

For example:

  • Public image: dhi.io/nginx-s3-gateway:<tag>
  • Mirrored image: <your-namespace>/dhi-nginx-s3-gateway:<tag>

For the examples, you must first use docker login dhi.io to authenticate to the registry to pull the images.

Start an NGINX S3 Gateway image

The gateway is configured entirely through environment variables. It listens on port 8080 inside the container and proxies read requests to an S3 or S3-compatible bucket, signing each request with the credentials you provide.

The following example serves a private Amazon S3 bucket. Replace the placeholders with your bucket, Region, and credentials, then request an object through the gateway:

$ docker run -d --name nginx-s3-gateway -p 127.0.0.1:8080:8080 \
  -e S3_BUCKET_NAME=<bucket> \
  -e S3_SERVER=s3.<region>.amazonaws.com \
  -e S3_SERVER_PORT=443 \
  -e S3_SERVER_PROTO=https \
  -e S3_REGION=<region> \
  -e S3_STYLE=virtual-v2 \
  -e AWS_SIGS_VERSION=4 \
  -e ALLOW_DIRECTORY_LIST=false \
  -e AWS_ACCESS_KEY_ID=<access-key-id> \
  -e AWS_SECRET_ACCESS_KEY=<secret-access-key> \
  dhi.io/nginx-s3-gateway:<tag>

$ curl -i http://127.0.0.1:8080/<object-key>

The gateway answers GET, HEAD, and byte-range requests for objects in the bucket and caches what it fetches. A missing object is a 404. Any other method (PUT, POST, DELETE, PATCH) is refused with a 405. The port binds to the loopback interface in this example because the gateway does not authenticate its own clients: anyone who can reach the port can read the bucket.

Environment variables

The gateway refuses to start, and prints which variable is wrong, when a required variable is missing or invalid.

VariableDescriptionRequired
S3_BUCKET_NAMEName of the bucket to proxyYes
S3_SERVER, S3_SERVER_PORT, S3_SERVER_PROTOHost, port and protocol (http or https) of the S3 endpointYes
S3_REGIONRegion used to sign requestsYes
S3_STYLEAddressing style: path, virtual, virtual-v2, or defaultYes
AWS_SIGS_VERSIONAWS signature version, 4 or 2Yes
ALLOW_DIRECTORY_LISTWhether to list bucket contents, true or falseYes
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEYStatic credentials, or the _FILE form of eachYes
PROXY_CACHE_MAX_SIZE, PROXY_CACHE_VALID_OK, ...Cache size and lifetimes, defaults 10g and 1hNo

For every other setting, including instance-profile, ECS, and EKS credentials, see the upstream configuration documentation⁠.

Common NGINX S3 Gateway use cases

List the contents of a bucket

Set ALLOW_DIRECTORY_LIST=true and request a prefix that ends in a slash. The gateway returns an HTML listing:

$ docker run -d --name nginx-s3-gateway -p 127.0.0.1:8080:8080 \
  -e ALLOW_DIRECTORY_LIST=true \
  -e S3_BUCKET_NAME=<bucket> -e S3_SERVER=s3.<region>.amazonaws.com -e S3_SERVER_PORT=443 \
  -e S3_SERVER_PROTO=https -e S3_REGION=<region> -e S3_STYLE=virtual-v2 -e AWS_SIGS_VERSION=4 \
  -e AWS_ACCESS_KEY_ID=<access-key-id> -e AWS_SECRET_ACCESS_KEY=<secret-access-key> \
  dhi.io/nginx-s3-gateway:<tag>

$ curl http://127.0.0.1:8080/<prefix>/
Read credentials from Docker secrets

Set the _FILE form of a credential variable to a file path instead of putting the secret in the environment. The file must be readable by the gateway's user, and a credential variable and its _FILE form cannot both be set:

services:
  nginx-s3-gateway:
    image: dhi.io/nginx-s3-gateway:<tag>
    ports:
      - "127.0.0.1:8080:8080"
    environment:
      S3_BUCKET_NAME: <bucket>
      S3_SERVER: s3.<region>.amazonaws.com
      S3_SERVER_PORT: "443"
      S3_SERVER_PROTO: https
      S3_REGION: <region>
      S3_STYLE: virtual-v2
      AWS_SIGS_VERSION: "4"
      ALLOW_DIRECTORY_LIST: "false"
      AWS_ACCESS_KEY_ID_FILE: /run/secrets/aws_access_key_id
      AWS_SECRET_ACCESS_KEY_FILE: /run/secrets/aws_secret_access_key
    secrets:
      - aws_access_key_id
      - aws_secret_access_key

secrets:
  aws_access_key_id:
    file: ./secrets/aws_access_key_id
  aws_secret_access_key:
    file: ./secrets/aws_secret_access_key
Answer CORS requests from a browser application

Set CORS_ENABLED=true and the allowed origin. The gateway then answers preflight requests and adds the CORS headers to object responses:

$ docker run -d --name nginx-s3-gateway -p 127.0.0.1:8080:8080 \
  -e CORS_ENABLED=true -e CORS_ALLOWED_ORIGIN=https://app.example.com \
  -e S3_BUCKET_NAME=<bucket> -e S3_SERVER=s3.<region>.amazonaws.com -e S3_SERVER_PORT=443 \
  -e S3_SERVER_PROTO=https -e S3_REGION=<region> -e S3_STYLE=virtual-v2 -e AWS_SIGS_VERSION=4 \
  -e ALLOW_DIRECTORY_LIST=false \
  -e AWS_ACCESS_KEY_ID=<access-key-id> -e AWS_SECRET_ACCESS_KEY=<secret-access-key> \
  dhi.io/nginx-s3-gateway:<tag>

$ curl -i -X OPTIONS http://127.0.0.1:8080/<object-key> \
  -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: GET"
Proxy an S3-compatible endpoint with a private certificate authority

For an https endpoint the gateway verifies the origin certificate against /etc/ssl/certs/ca-certificates.crt. To trust a private CA instead, mount its certificate and point S3_TRUSTED_CERT_PATH at it. A certificate the gateway does not trust is never proxied:

$ docker run -d --name nginx-s3-gateway -p 127.0.0.1:8080:8080 \
  -v "$PWD/ca.crt:/etc/nginx/ca.crt:ro" -e S3_TRUSTED_CERT_PATH=/etc/nginx/ca.crt \
  -e S3_BUCKET_NAME=<bucket> -e S3_SERVER=<s3-host> -e S3_SERVER_PORT=9000 \
  -e S3_SERVER_PROTO=https -e S3_REGION=<region> -e S3_STYLE=path -e AWS_SIGS_VERSION=4 \
  -e ALLOW_DIRECTORY_LIST=false \
  -e AWS_ACCESS_KEY_ID=<access-key-id> -e AWS_SECRET_ACCESS_KEY=<secret-access-key> \
  dhi.io/nginx-s3-gateway:<tag>

Non-hardened images vs. Docker Hardened Images

The Docker Hardened Image runs the same gateway configuration and entrypoint scripts as upstream's unprivileged-oss image, so the rendered NGINX configuration is identical and the same environment variables and files work. The differences:

ItemUpstream unprivileged-ossDocker Hardened Image
PortExposes 80 and 8080, listens on 8080Exposes and listens on 8080
Usernginx, uid 101nginx, uid 65532
Empty defaultsENV sets several variables to an empty valueNot carried in the image. The entrypoint sets them empty before the configuration is rendered
Build metadataNGINX_VERSION, NJS_VERSION, PKG_RELEASE setNot set. The gateway does not read them
njsThe njs release upstream installs, 1.0.1The njs release published in the NGINX package, 0.9.9
NGINXUpstream's base image pins NGINX 1.31.5The nginx-mainline package current at build
njs command line/usr/bin/njsNot included. The gateway loads njs as an NGINX module
Read-only rootNot supportedNot supported. The entrypoint renders the configuration into /etc/nginx/conf.d, the directory that also holds the shipped credential cache files, so a tmpfs over it removes them

NGINX S3 Gateway is an exception to the shell-less runtime variants: the gateway's entrypoint scripts are shell scripts that render the configuration from environment variables, so the runtime variants include bash and the utilities those scripts call (awk, curl, envsubst, and the coreutils). The Debian variants add dash; the Alpine variants have no dash and get /bin/sh and the core utilities from busybox. They still run as a nonroot user and include no package manager. The dev variants add a package manager and run as root.

In the FIPS variants, both NGINX's TLS connection to the origin and the njs crypto module that signs requests use the OpenSSL FIPS provider. The gateway does not offer ChaCha20-Poly1305 to the origin, and crypto.createHash('md5') in njs is refused.

Image variants

Docker Hardened Images come in different variants depending on their intended use. Image variants are identified by their tag.

  • Runtime variants are designed to run your application in production. These images are intended to be used either directly or as the FROM image in the final stage of a multi-stage build. These images typically:

    • Run as a nonroot user
    • Do not include a shell or a package manager, except NGINX S3 Gateway, which includes a shell (see the exception in the section above)
    • Contain only the minimal set of libraries needed to run the app
  • Build-time variants typically include dev in the tag name and are intended for use in the first stage of a multi-stage Dockerfile. These images typically:

    • Run as the root user
    • Include a shell and package manager
    • Are used to build or compile applications
  • FIPS variants include fips in the variant name and tag. They come in both runtime and build-time variants. These variants use cryptographic modules that have been validated under FIPS 140, a U.S. government standard for secure cryptographic operations. For example, usage of MD5 fails in FIPS variants.

To view the image variants and get more information about them, select the Tags tab for this repository, and then select a tag.

Migrate to a Docker Hardened Image

To migrate your application to a Docker Hardened Image, you must update your Dockerfile. At minimum, you must update the base image in your existing Dockerfile to a Docker Hardened Image. This and a few other common changes are listed in the following table of migration notes.

ItemMigration note
Base imageReplace your base images in your Dockerfile with a Docker Hardened Image.
Package managementNon-dev images, intended for runtime, don't contain package managers. Use package managers only in images with a dev tag.
Non-root userBy default, non-dev images, intended for runtime, run as the nonroot user. Ensure that necessary files and directories are accessible to the nonroot user.
Multi-stage buildUtilize images with a dev tag for build stages and non-dev images for runtime. For binary executables, use a static image for runtime.
TLS certificatesDocker Hardened Images contain standard TLS certificates by default. There is no need to install TLS certificates.
PortsNon-dev hardened images run as a nonroot user by default. As a result, applications in these images can't bind to privileged ports (below 1024) when running in Kubernetes or in Docker Engine versions older than 20.10. To avoid issues, configure your application to listen on port 1025 or higher inside the container.
Entry pointDocker Hardened Images may have different entry points than images such as Docker Official Images. Inspect entry points for Docker Hardened Images and update your Dockerfile if necessary.
No shellBy default, non-dev images, intended for runtime, don't contain a shell. NGINX S3 Gateway runtime images include one, because the entrypoint scripts need it. Use dev images in build stages to run other shell commands and then copy artifacts to the runtime stage.

The following steps outline the general migration process.

  1. Find hardened images for your app.

    A hardened image may have several variants. Inspect the image tags and find the image variant that meets your needs.

  2. Update the base image in your Dockerfile.

    Update the base image in your application's Dockerfile to the hardened image you found in the previous step. For framework images, this is typically going to be an image tagged as dev because it has the tools needed to install packages and dependencies.

  3. For multi-stage Dockerfiles, update the runtime image in your Dockerfile.

    To ensure that your final image is as minimal as possible, you should use a multi-stage build. All stages in your Dockerfile should use a hardened image. While intermediary stages will typically use images tagged as dev, your final runtime stage should use a non-dev image variant.

  4. Install additional packages

    Docker Hardened Images contain minimal packages in order to reduce the potential attack surface. You may need to install additional packages in your Dockerfile. Inspect the image variants to identify which packages are already installed.

    Only images tagged as dev typically have package managers. You should use a multi-stage Dockerfile to install the packages. Install the packages in the build stage that uses a dev image. Then, if needed, copy any necessary artifacts to the runtime stage that uses a non-dev image.

    For Alpine-based images, you can use apk to install packages. For Debian-based images, you can use apt-get to install packages.

Troubleshooting migration

The following are common issues that you may encounter during migration.

General debugging

The hardened images intended for runtime don't contain debugging tools, and typically no shell (NGINX S3 Gateway runtime images include one for the entrypoint scripts). The recommended method for debugging applications built with Docker Hardened Images is to use Docker Debug⁠ to attach to these containers. Docker Debug provides a shell, common debugging tools, and lets you install other tools in an ephemeral, writable layer that only exists during the debugging session.

Permissions

By default image variants intended for runtime, run as the nonroot user. Ensure that necessary files and directories are accessible to the nonroot user. You may need to copy files to different directories or change permissions so your application running as the nonroot user can access them.

Privileged ports

Non-dev hardened images run as a nonroot user by default. As a result, applications in these images can't bind to privileged ports (below 1024) when running in Kubernetes or in Docker Engine versions older than 20.10. To avoid issues, configure your application to listen on port 1025 or higher inside the container, even if you map it to a lower port on the host. For example, docker run -p 80:8080 my-image will work because the port inside the container is 8080, and docker run -p 80:81 my-image won't work because the port inside the container is 81.

No shell

By default, image variants intended for runtime don't contain a shell, except NGINX S3 Gateway, whose entrypoint scripts need one. Use dev images in build stages to run shell commands and then copy any necessary artifacts into the runtime stage. In addition, use Docker Debug to debug containers with no shell.

Entry point

Docker Hardened Images may have different entry points than images such as Docker Official Images. Use docker inspect to inspect entry points for Docker Hardened Images and update your Dockerfile if necessary.