Skip to content

Nginx Module

Nginx is a web server, reverse proxy and mail proxy and http cache.

Usage example

The following example shows how to start Nginx.

public NginxContainer<?> nginx = new NginxContainer<>(NGINX_IMAGE)
    .withCopyFileToContainer(MountableFile.forHostPath(tmpDirectory), "/usr/share/nginx/html")
    .waitingFor(new HttpWaitStrategy());

How to add custom content to the Nginx server.

// Create a temporary dir
File contentFolder = new File(tmpDirectory);

// And "hello world" HTTP file
File indexFile = new File(contentFolder, "index.html");
PrintStream printStream = new PrintStream(new FileOutputStream(indexFile));
printStream.println("<html><body>Hello World!</body></html>");

And how to query the Nginx server for the custom content added.

URL baseUrl = nginx.getBaseUrl("http", 80);

    .as("An HTTP GET from the Nginx server returns the index.html from the custom content directory")
    .contains("Hello World!");

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

testImplementation "org.testcontainers:nginx:1.20.0"