preface

The situation is that I borrowed my friend’s Aliyun server to deploy my website with Docker (easy to manage). His server itself hosts a web site using Nginx and uses the default port 80. The server has a domain name, keep999.cn

Mine is docker inside the added nginx container, proxy static web page. Docker maps port 8080 out, so, to access, need to

Keep999.cn :8080 to access. Now, I don’t want to add port access, just want to use the domain name (too lazy to write port), so, I have to start to think of a way to solve the problem.

[TOC]

harvest

By the end of this article you will have learned:

  • Docker creates nginx containers
  • Nginx mapping configuration file web directory log directory
  • Docker deploys multiple Web sites
  • Secondary domain names are forwarded to different Web sites
  • Ali Cloud set cloud resolution

To the chase

Specific practices:

Use domain name + forward to visit my website

Second, use domain name resolution

Use domain name + forward to visit my website

General method:

Docker container Three nginx containers

Mynginx my site is deployed on nginx port 8080

2, web_nginx friend’s site deployed nginx port 8081

Proxy_nginx nginx port 80 for proxy forwarding

First, this method is ideal, relatively high quality, unified use of Nginx to do entrance proxy and forwarding. Note that the proxy_nginx port must be 80, not anything else. Because your browser accesses the domain name, the default port is not written, which is port 80.

1, first of all, ali cloud configuration of a secondary domain name.

Configure the security group, I directly open 80 [proxy_nginx], 8080-8100 [deploy multiple sites] multiple ports

Docker creates containers and maps configuration files, etc

1) Docker creates mynginx to deploy my website

Create a new nginx to copy the configuration file for use during mapping

docker run nginx
Copy the code

Create a new directory to manage nginx, such as/myDockerData /nginx, and create the Dist Log directory

mkdir logMkdir dist docker ps View the ID of the containerCopy the code

Copy the configuration file to your own management directory

Nginx docker cp 【CONTAINER ID】 :/etc/nginx/nginx.conf /mydockerdata/nginx/Copy the code

When the copy is complete, you can delete the new nginx and create your own

docker rm nginx -f 
Copy the code

Upload static site to/myDockerData /nginx/dist

scp -r  /Users/jafir/Documents/myadmin/dist/  [email protected]:/mydockerdata/nginx/
Copy the code

Create an nginx container

Docker run -p 8080:80 Maps port 8080 to container 80 --name mynginx container name mynginx -v/myDockerData /nginx/log/:/var/log/ nginx mappinglogFile directory - v/mydockerdata/nginx/nginx. Conf: / etc/nginx/nginx. Conf mapping configuration file - v/mydockerdata/nginx/dist / : / usr/share/nginx/HTML Map the directory for storing web pages-dNginx images run in the backgroundCopy the code

Note that the local IP is 8080, the container is 80 (ports 80 in multiple containers do not conflict with each other, because docker containers are isolated), and external access is through 8080

The nginx.conf configuration in the container can be modified by modifying the native nginx.conf configuration (because of mapping).

vim /mydockerdata/nginx/nginx.conf
Copy the code

Add a server under the HTTP node

server { listen 80; Server_name keep999.cn; Root/usr/share/nginx/html; # item path index index. HTML; # Any route that doesn't have a file extension (e.g. /devices) location / { try_files $uri $uri/ /index.html; }}Copy the code

Press ESC, :wq to save and restart mynginx

docker restart mynginx
Copy the code

There you go. Visit http://47.108.59.73:8080 or http://keep999.cn:8080 to get to the site

2) Use the same method as 1) to create/myDockerData /web_nginx directory to manage your friends’ websites

**/var/www/html** the path to your friend’s site is under **/var/www/html**, so the path map should be changed when creating the container

docker run -p 8081:80 --name web_nginx -v /mydockerdata/web_nginx/log/:/var/log/nginx -v /mydockerdata/web_nginx/nginx.conf:/etc/nginx/nginx.conf -v /var/www/html:/usr/share/nginx/html -d nginx
Copy the code
server {
        listen       80;         Listen to the port
        server_name  keep999.cn;    # listen to the URL
        root  /usr/share/nginx/html;           # Project path
        index index.html;
        # Any route that doesn't have a file extension (e.g. /devices)
        location / {
            try_files $uri $uri/ /index.html; }}Copy the code

The same goes for configuration files, which are best kept separate for both projects

Once created, you can visit his website at http://47.108.59.73:8081

3) create proxy_nginx to proxy and secondary domain forwarding

Create nginx management directory is the same, / myDockerdata /proxy_nginx directory to manage

Kill -9 PID, and then create our container

docker run -p 80:80 --name proxy_nginx -v /mydockerdata/proxy_nginx/log/:/var/log/nginx -v /mydockerdata/proxy_nginx/nginx.conf:/etc/nginx/nginx.conf -v /mydockerdata/proxy_nginx/dist/:/usr/share/nginx/html -d nginx
Copy the code

The configuration files are important here

    server {
        listen       80;         Listen to the port
        server_name  a.keep999.cn;    # listen to the URL
        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://47.108.59.73:8080;
        }
    }
    server {
        listen       80;         Listen to the port
        server_name  keep999.cn;    # listen to the URL
        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://47.108.59.73:8081; }}Copy the code

Forward a.keep999.cn to my project. My project access is http://47.108.59.73:8080

Forward keep999.cn to his project, whose project access is http://47.108.59.73:8081

Docker restart proxy_nginx

The entire directory tree structure/myDockerdata/under

│ ─ ─ ─ mynginx my project | ├ ─ ─ - dist | ├ ─ ─log. | │ ├ ─ ─ the access log | │ └ ─ ─ the error. The log | └ ─ ─ nginx. Conf ├ ─ ─ proxy_nginx agent │ ├ ─ ─ dist │ ├ ─ ─log│ │ ├ ─ ─ access. Log │ │ └ ─ ─ the error. The log │ └ ─ ─ nginx. Conf └ ─ ─ web_nginx friends project ├ ─ ─ dist ├ ─ ─log│ ├─ ├─ download.txt ├─ download.txtCopy the code

Second, use domain name resolution

The second is simple, using Aliyun’s cloud resolution directly.

First, as in (1), generate the nginx container for my project and make keep999.cn:8080 accessible to my project

Then add cloud resolution, [hidden URL]

In this case, the project can be accessed through b.keep999.cn.

But why isn’t it recommended? Because my friend’s server nginx and Docker are of the same level, it should be handed over to Docker to create containers, which is convenient for management and does not occupy port 80, which is more maneuverable. It is also able to better understand and master the relationship between Docker and container, and the relationship between Docker and the machine. To learn more about Docker and its microservices.

conclusion

Nginx is still pretty powerful, and it does a lot more than that.

It has three main functions: reverse proxy, load balancing and static resource server. We have used reverse proxy and static website here, but have not used load balancing. To put it simply, load balancing means that multiple servers distribute requests to different servers based on the configured policy to share the load of each server. Those who are interested can try it out for themselves.

In the next article, we will continue to add HTTPS to web access, learn how to apply for free certificates and use Nginx to configure SSL, see Docker nginx HTTPS secondary domain name to access multiple Web projects

About the author

The author is a programmer who loves learning, open source, sharing, spreading positive energy, likes playing basketball, and has a lot of hair. –

Warmly welcome everyone to pay attention to, like, comment exchange!

Jane: www.jianshu.com/u/d234d1569…

github:github.com/fly7632785

CSDN:blog.csdn.net/fly7632785

The Denver nuggets: juejin. Cn/user / 993614…