Nginx and nginx Configuration

Source: Internet
Author: User
Tags sendfile types of functions

Nginx and nginx Configuration
I. Brief Introduction to nginx the nginx structure is divided into three modules:

1. core modules (HTTP module, EVENT module, and MAIL module)

2. Basic modules (HTTP Access module, HTTP FastCGI module, HTTP Proxy module, and HTTP Rewrite module)

3. Third-party modules (GI module, HTTP Proxy module, and HTTP Rewrite module)

There are three types of functions

1. Handlers (processing module ). These modules directly process requests and output content and modify headers information. Generally, handlers can have only one processor module.

2. Filters (filter module ). This type of module mainly modifies the content output by other processor modules, and is finally output by Nginx.

3. · Proxies (proxy module ). Is the Nginx HTTP Upstream module. These modules interact with backend services such as fastcgi to implement Service proxy and load balancing functions.

Nginx is divided into single-worker and multi-worker processes.

1. in single-threaded mode, in addition to the main process, there is also a single-threaded Working Process

2. In multi-worker process mode, each worker process contains multiple processes

Nginx adopts the single-worker mode by default.

Ii. Nginx Configuration

The Nginx configuration file is in plain text format. It is generally in the conf directory of the Nginx installation directory.

The configuration file is organized as a block. Each block is usually represented by a braces.

The block can be divided into several levels. The Main command in the configuration file is located at the highest level. The Main layer can have Events, HTTP, and other levels. The HTTP layer also contains the Server layer, server block: server block can be divided into location layers, and a server block can contain multiple location blocks.

The Nginx configuration file is mainly divided into four parts:

Main: global settings

Server: Host settings ----- host and Port

Upstream: Server Load balancer settings ----- set a series of backend servers

Location: URL matching settings for specific locations ----- used to match the location of a webpage

The relationship between the four is that the server inherits the main, the location inherits the server, and upstream neither inherits other settings nor is inherited.

The following describes nginx. conf in detail.

# User specifies the worker process running users and user groups of Nginx. By default, the nobody account runs the user nobody; # worker_processes specifies the number of processes to be enabled by Nginx, we recommend that you set it to equal to the total number of CPU cores worker_processes 2; # error_log: defines the global error log file. The log output levels include debug, info, notice, warn, error, and crit. Among them, the debug output logs are the most detailed, while the crit output logs are the least. Error_log logs/error. log; error_log logs/error. log notice; error_log logs/error. log info; # pid is used to specify the storage file location of the process id. pid logs/nginx. pid; # worker_rlimit_nofile: used to bind the worker Process and CPUwoker_rlimit_nofile 65535 # Set the Nginx working mode and maximum number of connections events {# use is used to specify the Nginx working mode, nginx supports the following working modes: select, poll, kqueue, epoll, rtsig, And/dev/poll. Both select and poll are standard working modes, and kqueue and epoll are efficient working modes. For Linux systems, epoll is the first choice for use epoll; # The parameter "worker_connections" is used to define the maximum number of connections for each Nginx process. The default value is 1024worker_connections 1024 ;}

# Set the HTTP server

Http {log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" "$ response" $ upstream_cache_status '; # main refers to the name of the log output format, which can be referenced in the access_log command # To set the files contained in the configuration file, which can reduce the complexity of the main configuration file include mime. types; # default file type default_type application/octet-stream; # sets the maximum number of bytes for a single file requested by the client. client_max_body_size 8 m; # specifies the headerbuffer size from the client request header. For most requests, the buffer size of 1 kb is sufficient. If the message header is customized or a larger cookie exists, the buffer size can be increased. Here, the value is set to 32KB client_header_buffer_size 32 k; # used to specify the maximum number and size of the message headers in the client request. "4" is the number and "k" is the size, the Maximum Cache volume is 4 128 kb large_client_header_buffers 4 kb; # enable the efficient file transmission mode. The sendfile command specifies whether nginx calls the sendfile function to output files. For common applications, set it to on, if it is used for application disk I/O heavy load applications such as downloading, you can set it to off to balance the disk and network I/O processing speed and reduce the system load. Note: If the image is not displayed properly, change it to off. Sendfile on; # Set the timeout time for client connection persistence. After this time is exceeded, the server will close the connection, in the unit of seconds keepalive_timeout 60; # the size of the hash table of the server name server_names_hash_bucket_size 128;

HttpGzip module in Nginx

gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.1;gzip_comp_level 2;gzip_types text/plain application/x-javascript test/css application/xml;gzip_vary on;

Gzip: used to enable or disable the gzip module. "gzip on" indicates that GZIP compression is enabled and output data streams are compressed in real time.

Gzip_min_length: sets the minimum number of page bytes that can be compressed. The number of page bytes is obtained from the Content-Length header. The default value is 0, and most pages are compressed. We recommend that you set the size to 1 kb. smaller than 1 kb may increase the compression size.

Gzip_buffers: Apply for 4 memory units of 16 KB as the compression result stream cache. By default, apply for the same memory size as the original data to store GZIP compression results.

Gzip_http_version: used to identify the HTTP protocol version. The default value is 1.1. Currently, most browsers support GZIP decompression. Use the default value.

Gzip_comp_level: used to specify the GZIP compression ratio. 1 indicates the minimum compression ratio and the fastest processing speed. 9 indicates the maximum compression ratio and fast transmission speed, but the slowest processing speed, which also consumes CPU resources.

Gzip_types: used to specify the compression type. Whether or not it is specified, the "text/html" type is always compressed.

Gzip_vary: allows the front-end cache server to cache GZIP-compressed pages. For example, you can use Squid to cache Nginx-compressed data.

The following code configures a VM:

server {        listen 6100;        server_name 192.168.10.10 www.lehh.com www.m.lehh.com;        charset utf-8;        index index.html index.htm index.jsp;        root /web/wwwroot/www.ixdba.net        access_log logs/www.ixdba.net.access.log main

Server: defines the keyword starting with the VM

Server_name: used to specify an IP address or domain name. Multiple Domain Names are separated by spaces.

Index: used to set the default homepage address for access

Root: Specifies the web page root directory of the VM. This directory can be a relative or absolute path.

Charset: used to set the default encoding format of a webpage

Access_log: used to specify the path for storing access logs of the virtual host. The final main is used to specify the output format of access logs.

The following code configures the URL address:

loaction ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${        root /web/wwwroot/www.ixdba.net;        expires 30d    }

URL matching is the most flexible part in Nginx configuration. The address is defined using the location keyword.

Match start. Location supports regular expressions and condition-based matching. You can use the location command to filter dynamic and static Web pages.
All static files with an extension name ending with .gifini.jpg0000.00000000.png0000.bmp ).swf are handed over to Nginx for processing in the locationcode of hosts, and expires is used to specify the expiration time of static files, which is 30 days.
Similarly, the following code submits all files under upload and html to Nginx for processing. Of course, the upload and html directories are included in the/web/wwwroot/www.ixdba.net directory.

 

For the time being, there are still a lot of subsequent content to update the redirection and static cache configurations tomorrow.

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.