Nginx will assign you less stress to the server to access

1. Reverse proxy

When we have a server cluster, and the content of each server in the server cluster is the same, we cannot access the server cluster directly from the PC, we have to access the cluster through the third party service period. At this time, we access the contents of the server cluster through a third-party server, but we do not know which server provides the content. This proxy method is called reverse proxy.

2. Load balancing

Company will establish a lot of server, the server of the cluster servers, and then, when users visit the web site to access an intermediate server, let this middle server in the server in the cluster to select a server less pressure, and then introduces the access request to choose the server, so every time the user access, All servers in a server cluster are balanced to prevent server crashes.

3. Reverse proxy and load balancing

When a user visits a web site, the nGINx server first visits the nginx server, and then the NGINx server selects a less stressed server from the server cluster and directs the access request to that server

Nginx configuration

Install nginx

  1. Go to homebrew’s official website and copy the commands to pre-install what you need
  2. Brew install nginx Install nginx
  3. Nginx -v Displays the version number to enter nginx
  4. cd/usr/local/etc/nginx

Nginx common commands

  1. Enable nginx access to localhost:8080(default)
  2. Stop nginx nginx-s stop
  3. Nginx -s reload nginx -s reload nginx
  4. Nginx -t check whether the nginx.conf configuration is correct
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok 
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Copy the code

4.proxy_pass

Nginx reverse proxy is mainly configured through proxy_pass. Fill the developer address of your project after proxy_pass. The normal format is proxy_pass URL

server { listen 80; Location / {proxy_pass http://10.10.10.10:20186; }}Copy the code

5.Upstream module implements load balancing

  1. Ip_hash directive
  2. Server instructions
  3. Upstream directive and related variables
worker_processes 1; events { worker_connections 1024; } HTTP {upstream firstdemo {server 39.106.145.33; Server 47.93.6.93; } server { listen 8080; location / { proxy_pass http://firstdemo; }}}Copy the code
  • Worker_processes: indicates the number of working processes, which is the same as the number of CPU cores
  • Worker_connections: Maximum number of links allowed per process
  • Upstream module:
    • Implement reverse proxy
    • Listen Monitors the port number
    • Locaiton / {} Access and path
    • Proxy_pass http://firstdemo, proxy to two servers in firstDemo
  • Restart the nginx
  • Access localhost: 8080

Each refresh is accessed to a different server, thus achieving load balancing