How to install and configure Docker nginx

How to install and configure Docker nginx

Download Nginx image in Docker

docker pull nginx
docker images 

Create a mount directory

mkdir -p /data/nginx/{conf,conf.d,html,logs} 

Write the nginx.conf configuration file and put it in the folder

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
  worker_connections 1024;
}

http {
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  # Load modular configuration files from the /etc/nginx/conf.d directory.
  # See http://nginx.org/en/docs/ngx_core_module.html#include
  # for more information.
  include /etc/nginx/conf.d/*.conf;

  server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name 182.254.161.54;
    root /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    proxy_pass http://pic; 
    }

    error_page 404 /404.html;
      location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
      location = /50x.html {
    }
  }

  upstream pic{
        server 182.254.161.54:8088 weight=5;
        server 182.254.161.54:8089 weight=5;
  }

}

Start the container

Copy the code as follows:
docker run --name mynginx -d -p 82:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx

View the started container

docker ps 

Previously, two tomcats have been deployed in Docker, one is port 8088 and the other is port 8089, and simple pages have been written in the two containers.

Access port 8088

Access port 8089

Now we use Nginx to access the contents of the two tomcats to achieve load balancing. For the sake of difference, the load balancing function can be better reflected. The contents of the two pages are different, but the access paths are the same. We just use Nginx reverse proxy to access them in rotation.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to install nginx in docker and configure access via https
  • Docker installation Nginx tutorial implementation illustration
  • How to install Nginx in Docker
  • Install Nginx and configure port forwarding using Docker

<<:  Installation method of MySQL 5.7.18 decompressed version under Win7x64

>>:  A simple way to change the password in MySQL 5.7

Recommend

JS+Canvas realizes dynamic clock effect

A dynamic clock demo based on Canvas is provided ...

What you need to know about MySQL auto-increment ID

Introduction: When using MySQL to create a table,...

webpack -v error solution

background I want to check the webpack version, b...

Solution to mysql prompt "got timeout reading communication packets"

Error message: user: 'root' host: `localh...

Overview and differences between html inline elements and html block-level elements

Block-level element features : •Always occupies a ...

Example of viewing and modifying MySQL transaction isolation level

Check the transaction isolation level In MySQL, y...

Detailed explanation of MySQL deadlock and database and table sharding issues

Record the problem points of MySQL production. Bu...

js to realize a simple puzzle game

This article shares the specific code of js to im...

Detailed explanation of writing and using Makefile under Linux

Table of contents Makefile Makefile naming and ru...

4 principles for clean and beautiful web design

This article will discuss these 4 principles as t...

MySQL database table partitioning considerations [recommended]

Table partitioning is different from database par...

Tips to prevent others from saving as my web page and copying my site

Nowadays, copying websites is very common on the I...

CentOS 7 builds hadoop 2.10 high availability (HA)

This article introduces how to build a high-avail...

18 common commands in MySQL command line

In daily website maintenance and management, a lo...