Nginx's HTTP Module

Source: Internet
Author: User

1.HTTP Core module.
These HTTP modules are compiled automatically when you compile Nginx, unless you use the Configure command to disallow compilation of these modules.
(1) Alias instruction.
This directive is used to implement mappings between URLs and file system paths. It is similar to the root command, but the root directory of the Web page file does not change, but only the file system path that requests the URL.
Syntax: Alias file-path| Directory-path;
Example: location/i/{alias/spool/w3/images/;}
In the example, the URL address "/i/top.gif" is accessed, and the file "/spool/w3/images/top.gif" is returned
(2) client_body_in_file_only instruction.
Syntax: Client_body_in_file_only on|off
This directive allows the content of a client's request to be logged to a file that is not deleted after the request is completed. In built-in Perl, this directive can be used to debug the $r->request_body_file method.
(3) Client_body_in_single_buffer directive
Syntax: Client_body_in_single_buffer
This directive specifies whether to keep the entire content in a single client request buffer, which is recommended for use with variable $request_body.
(4) Client_body_buffer_size directive
Syntax: Client_body_buffer_size the_size
Example Client_body_buffer_size 128k
This directive specifies the buffer size of the client request content. If the client requests content that is larger than the buffer, the entire request content or part of the request content will be written to the temporary file. The default size of the buffer is twice times the size of the page, which is 8k or 16k.
(5) Client_body_temp_path directive
Syntax: Client_body_temp_path dir-path [LEVEL1[LEVEL2[LEVEL3]
This directive is used to specify the directory where the requested content temporary files are stored. The cache directory supports up to 3 levels of subdirectories.
Example: Client_body_temp_path/spool/nginx/client_temp 1 2;
The above example has a directory structure similar to the following:
/spool/nginx/client_temp/7/45/xxxxx
(6) Client_body_timeout directive
Syntax: Client_body_timeout time
This directive is used to set the time-out for reading the contents of a client request. If the time of the instruction is exceeded, Nginx will return the "request" error message (HTTP status code 408)
(7) Client_header_buffer_size directive
Syntax: Client_header_buffer_size size
This directive is used to set the header buffer size for client requests. The 1KB size header buffer is sufficient for most requests, but may not be enough for requests with larger cookie content.
(8) Client_header_timeout instruction.
Syntax: Client_header_timeout time
This instruction is used to set the time-out period for reading the header information of the client requesting header, and if it exceeds the time set by the instruction, Nginx will return the "Request timed out" error message (HTTP status code 408)
(9) Client_max_body_size directive
Syntax: Client_max_body_size size
Example: Client_max_body_size 300m
This directive is used to set the maximum value of the content that is allowed to be accepted by the client, that is, the maximum value of the Content-length set in the header information of the client. If the maximum value set by this directive is exceeded. Nginx will return "Request Entity Too Large" Error message (HTTP status code is 413). When the default value is 1MB, if the Nginx server provides operations such as uploading large files above 1MB, increase the value.
(Ten) Default_type directive
Syntax: Default_type mime-type
Mime-type is the file media type used to tell the browser to request, for example: Mime-type name Text/plain indicates that the file is a text file, text/ HTML indicates that the file is an HTML Web page file. If Nginx does not recognize what type of Mime-type the file belongs to, the file is marked as Default_type instruction set Mime-type
(one) error_page Code[code ...] [=|=answer-code] Uri
This instruction is used to set the corresponding URI address that is displayed to the client if a specified HTTP error status code appears.
Example: Error_page 404/404.html;
() index directive
Syntax: index file [file ...]
This directive is used to set Nginx's default home page file.
Index index. $geo. HTML index.0.html/index.html
(+) Internal directive
This instruction is used to set a location path that can only be used inside nginx and cannot be accessed externally.
location/404.html {internal;}
(+) Keepalive_timeout [TIME] directive
This instruction makes the client-to-server connection persistent and the Keep-alive feature avoids establishing or re-establishing a connection when a subsequent request to the server occurs. Most Web servers on the market, including IIS,APACHE,NGINX,LIGHTTPD, support HTTP Keep-alive. This feature is useful for websites that provide static content, which is harder for heavy-weight sites.
(keepalive_requests) directive
Syntax: keepalive_requests n
Sets the number of times a keep-alive connection is used. After a request has been completed, if the number of times that the connection is used does not exceed the number of requests set by the keepalive_requests instruction, the server does not immediately disconnect, but until the keepalive_ is reached Timeout instruction to set the time before closing the connection.
(+) large_client_header_buffers directive
This directive is used to set the header buffer size for client requests, which defaults to 4KB. The client request line cannot exceed the value set by the large_client_header_buffers instruction. Header header information for client requests cannot be greater than large_client_ The header_buffers instruction sets the buffer size, otherwise the 414 or 400 error is reported, if the client's cookie information is large, the buffer size must be increased.
(+) Limit_except
This directive is used to restrict the HTTP method from accessing the contents of the location
Example: Limit_except get{
Allow 192.168.1.0/32;
Deny all;
}
() limit_rate directive
Syntax: Limit_rate speed
This instruction is mainly used for speed limit, the speed limit unit is "bytes/sec", generally in the provision of HTTP download and other applications will use the directive. Speed limit only for one connection, if the client opens two connection download, the download speed will be twice times the limit value
Example 1: Limit the download speed per connection to 100kb/seconds
Limit_rate 100k;
Example 2: Opening the speed limit function under certain conditions
server {
if ($slow)
{
Set $limit _rate 4k;
}
}
(+) limit_rate_after directive
Syntax: Limit_rate_after time
This instruction can set a number of bytes (1MB), after downloading the number of bytes greater than this value, the limit_rate instruction speed limit function will be effective. For applications such as MP3 online playback, HTTP Flash FLV video on demand, the use of this command will be a good result.
Example: The speed limit is 100kb/seconds after downloading more than 1MB bytes of file
Limit_rate_after 1m;
Limit_rate 100k;

(listen) directive
This instruction is used to set the server address and port number of the virtual host listener, you can set the server address and port number at the same time, you can also specify an IP address, or a port number, or a server name. If the listen directive only sets a server name or IP address, its default port number is 80.

(+) Location directive
The directive is configured differently for different URIs, either using a string or using a regular expression, using the following prefix.
1) ~*, which indicates a case-insensitive match.
2) ~, which indicates a case-sensitive match.

