Using Nginx on Docker

chauhoangminhnguyen - Sep 10 - - Dev Community

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.

Nginx on docker

Run Nginx with Docker

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

docker run -dp 8080:80 nginx:alpine
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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 / {
  }
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

After that, run Docker Compose with the following command:

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

The result will be as follows:
Custom index page

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.

BlogspotBlogspotDev.toFacebookX


Some series you might find interesting:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player