Using Nginx on Docker

Chau Nguyen
2 min readSep 10, 2024

--

Introduction

Nginx is a popular open-source web server known for its superior performance compared to the Apache web server. Nginx supports various functionalities, including deploying an API gateway (reverse proxy), load balancer, and email proxy. It was initially developed to build a web server capable of efficiently handling 10,000 concurrent connections with low memory usage.

Run Nginx with Docker

To use Nginx with Docker, simply execute the following command:

docker run -dp 8080:80 nginx:alpine

By default, Nginx uses port 80, but you can map it to a different port if needed.

Custom Nginx Configuration

To customize the Nginx configuration, first, create a ` docker-compose.yml` file with the following content:

services:
serviceName:
image: nginx:alpine
ports:
- 8080:80
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./index.html:/usr/share/nginx/html/index.html

In the ` volumes` field, note that I have mapped two files: ` default.conf` to change the default Nginx configuration and ` index.html` to customize the default interface when accessing the Nginx web view.

The content of the ` default.conf` file is as follows:

server {
listen 80 default_server;
listen [::]:80 default_server;

server_name _;
root /usr/share/nginx/html;

location / {
}
}

This is the content of the ` index.html` file, which you can modify as needed:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nginx</title>
</head>

<body>
<h1>This is custom index page</h1>
</body>

</html>

After that, run Docker Compose with the following command:

docker compose up -d

The result will be as follows:

If you found value in this post, show your appreciation by sharing and commenting!

If you found this content helpful, please visit the original article on my blog to support the author and explore more interesting content.

BloggerMediumDev.toFacebookX.com

Some series you might find interesting:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Chau Nguyen
Chau Nguyen

No responses yet

Write a response