(log_not_found) directive
Syntax: Log_not_found [On|off]
This instruction is used to enable or disable the 404 error log, which can be used to prevent Nginx records from finding error messages such as robots.txt and Favicon.ico files.

(at) log_subrequest directive
Syntax: log_subrequest [On|off]
This directive is used to initiate or prohibit access_log such as rewrite rules, SSI request, and other sub-requests.

() Root command
This directive is used primarily to specify the document root directory of the request. For example, the configuration content is location/i/{root/spool/w3;} , the request URI address "/i/top.gif" returns the contents of the file "/spool/w3/i/top.gif" to the client.


Variables that can be used in 2.HTTP core modules.
The Nginxhttp core module supports some built-in variables with the same name as the Apache variable names, such as: $http _user_agent, $http _cookie, and some other nginx-specific variables are supported.

$arg _parameter: The variable contains the value of the GET request variable parameter when the query string.
$args: This variable equals the parameter in the request line.
$binary _REMOTE_ADDR: The client address in binary format.
$content _length: This variable equals the Content-length value in the client request header.
$content _type: This variable equals the Content-type value in the client request header.
$cookie _cookie: The client requests a cookie variable in the header. The prefix "$cookie _" plus a variable with the cookie name, the value of which is the value of the cookie name.
$document _root: This variable is equal to the document root path set by the root instruction that the current request belongs to.
$limist _rate: This variable allows you to limit the connection rate.
$remote _addr: The IP address of the client.
$remote _port: The port of the client.

3.HTTP Access Module
The HTTP Access module provides a simple host-name-based access control. Through this module, you can allow or disallow the specified IP address or IP segment to access certain virtual hosts or directories.
Cases:
Location/{
Deny 192.168.1.1;
Allow 192.168.1.0/24;
Allow 10.1.1.0/16;
Deny all;
}

