Function module of the Nginx Server

Source: Internet
Author: User
Tags nginx server

Function module of the Nginx Server
Differences between Nginx and Apache

1. Advantages of Nginx compared with Apache

For this question, it is difficult to say which one is better. They all have advantages and disadvantages. For example, Apache provides hundreds of modules, and modules mean functions, however, as an Apache server, it has a maximum of 2000 concurrent threads, and Nginx provides dozens of modules, but it can provide 20000 concurrent connections. Apache has many functions, but Nginx has few functions, but there is a good explanation:

Apache is like Office. It has hundreds of thousands of options, but you only need six options. Nginx only provides these six functions, however, these six tasks are basically 50 times that of Apache. -- Chris Lea, ChrisLea.com

2. From the perspective of Nginx accepting client request processing, it is different from Apache

Nginx adopts the event-driven structure. It uses asynchronous request sockets to accept requests. It is a non-blocking structure and does not apply to individual county-level processing. It aims to reduce memory and CPU overhead. Apache uses synchronous sockets, threads, and processes. Each request is processed by a separate process and line city.

Function module of the Nginx Server

1. Traffic limit
For websites that provide download, the current value of traffic is required. In Nginx, we can limit the traffic. The Core module provides the limit_rate and limit_rate_after commands.

(A) command limit_rate
Command name: limit_rate
Environment: http, server, location, if in location
Default Value: no

This command is used to specify the speed at which data is transmitted to the client. The unit of speed is the number of bytes transmitted per second. This command only sets a connection. That is to say, if two connections are connected at the same time, their speed will be twice the speed set by this command, just as our bandwidth is.

If you need to restrict the client connection at the server level, in this case-this command may not be suitable, but you can set the $ limit_rate variable to pass the corresponding value for the variable, for example:

server{ if($slow){ set $limit_rate 4k; }}

(B) command limit_rate_after
Command name: limit_rate_after
Environment: if field in http, server, location, and if in location
Default Value: limit_rate_after 1 m

This command is "in... After the speed limit is ...", This means that the corresponding operation is performed after the download time is the maximum speed. However, in actual use, the parameter limit_rate_after is a size value of the downloaded bytes, not a time value, for example, limit_rate_after 3 M indicates the maximum download speed after 3 MB.

For example:

location /download{ limit_rate_after 3m; limit_rate 512k;}

When a client is connected, it will download 3 MB at the fastest speed and then download at K.

2. Limit the number of concurrent connections of users
The limit_zone module can be used to limit the number of connections of users and limit the number of concurrent connections of the same user IP address.
This module provides two Commands: limit_zone and limit_conn, where limit_zone can only be in the http segment, and limit_conn can be used in the http, server, and location segments.

Example:

http{ limit_zone one $binary_remote_addr 10m; server{ location /download/{ limit_conn one 1; } }}

(A) command limit_zone
Command name: limit_zone
Syntax: limit_zone zone_name $ variable memory_max_size
Environment: http
Default Value: no

This command is used to define a zone, which will be used to store the session status. The number of sessions that can be stored is determined by the distribution of the delivered variable and the size of memory_max_size.
For example:
Limit_zone one $ binary_remote_addr 10 m;
The IP address of the client is used as the session. Note: Binaryremoteaddr instead Remote_addr, because the length of remote_addr is 7-15 bytes, and the length of painting information is 32 bytes. When a zone of 1 MB is set, if binary_remote_addr is used, the zone will store 32000 sessions.

(B) command limit_conn
Command name: limit_conn
Syntax: limit_conn zone_name max_clients_per_ip
Environment: http, server, location
Default Value: no

This command is used to set the maximum number of concurrent connections for a session. If the number of initiated requests exceeds this limit, a 503 error will occur.
For example:

limit_zone one $binary_remote_addr 10m;server{ location /download/{ limit_conn one 1; }}

This setting will make the number of concurrent connections between the two words and the same ip address cannot exceed 1.

(B) command limit_conn_log_level
Command name: limit_conn_log_level
Syntax: limit_conn_log_level log_level info | notice | warn | error
Environment: http, server, location
Default Value: error
This command is used to set the error level. When the connection reaches the limit, an error log is generated.

Comprehensive instance:
worker_progresses 4;event{ worker_connection 10240;}http{ include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; limit_zone flv_down $binary_remote_addr 10m; server{ listen 80; server_name flv.abc.com; .... location /download{ limit_conn flv_down 1; } } error_page 500 502 503 504 /50x.html; location =/50x.html{ root html; }}

1. Use the limit_zone command to define a zone as flv_down

limit_zone flv_down $binary_remote_addr 10m;

Flv_down: The name of a zone;
Binaryremoteaddr: a variable used to differentiate between clients. You can also select other variables to differentiate, but the most typical one is Binary_remote_addr. Here, IP addresses in binary format are used, which is more efficient than ASCII format.
10 m: used to limit the size

2. Use the limit_conn command to implement the zone defined by the limit_zone command. 1 indicates the number of simultaneous connections.

limit_conn flv_down 1;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.