Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common functions of Nginx

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the common functions of Nginx". In daily operation, I believe many people have doubts about the common functions of Nginx. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the common functions of Nginx?" Next, please follow the editor to study!

Nginx was designed and developed by lgor Sysoev for the second most visited rambler.ru site in Russia. Since its release in 2004, with the power of open source, it has been close to maturity and perfection. Nginx has rich functions and can be used as a HTTP server, a reverse proxy server and a mail server. Support FastCGI, SSL, Virtual Host, URL Rewrite, Gzip and other functions. And support many third-party module extensions.

Common functions of Nginx

1. Http proxy, reverse proxy

:

As one of the most commonly used functions of web server, especially reverse proxy.

Here I give 2 pictures, to the forward agent and response agent to do an interpretation, details, you can refer to the next information.

When doing reverse proxy, Nginx provides stable performance and flexible forwarding function. Nginx can adopt different forwarding strategies according to different regular matches, such as the file server at the end of the picture file and the web server for dynamic pages. As long as you can write regularly and have a corresponding server solution, you can play as much as you like. And Nginx carries on the error page jump, exception judgment and so on. If there is an exception on the distributed server, he can reforward the request to another server and automatically remove the exception server.

2. Load balancing

There are two kinds of load balancing strategies provided by Nginx: built-in strategy and extended strategy. The built-in policies are polling, weighted polling, Ip hash. Expansion strategy, only you can not think of nothing he can not do, you can refer to all the load balancing algorithms, give him one by one to find out to do the implementation.

Understand the implementation of these three load balancing algorithms in the above three diagrams

Ip hash algorithm, hash the ip requested by the client, and then distribute the request of the same client ip to the same server for processing according to the hash result, which can solve the problem of session non-sharing.

3. Web cache

Nginx can do different cache processing for different files, the configuration is flexible, and supports FastCGI_Cache, mainly used for caching dynamic programs of FastCGI. With the third-party ngx_cache_purge, the content of the URL cache can be added or deleted.

4. Nginx related address

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

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

Nginx profile structure

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

Comment symbol bit # in nginx.conf

The structure of the nginx file, for beginners, you can take a look at it.

Nginx file structure

... # global block events {# events block...} http # http block {... # http global block server # server block {... # server global block location [PATTERN] # location block {...} location [PATTERN] {...}} server {...}... # http Global Block}

1. Global block: configure directives that affect the global nginx. Generally, there are user groups running nginx server, pid storage path of nginx process, log storage path, introduction of configuration files, number of worker process allowed to be generated, and so on.

2. Events block: the configuration affects the nginx server or the network connection with the user. There is a maximum number of connections per process, which event-driven model is selected to handle connection requests, whether multiple network connections are allowed to be accepted at the same time, serialization of multiple network connections is enabled, and so on.

3. Http blocks: you can nest multiple server, configure most functions such as proxy, cache, log definition, and configure third-party modules. Such as file introduction, mime-type definition, log customization, whether to use sendfile to transfer files, connection timeout, the number of requests for a single connection, etc.

4. Server block: configure the relevant parameters of the virtual host. There can be multiple server in a http.

5. Location block: configure the routing of the request and the processing of various pages.

The following is to give you a configuration file, as an understanding, but also into a test machine I built, to give you an example.

# each instruction must end with a semicolon. # user administrator administrators; # configure users or groups. Default is nobody nobody. # worker_processes 2; # the number of processes allowed to be generated. The default is 1#pid / nginx/pid/nginx.pid; # to specify the address where the nginx process runs files to store, error_log log/error.log debug; # to determine the log path and level. This setting can be put into the global block, http block, server block, and the level is: debug | info | notice | warn | error | crit | alert | emergevents {accept_mutex on; # sets the serialization of network connections to prevent the occurrence of panic phenomena. The default is on multi_accept on; # to set whether a process accepts multiple network connections at the same time. The default is off # 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 and File Type Mapping Table default_type application/octet-stream; # default file type, default is text/plain # access_log off; # unservice 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 of log format sendfile on; # allows sendfile to transfer files, default is off, can be in http block, server block, location block. Sendfile_max_chunk 100k; # the number of transfers per call per process cannot be greater than the set value. The default is 0, that is, there is no upper limit. Keepalive_timeout 65; # connection timeout. The default is 75s, which can be found in the http,server,location block. Upstream mysvr {server 127.0.0.1server 7878; server 192.168.10.121 backup; # Hot standby} error_page 404 https://www.baidu.com; # error page server {keepalive_requests 120; # maximum number of single connection requests. Listen 4545; # listening port server_name 127.0.0.1; # listening address location ~ * ^. + ${# request url filtering, regular matching, ~ is case-sensitive, ~ * is case-insensitive. # root path; # Root directory # index vv.txt; # set default page proxy_pass http://mysvr; # request to mysvr defined server list deny 127.0.0.1; # rejected ip allow 172.18.5.54; # allowed ip}

Above is the basic configuration of nginx. You should pay attention to the following points:

1. 1.$remote_addr and $http_x_forwarded_for are used to record the ip address of the client; 2.$remote_user: to record the client user name; 3.$time_local: to record the access time and time zone; 4.$request: the url and http protocol used to record the request

5.$status: used to record the status of the request; 6.$body_bytes_s ent: record the size of the main content of the file sent to the client; 7.$http_referer: used to record the access from that page link; 8.$http_user_agent: record the relevant information of the client browser

2. Shock phenomenon: when a network connection arrives, multiple sleep processes are awakened by colleagues, but only one process can get the link, which will affect the performance of the system.

3. Each instruction must end with a semicolon.

At this point, the study on "what are the common functions of Nginx" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report