4.HTTP Auth Basic Module
This module uses a user name and password login based on HTTP Basic authentication to protect your virtual host or directory location/{
Auth_basic "Restricted";
Auth_basic_user_file htpasswd;
}
Auth_basic: Used to specify the name of the prompt in the login box for the popup user name and password.
Auth_basic_user_file directive: Used to set the name of the htpasswd password file.

5.HTTP Browser Module
The module can create variables based on the information in the client's "user-agent" request header.
$modern _browser: If the browser is recognized as a new browser, it is equal to the value set by the modern_browser_value instruction.
$ancient _browser: If the browser is recognized as an old browser, it is equal to the value set by the ancient_browser_value instruction.
$msie: If the browser is authenticated as a MSIE browser, the value of this variable is 1.

6.HTTP CharSet Module
The module is used to add a text encoding type to the HTTP answer header "Content-type indicated". In addition, the module can convert the original text encoding of the server-side Web page to another text encoding, output to the client.
For example:
CharSet windows-1251
Source_charset Koi8-r
The charset instruction is used to add a text encoding type to the HTTP answer header "Content-type indicated". If the encoding is not the same as the encoding of the source_charset instruction, it will be re-encoded. The parameter is "off".

Override_charset On|off
When this command is turned on, the encoding conversion is turned on if the backend's FASTCGI server response header has "Content-type" header information.

7.HTTP Empty gif Module
The directive can keep a 1x1 pixel transparent GIF image in memory and can get very fast response time when the picture is requested.
Example: location =/_.gif{empty_gif;}


8.geo Module
Geo modules are primarily used for global load balancing and can be accessed to different servers depending on the client IP. Some customers in different regions, using different servers to handle the requirements, can use the GEO module.
The configuration is as follows:
Geo $geo {
Default 0;
Include conf/geo.conf;
127.0.0.1/32 2;
127.0.0.0/24 Ru;
192.168.1.0/24 1;
10.1.0.0/16 1;
}
Defalut: Any IP address, equivalent to 0.0.0.0/0
Include: You can reference a text file that contains the configuration content of Geo.

9.Gzip Module
The gzip module is primarily used to compress output to users who return to the client using gzip.
Currently 90% of browsers support gzip and deflate two compression formats. If the browser supports gzip compression, a line of "Accept-encoding:gzip,deflate" is sent in the HTTP request header, At this time the Nginx server can output the gzip compressed page to the browser, the browser decompression. This approach can reduce the amount of data transmitted on the network over 60%, not only save the server bandwidth, but also accelerate the user's download speed and experience.
(1) Gzip instructions.
Gzip on|off This directive is used to turn the Gzip module on or off
(2) Gzip_buffers directive
Gzip_buffers 4 4k/8k
The system gets a few units of cache for storing gzip compressed result data streams, for example 4 4k represents the memory in 4k, which is 4 times times the original data size in 4k. If not set, the default value is to request the same size memory space as the original data to store the gzip compression results.
(3) Gzip_comp_level directive
gzip compression ratio, 1 compression than the minimum processing speed, 9 compression than the maximum but the slowest processing speed (transmission is faster but the CPU is more expensive)
(4) Gzip_min_length directive
Gzip_min_length length
Set the minimum number of bytes allowed to compress pages, the number of page bytes from the header of the content-length, the default value is 0, regardless of the number of pages compression, it is recommended to set the number of bytes larger than 1k, less than 1k may be more pressure.
(5) Gzip_http_version directive
Gzip_http_version 1.0|1.1
Identifies the protocol version of HTTP. Since some early browsers or HTTP clients may not support gzip self-extracting, users will see garbled characters, so it is necessary to make some judgments.
(6) Gzip_proxied directive
Nginx as a reverse proxy when enabled, turn on or off the results returned by the backend server, matching the premise that the backend server must return the header containing via.
(7) Gzip_types directive
Syntax: Gzip_types mime-type[mime-type ...]
The match MIME type is compressed, and the "text/html" type is always compressed (whether or not specified).

10.HTTP Headers Module
This set of instructions is mainly used to set the header information of the attachment when the Nginx returns the content of the Web page to the user.
(1) Add_header directive
When the HTTP response status code is 200,204,301,302, or 304, the specified header header is incremented, where the value of the header head can be used for the variable.
(2) Expires directive
Use this directive to control the header information of "Expires" and "Cache-control" in HTTP reply, and play the role of controlling the browser page cache.
Syntax: Expires [Time|epoch|max|off]

