Personal learning series – getting started with nginx

Time:2020-1-7

Recently, I paid attention to the distributed Dubbo and spring cloud. Dubbo simply made a summary. Spring cloud has not yet carried out systematic learning, and I will write an article about it after a period of learning.
The introduction of Baidu Encyclopedia
Nginx(engine x) is a high-performance HTTP and reverse proxy web server, and also provides IMAP / POP3 / SMTP services.

Simply put, Dubbo’s load balancing is at the service level, and nginx’s load balancing is at the HTTP request level.

Simple implementation of nginx

Nginx download and installation

First of all, I use docker for operation. After all, docker is very popular now, and it’s easy to build.
I use the docker built by CentOS 8 here. You can take a look at the docker tutorial I wrote before.

Docker installation nginx

  • First, we can look at the nginx version in docker:
    Personal learning series - getting started with nginx
  • Download the image of nginx:
    Personal learning series - getting started with nginx
  • Start nginx:
    Since the docker cannot start the nginx service directly after starting the nginx container, you need to go to / usr / SBIN / start nginx.
    Personal learning series - getting started with nginx
    In this way, when we visit the CentOS address, we can see that nginx has been started:
    Personal learning series - getting started with nginx

Here we may find that this page is not displayed after we visit the address, but the displayed page is unable to connect. In fact, we do not open port 80 of CentOS, we just need to close the firewall.

1. View firewall status
     firewall-cmd --state
  2. Stop firewall
     systemctl stop firewalld.service
  3. Disable the startup of firewall
     systemctl disable firewalld.service

Simple application of nginx

First, we need to download two Tomcat when implementing nginx application, so we should start two Tomcat in docker.
Personal learning series - getting started with nginx
Start two Tomcat ports 8080 and 8081
Personal learning series - getting started with nginx
We need to go into the container and start Tomcat
Personal learning series - getting started with nginx
So we have started both Tomcat
Personal learning series - getting started with nginx
Personal learning series - getting started with nginx

Reverse proxy

Reverse proxyServer hiddenInformation!
Personal learning series - getting started with nginx
The function we want to achieve is to access the address of the virtual machine, and then through nginx reverse proxy to Tomcat in 8080.
Now we visit the address of the virtual machine or display the welcome page of nginx:
We enter the / etc / nginx folder of the nginx container to modify nginx.conf:
Personal learning series - getting started with nginx

  • Install VIM editor first

    apt-get update
    apt-get install vim
  • To edit:
    Personal learning series - getting started with nginx

    server {
     Listen 80; port number
     Server name 192.168.233.128; identification path of virtual server
         location  ~ /edu/ {
             root html;
             Proxy pass http://192.168.233.128:8080; proxy address
             index index.html index.html;
     }
         location  ~ /vod/ {
             root html;
             Proxy pass http://192.168.233.128:8081; proxy address
             index index.html index.html;
     }
    }
  • Start ngins:

    Restart nginx
    service nginx restart   
    start nginx
    ./nginx
    Stop nginx
    ./nginx -s stop
  • View page:
    Personal learning series - getting started with nginx
    Personal learning series - getting started with nginx

Nginx load balancing

  • First, you need to configure the same file in two Tomcat:
    We need to create a test folder in even tomcat, and then create a new HTML file with the same name. Here we still call it a.html.
    Tomcat server for 8080:
    Personal learning series - getting started with nginx
    Personal learning series - getting started with nginx
    Tomcat server for 8081:
    Personal learning series - getting started with nginx
    Personal learning series - getting started with nginx
  • Configure nginx.conf
    First, we need to create a new upstream module:
    Personal learning series - getting started with nginx
    Then we need to configure the relevant information in the server. The proxy pass here needs to use the upstream module above:
    Personal learning series - getting started with nginx
    Start or restart nginx. Here we use the default polling:
1. Polling (default)
  Each request is allocated to different back-end servers in chronological order. If the back-end servers are down, they can be automatically eliminated.
2.weight
  Specifies the probability of polling, weight is proportional to the access ratio, and is used in the case of uneven performance of the back-end server.
  upstream myserver {
    server 192.168.233.128:8080 weight 5;
    server 192.168.233.128:8081 weigth 10;
  }
3. ip_hash
  Each request is allocated according to the hash result of the access IP, so that each visitor accesses a back-end server.
  upstream myserver {
    ip_hash;
    server 192.168.233.128:8080;
    server 192.168.233.128:8081;
  }
4. Fair (third party)
  Requests are allocated according to the response time of the back-end server, and those with short response time are allocated preferentially. Similar to the weight allocation strategy.
  upstream myserver {
    server 192.168.233.128:8080;
    server 192.168.233.128:8081;
    fair;
  }

Test (passed):
Personal learning series - getting started with nginx
Personal learning series - getting started with nginx