Nginx

What is?

Nginx is a high-performance HTTP and reverse proxy server

Why?

  1. Less memory, strong concurrency.
  2. Nginx was developed specifically for performance optimization, and performance is its most important requirement, with a strong focus on efficiency
  3. security

How to use

Common commands
  • Start the Nginx

    • /usr/sbin/nginx
    • nginx
  • Stop Nginx

    • nginx -s stop
  • View the Nginx process

    • ps -ef|grep nginx
  • Smooth start nginx

    • nginx -s reload
    • kill -HUP cat /var/run/nginx.pid
  • Forcibly stop nginx

    • pkill -9 nginx
  • Check that the changes to the nginx.conf file are correct

    • nginx -t -c /etc/nginx/nginx.conf
    • nginx -t
  • View the version information of nginx

    • nginx -v
Configuration file (nginx.conf)
  • Main configuration segment: global configuration segment. The main configuration segment may contain the Event configuration segment.

    • Configuration parameters for debugging and locating problems:
      • daemon {on|off}; // Whether the daemon is running the nginx process, set to off during debugging
      • master_process {on|off}; // Whether to run nginx as the master/worker model
      • Error_log Position 1 Level 2; // Configure error logs (click footnote for location and level)
    • Configuration parameters required for normal operation
      • worker_processse N; // Start N worker processes, where N is usually set to total CPU cores -1 or equal to total T cores in order to avoid context switching
      • Worker_cpu_affinity CPUMASK3 [CPUMASK…]. ; // Bind the process to a CPU to avoid frequent cache flushing
      • time_resolution INTERVAL; // Timer resolution. Lower this value to reduce the number of getTimeofday () system calls
      • worker_priority NUMBER; // Specify the NICE value of worker processes (priority)
    • Event-related Configuration
  • Event {} : Defines the event model working features;

    • use epoll; // A way of multiplexing I/O, used only for linux2.6 and above, to greatly improve nginx performance
    • accept_mutex {on|off}; // Load balancing lock used when master schedules user requests to worker processes. “on” means that multiple workers can respond to new requests in turn and serialize
    • lock_file FILE; // Accept_nutex specifies the path to the mutex lock file
    • use [epoll | rtsig | select | poll]; // Specify the event model to use. It is recommended that nginx choose its own
    • worker_connections #; // The maximum number of connections each process can accept
    • multi_accept on; Accept as many requests as possible
  • HTTP {} : defines HTTP configurations.

    • Server {} : Defines a virtual host
    • The Location section matches the URI requested by the client by specifying a pattern
      • Function: Allows users to match each location defined according to the URI requested by the user. When matching, the request will be processed by the configuration in the response location configuration fast, such as access control and other functions
      • Location [modifier] = ABC {… }
        • The modifier
          1. = Exact match
          2. ~ Regular expression pattern matching, case sensitive
          3. The ~* regular expression pattern matches and is case insensitive
          4. ^~ prefix matching, similar to no modifier behavior, also starts with the specified module, except that if the pattern matches, the search for other patterns stops. Regular expressions are not supported
          5. @ defines the name of the location section. These sections are not accessible by clients, only by internally generated requests such as try_files or error_page
          6. asd/asdas
        • Rewrite REGEX REPLACEMENT FLAG (rewrite REGEX REPLACEMENT FLAG;).
          • REPLACEMENT can be a path
          • FLAG
            • This is the last FLAG used to indicate the end of the rewrite rule and the next match (10 to 20 at most). Instead, the UserAgent initiates the request again for the rewritten URL and performs a similar process from the beginning
            • Break — The Rewrite rule is stopped from matching. Once the Rewrite rule has been rewritten, the userAgent will re-request the URL without being checked by any Rewrite rules in the current location
            • Redirect — The HTTP state that has been temporarily redirected 302 returns the new URL
            • Permanent – Returns a new URl permanently from the directed HTTP state 301
          • Identifier REGEX
            • ^ must begin with the entity after ^
            • Must start with the entity that must precede must
            • Matches any character
            • [] Matches any character in the specified character set
            • [^] Matches any arbitrary string not included in the specified character set
            • | | matching entities before or after
          • () group, a group of entities to match, there is often a | to assist
    HTTP {## include mime.types; default_type application/octet-stream; keepalive_timeout 65; gzip on; sendfile on; ## Specifies whether nginx calls sendFile (zero copy) to output files. For common applications, it must be set to on. If it is used for heavy load applications such as disk I/O download, it can be set to OFF to balance disk and network I/O processing speed and reduce the uptime autoindex on system. Upstream {## load balancing configuration is disabled. } server {## server level, each server is like a <VirtualHost > in HTTPD listen 8080; server_name localhost; ## can be followed by multiple hosts, the name can be a regular expression or wildcard ## multiple servers, matching order as follows: ##1 Do accurate match check first; Left wildcard match check, e.g. *.idfsoft.com ##3. * *\. Idfsoft\.com $##5. Default_server location / {## request level, similar to < location > in HTTPD, Used to define the mapping between URL and local file system root HTML; index index.html index.htm; Allow 192.168.91.129/32; ## set which host or hosts are allowed to access, multiple parameters are separated by Spaces; # set which host or host is not allowed to access, multiple parameters separated by Spaces}}}Copy the code
Load balancing, static and static separation, reverse proxy, forward proxy

Load balancing: Increase the number of servers, build a cluster, and distribute requests to multiple servers instead of centralized requests to a single server

Reverse proxy: The client cannot sense the proxy because the client does not need to be configured to access the network. The client only needs to send the request to the reverse proxy server. The reverse proxy server selects the destination server to obtain data and then returns the request to the client.

Forward proxy: It is not feasible for computer users on the LAN to access the network directly. They can only access the network through a proxy server. This proxy service is called forward proxy.

Static and dynamic separation: in order to speed up the speed of website parsing, dynamic pages and static pages can be handed over to different servers to parse, speed up the speed of parsing, reduce the pressure of a single server.