Deploying nginx with docker compose to configure virtual host

Time:2020-8-13

About nginx

NginxIt’s a high performance oneHTTPServer, reverse proxy server and e-mail(IMAP/POP3)Proxy server. Official testNginxIt can support 50000 concurrent links, andCPUAnd memory consumption is very low, running very stable.
 

Application scenarios of nginx

  • HTTPThe server:NginxIt’s aHTTPServices can be provided independentlyHTTPService. Can do web page static server.
  • Virtual host: can realize in a server virtual out of multiple websites.
  • Reverse proxy + load balancing: when the website traffic reaches a certain degree, a single server can not meet the user’s request, and multiple server clusters can be usedNginxDo reverse agent. Moreover, multiple servers can share the load equally, so that a server will not be idle due to high load downtime of one server.

Container arrangement

Server IP:192.168.230.130

  • establishdocker-compose.yml

    version: '3.1'
    services:
      nginx:
        restart: always
        image: nginx
        container_name: nginx
        ports:
  volumes:
    - ./conf/nginx.conf:/etc/nginx/nginx.conf
    - ./wwwroot:/usr/share/nginx/wwwroot
-Start container

$ docker-compose up -d

-Open browser to access [192.168.230.130] (192.168.230.130)

###What is a virtual host

Virtual host is a kind of special software and hardware technology. It can divide each computer on the network into multiple virtual hosts. Each virtual host can provide "www" services independently. In this way, a host can provide multiple "Web" services externally. Each virtual host is independent and does not affect each other.

Virtual host configuration can be realized through 'nginx'. Three types of virtual host configuration are supported by 'nginx'

-IP based virtual host
-Domain name based virtual host
-Port based virtual host

###Functional requirements

-'nginx' server: ` 192.168.230.130`
-'nginx' provides' 80 'and' 8080 'listening services
-To request the '80' port, request the 'HTML' in the 'html80' directory`
-To request the '8080' port, request the 'HTML' in the 'html8080' directory`

###Configure nginx virtual host

-Create 'html80' and 'html8080' under the directory '/ usr / local / docker / nginx / wwwroot', and create two directories respectively` index.html `Documents

-Create it in the directory '/ usr / local / docker / nginx / conf'` nginx.conf `, mount the nginx configuration file

user nginx;
#Specifies the amount of CPU resources to be used
worker_processes 1;

events {

#Number of connections
  worker_connections  1024;

}

http {

include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  
  #Virtual host one configuration
  server {
      #Specify port
      listen       80;
      #Specify IP (can be domain name)
      server_name  192.168.230.130;
      location / {
          #Resource access path in virtual host
          root   /usr/share/nginx/wwwroot/html8080;
          #Home page
          index  index.html index.htm;
      }
  }

  #Virtual host 2 configuration
  server {
      listen       8080;
      server_name  192.168.230.130;
      location / {
          root   /usr/share/nginx/wwwroot/html8080;
          index  index.html index.htm;
      }
  }

}

Each 'server' node represents a virtual host

-Create ` docker- compose.yml `

version: ‘3.1’
services:

nginx:
  restart: always
  image: nginx
  container_name: nginx
  ports:
    - 80:80
  volumes:
    - ./conf/nginx.conf:/etc/nginx/nginx.conf
    - ./wwwroot:/usr/share/nginx/wwwroot
-Start container

$ docker-compose up -d

-Open the browser to access [192.168.230.130] (192.168.230.130) and [192.168.230.130:8080] (192.168.230.130:8080)


-Author: Peng Chao
-This article was first published in personal blog:[ https://antoniopeng.com/2019/09/08/nginx/%E4%BD%BF%E7%94%A8DockerCompose%E9%83%A8%E7%BD%B2Nginx%E9%85%8D%E7%BD%AE%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA/ ] https://antoniopeng.com/2019/09/08/nginx/%E4%BD%BF%E7%94%A8DockerCompose%E9%83%A8%E7%BD%B2Nginx%E9%85%8D%E7%BD%AE%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA/ )