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.

@Rule
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);
contentFolder.mkdir();
contentFolder.deleteOnExit();

// And "hello world" HTTP file
File indexFile = new File(contentFolder, "index.html");
indexFile.deleteOnExit();
@Cleanup
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);

assertThat(responseFromNginx(baseUrl))
    .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.19.7"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>nginx</artifactId>
    <version>1.19.7</version>
    <scope>test</scope>
</dependency>