Easy Tutorial
❮ Cpp Difference Between Pointers And References Ssh Slow ❯

This is a Chinese to English translation, please provide the English translation for this text. Do not provide any explanations or text apart from the translation.

Nginx Configuration Detailed Explanation

*Category Programming Technology

Preface

Nginx was designed and developed by Igor Sysoev for the second most visited site in Russia, rambler.ru. Since its release in 2004, thanks to the power of open source, it has nearly reached maturity and perfection.

Nginx is rich in features, can be used as an HTTP server, a reverse proxy server, and a mail server. It supports FastCGI, SSL, Virtual Host, URL Rewrite, Gzip, and many other features. It also supports many third-party module extensions.

Nginx's stability, feature set, example configuration files, and low system resource consumption have allowed it to rise to prominence, with a usage rate of 12.18% among active websites worldwide, approximately 22.2 million websites.

The praise is almost over, if you are not satisfied enough, you can find such praise on Baidu Baike or some books, which are everywhere.


Common Nginx Features

  1. Http proxy, reverse proxy: One of the most commonly used features of web servers, especially reverse proxy.

Here I provide 2 pictures to interpret the forward proxy and reverse proxy, for specific details, you can refer to the materials.

When Nginx acts as a reverse proxy, it provides stable performance and flexible forwarding configuration. Nginx can adopt different forwarding strategies based on different regular matches, such as files ending with images to the file server, dynamic pages to the web server, as long as your regular expression is correct, and you have a corresponding server solution, you can play as you wish. And Nginx also performs error page redirection, exception judgment, etc. If the distributed server has an exception, it can forward the request to another server and automatically remove the exception server.


  1. Load balancing

The load balancing strategies provided by Nginx are two types: built-in strategies and extended strategies. The built-in strategy is round-robin, weighted round-robin, and IP hash. Extended strategies are boundless, only you can't think of it, there is nothing you can't do, you can refer to all load balancing algorithms, find them one by one to implement.

Three pictures are shown to understand the implementation of these three load balancing algorithms.

The IP hash algorithm performs a hash operation on the client's IP request, and then distributes the requests from the same client IP to the same server for processing, which can solve the problem of session non-sharing.


  1. Web caching

Nginx can perform different caching processing for different files, flexible configuration, and supports FastCGI_Cache, mainly used for caching dynamic programs of FastCGI. In conjunction with the third-party ngx_cache_purge, the cache content of the specified URL can be managed for addition and deletion.


  1. Nginx-related addresses

Source code: https://trac.nginx.org/nginx/browser

Official website: http://www.nginx.org/


Nginx Configuration File Structure

If you have downloaded it, you might as well open the nginx.conf file in the conf folder. The basic configuration of the Nginx server is also stored here.

The comment symbol in nginx.conf is: #

The content of the default nginx configuration file nginx.conf is as follows:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html
#use epoll; #Event-driven model, select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #Maximum number of connections, default is 512
}
http {
    include       mime.types; #File extension to file type mapping table
    default_type  application/octet-stream; #Default file type, default is text/plain
    #access_log off; #Disable service log    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #Custom format
    access_log log/access.log myFormat; #combined is the default value for log format
    sendfile on; #Allow sendfile method for file transfer, default is off, can be in http block, server block, location block.
    sendfile_max_chunk 100k; #The amount of data transferred each time by a process cannot be greater than the set value, default is 0, which means no limit.
    keepalive_timeout 65; #Connection timeout, default is 75s, can be in http, server, location blocks.

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #Hot backup
    }
    error_page 404 https://www.baidu.com; #Error page
    server {
        keepalive_requests 120; #The upper limit of requests per connection.
        listen       4545;   #Listening port
        server_name  127.0.0.1;   #Listening address       
        location  ~*^.+$ {       #Request URL filtering, regular match, ~ is case-sensitive, ~* is case-insensitive.
           #root path;  #Root directory
           #index vv.txt;  #Set the default page
           proxy_pass  http://mysvr;  #Request redirected to the server list defined in mysvr
           deny 127.0.0.1;  #Denied IP
           allow 172.18.5.54; #Allowed IP           
        } 
    }
}

The above is the basic configuration of nginx, and the following points should be noted:

  1. Several common configuration items:
  1. The thundering herd problem: when a network connection arrives, multiple sleeping processes are awakened at the same time, but only one process can obtain the connection, which can affect system performance.

  2. Each directive must end with a semicolon.

>

Original article: https://www.cnblogs.com/knowledgesea/p/5175711.html

** Click to share notes

Cancel

-

-

-

```

❮ Cpp Difference Between Pointers And References Ssh Slow ❯