Docker nginx deploys multiple static web resources and hides ports

Keywords: Docker Container

Background:

Deploy multiple static resources on docker, hoping to access them directly using domain name instead of domain name + port number

Specific ideas:

First, use docker to create three nginx containers
1. nginx port 81 of web1 website deployment
2. nginx port 82 for web2 website deployment
3,proxy_nginx port 80 for proxy forwarding

explain:

1. Unified use of proxy_nginx container is used as the proxy and forwarding of the entry. This method is ideal and high-quality. Note that proxy_ The port of nginx must be 80, because when your browser accesses the domain name, it does not write the port by default, that is, port 80.

Preparation in advance:

1. First go to the ECs to resolve the domain name and reopen the 80 / 82 port used above in the security group.
2. The demo domain names used in this article are xxx1.com and xxx2.com (please replace them with your own actual domain name)

Specific steps:

1, Pull image

docker pull nginx

After successful pulling, execute docker images to view the nginx image

2, Use docker to create containers, and map (Mount) configuration files, etc

  1. First, create an nginx container just to copy the configuration files in it for subsequent mapping (in nginx.conf).

Note: you can also write the contents of the mapping file yourself. Or you can directly enter the container and copy the corresponding configuration file after creating the container.

docker run nginx
  1. Create a directory management web1 project. For example, the author here is / home/web1 /, and create conf, www and logs directories under / home/web1 /
mkdir conf  # Used to manage nginx.conf
mkdir www   # For managing static resources
mkdir logs	# For managing logs
  1. Copy the configuration file in the nginx container created in advance to / home/web1/conf
# Find the ID of the container
docker ps -a

# Copy the configuration file in the nginx container created in advance to your own management directory
# xxx is the ID of the nginx container created above
docker cp xxx:/etc/nginx/nginx.conf /home/web1/conf/

After copying, the above newly created nginx mission is completed and can be deleted directly

# xxx is the ID of the nginx container created above
docker rm xxx
  1. Upload the static website to / home/web1/www (this directory is where the web pages are stored)
# -C option enables SSH Remote compression function and faster transmission speed
scp -P Server port number -C /Static resource path/ user name@host ip:/home/web1/www/
  1. Create nginx container
    Note: the following command is a single line, and the author wrapped it for convenience of comments
docker run
--privileged #Using this parameter, the root in the container has the real root permission. Otherwise, the root in the container is only an external ordinary user permission.
-it     #-t let docker assign a pseudo terminal and bind it to the standard input of the container, - i keep the standard input of the container open
-d     #Ensure that the container runs in the background
-p 81:80     #Map native port 81 to container port 80
--name web1     #container name web1
-v /home/web1/www:/usr/share/nginx/html     #Map web page storage directory
-v /home/web1/conf/nginx.conf:/etc/nginx/nginx.conf     #Mapping profile
-v /home/web1/logs:/var/log/nginx     #Map log file directory
nginx     #image

Note: here, the ip address of the local machine is 81 and the ip address in the container is 80 (ports 80 in multiple containers will not conflict with each other, because docker containers are isolated). External access is through 81.

  1. Modify the configuration of / home/web1/conf/nginx.conf (add server under its http node) to modify the nginx.conf configuration in the container (because it is mapped).
vim /home/web1/conf/nginx.conf

# Note: the / home/web1/conf/nginx.conf in this computer has been mapped to the actual container, and the actual container shall prevail
server {
	listen 80; #Port in the listening container
	# server_name xxx.com; #Mapping domain names
	server_name 120.xx.xx.xx; #Or local IP
	location / {
		root /usr/share/nginx/html; #The path of the static resource in the actual container
		index index.htm index.html;
	}
}
  1. Press esc and: wq is saved successfully. Restart web1 container
    Note: you can also restart web1 container directly here, but the disadvantage of this usage is that if there is an error in the configuration, it cannot be displayed
#Method 1
docker restart web1

#Method 2
docker exec -it web1 nginx -t
docker exec -it web1 nginx -s reload

That's it. Visit http://120.xx.xx.xx:81 Or http://xxx.com:81 You can visit the website;

  1. Refer to the above steps to create a new container web2:
docker run --privileged -it -d -p 82:80 --name web2 -v /home/web2/www:/usr/share/nginx/html -v /home/web2/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/web2/logs:/var/log/nginx nginx

9. Create a new container proxy_nginx is used for proxy forwarding, and the proxy file is configured

Note: proxy_ The nginx container uses the native port 80 to forward static resources web1 and web2 by proxy

docker run --privileged -it -d -p 80:80 --name proxy_nginx -v /home/proxy_nginx/www:/usr/share/nginx/html -v /home/proxy_nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/proxy_nginx/logs:/var/log/nginx nginx

Configure proxy file / home/proxy_nginx/conf/nginx.conf

#Direct paste code
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

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

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    # web1
    server {
		listen 80; #Port 80 of this machine
		server_name xxx1.com;
		location / {
			proxy_redirect off;
			proxy_set_header Host $host;
    	    proxy_set_header X-Real-IP $remote_addr;
     		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     		proxy_pass http://120.xx.xx.xx:81/;
		}
    }

    # web2
    server {
		listen 80; #Port 80 of this machine
		server_name xxx2.com;
		location / {
			proxy_redirect off;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_pass http://120.xx.xx.xx:82/;
        }
    }
}
  1. Forward xxx1.com to http://120.xx.xx.xx:81 (web1);
  2. Forward xxx2.com to http://120.xx.xx.xx:82 (web2);
  3. After modification, the docker restart proxy_nginx;
  4. You can visit xxx1.com directly in the browser

~end

Posted by jtacon on Sat, 23 Oct 2021 22:38:31 -0700