11.HTTP Index Module
The module can be used to specify the default home page file name under the virtual host directory. If more than one file is specified, it will be found individually in order from left to right. You can add a file with an absolute pathname at the end of the list.


12.HTTP Referer Module
When the browser sends a request to the Web server, it usually takes Referer and tells the server which page link I took from, and the server can get some information for processing, such as the anti-theft chain. Because the HTTP Referer header information can be generated by the program to disguise. So through the Referer anti-theft chain is not 100% reliable.


13.HTTP Limit Zone Module
This module is used to control the number of concurrent connections for a session for a condition, such as restricting the number of concurrent connections per IP.
Limit_zone one $binary _remote_addr 10m;
In the example above, define a record area called "one" with a total capacity of 10MB, with capacity $binary_remote_addr as the benchmark for the session (i.e. one address for one session)
Syntax: Limit_conn zone_name max_clients_per_ip
This directive specifies the maximum number of concurrent connections for a session. The server returns 503 when the maximum number of concurrent connections specified is exceeded.


14.HTTP Limit Req Module
This module allows you to limit the number of concurrent requests within a specified unit of time for session sessions, single client IP addresses. You can mitigate DOS malicious attacks on multiple application servers to some extent.


15.HTTP Log Module
(1) access_log instruction. This instruction is used to set the path, format, and buffer size of the log file. With "Off" as the unique parameter, the log file will not be logged. If no log format is specified, the default is "combined" Format. The size of the buffer must be less than the atomic record size of the writer disk file.
(2) Log_format instruction. This instruction is used to describe the log format. In the format of the log, you can use most of Nginx's common variables, as well as some variables that exist only when the log is written.


16.HTTP MAP Module
The module allows classification, or maps a set of values to a different set of values, and stores those values in a variable.
Cases:
Map $http _host $name {
hostnames;
Default 0;
example.com 1;
*.example.com 1;
Test.com 2;
*.test.com 2;
. site.com 3;
}

17.HTTP memcached Module
Memcached is a high-performance distributed memory object caching system for dynamic Web applications to offload database load. It provides a dynamic, database-driven site speed by caching data and objects in memory to reduce the number of times the database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C. However, the client can be written in any language and communicates with the daemon through the memcached protocol, and it does not provide redundancy. When a server stops running or crashes, All key/value pairs that are stored on the server will be lost. Memcached (official website: http://www.memcached.org/) was developed by Danga Interactive to increase the speed of livejournal.com access. LJ Dynamic page visits per second thousands of times, user 7 million. Memcached significantly reduces database load for better allocation of resources and faster access to them.


18.HTTP Proxy Module
This module is used to forward requests to other servers. Keep-alive refers to the http/1.1 protocol, where multiple HTTP requests are made and received in the same connection, saving the time overhead of creating the TCP connection process. and http/ 1.0 The Protocol does not have the ability to keep-alive requests. Therefore, in the http/1.0 protocol, each request to the backend creates a connection that is deleted when the transfer is complete. Nginx uses the http/1.1 protocol to communicate with the browser, using the HTTP/1.0 protocol to communicate with the back-end server.
Proxy_cache Related Instruction Set
1) Proxy_cache
2) Proxy_cache_path
3) Paroxy_cache_methods
4) proxy_cache_min_uses
5) Proxy_cache_valid
6) Proxy_cache_use_stale

14.HTTP SSI Module
SSI, commonly referred to as server embedding. How SSI works: before sending content to the browser, you can use the server side include (SSI) directive to include text, graphics, or application information in a Web page. For example, you can use SSI to include time/date stamps, A copyright notice or form for the customer to fill out and return. For text or graphics that recur in multiple files, it is an easy way to use include files. Save the content in one of the included files without having to enter the content into all the files.


15.HTTP userid module.
This module is equivalent to the Apache Mod_uid module, which is used primarily for client identification. It mainly uses $uid_got and $uid_set variables.

Nginx's HTTP Module

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.