Tell me what you know about Nginx

Analysis & Answers

nginx performance data

  • High concurrent connections: Officially, a single node supports 50,000 concurrent connections, and the actual production environment can withstand 20,000 to 30,000 concurrent connections.
  • Low memory consumption: under 30,000 concurrent connections, starting 10 nginx processes only consumes 150M of memory (15M×10=150M)

1. Forward and reverse proxy

The so-called "proxy" refers to setting up a hardware/software forwarding request at the edge of the internal network; "forward" or "reverse" depends on whether the forwarding is an "outbound request" or an "inbound request".

  • Forward proxy : Handles outbound requests from the client, forwards them to the Internet, and returns the resulting response to the client. 
  • Reverse proxy : Handles inbound requests from the Internet, forwards them to a backend worker, and returns the response to the Internet.

  1. Forward proxies and reverse proxies differ in the direction of the proxy, but both proxy to handle HTTP requests/responses.
  2. The purpose of proxy server existence:
    • Bastion host/isolated intranet: Intranet clients cannot access the external network and need to set up a bastion host and hide the intranet working server
    • Proxy server additional features: perform operations on traffic, use caching or compression to improve performance, defend against attacks, and filter information

2. Load balancing

Load balancing is generally accompanied by reverse proxy, which has the effect of distributing traffic, transparent proxying, and enhancing fault tolerance.

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}
复制代码

load balancing strategy

  • round-robin As the name suggests: polling
  • least-connected: The next request will be sent to the server with the least active connections
  • ip-hash: Determine which server to send the request to based on the client's IP address and hash function

3. Separation of movement and static

Dynamic and static separation is closely related to the current hot concept of front-end and back-end separation.

The front-end can be developed and tested by itself, and nginx can be used to form a static resource server. The back-end service is only used as an additional resource.

The following example shows that the static resources are in /usr/share/nginx/html, and the dynamic resource path contains api or swagger.

  upstream eap_website {
      server eapwebsite;
    }

  server {
      listen      80;
      location / {            # 静态资源
            root /usr/share/nginx/html;
            index index.html index.htm;
            try_files $uri /index.html;
      }

      location ^~ /api/  {     # 动态资源
         proxy_pass         http://eap_website/api/;
      }

      location ^~ /swagger/  {    # 动态资源
         proxy_pass         http://eap_website/swagger/;
      }
  }

Meow Interview Assistant: A one-stop solution to interview questions. You can search the WeChat applet [Meow Interview Assistant]  or follow [Meow Interview Assistant] -> Interview Assistant to  answer questions for free. If you have any good interview knowledge or skills, I look forward to sharing them with you!

Guess you like

Origin blog.csdn.net/jjclove/article/details/127394245