Nginx Increase Web Access speed

Source: Internet
Author: User
Tags file handling netcraft
Nginx Introduction

The article Form here:http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/

Nginx ("Engine X") is a high-performance HTTP and reverse proxy server and a IMAP/POP3/SMTP proxy server. Nginx was developed by Igor Sysoev, the second rambler.ru site for Russian traffic, which has run over 2.5 at the site. Igor releases the source code in the form of a BSD-like license. Although still beta, Nginx has been known for its stability, rich feature sets, sample configuration files, and low system resource consumption.

According to the NetCraft survey of the latest issue (June 08), more than 2 million of the hosts have used Nginx, which exceeds the other lightweight HTTP server LIGHTTPD, ranking fourth and developing rapidly. Here are the first few reports of the report:

Product Number of sites
Apache 84,309,103
Iis 60,987,087
Google GFE 10,465,178
Unknown 4,903,174
Nginx 2,125,160
Oversee 1,953,848
Lighttpd 1,532,952

For more detailed information on this survey report, please see the link below:

http://survey.netcraft.com/Reports/200806/

The figure below is a comparison of the number of sites using Nginx and lighttpd in recent months compared to Figure 1. The number of sites using Nginx and lighttpd in recent months compared to what must be known before using Nginx currently officially Nginx and Windows is not supported, you can only install and use it under Linux, UNIX, BSD systems; Nginx itself is just an HTTP and reverse proxy server, and it cannot support different page scripts like Apache by installing various modules, such as PHP, CGI, and so on; Ngin X supports simple load balancing and fault tolerance, and supports functionality as a basic HTTP server, such as logs, compression, Byte ranges, chunked responses, SSL, virtual hosts, and so on. Install the Nginx under Linux

To ensure a more flexible configuration using regular expressions in Nginx, it is necessary to determine whether the system has PCRE (Perl compatible Regular Expressions) packages installed before installation. You can download the latest Pcre source pack to ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/and download the build and install Pcre package using the following command:

# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
# tar zxvf pcre-7.7.tar.gz
# CD pcre-7.7 #/configure # make
install

The next installation Nginx,nginx typically has two versions, stable and development, you can choose one of these two versions for your purposes, and the following are detailed steps for installing Nginx into the/opt/nginx directory:

# wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
# tar zxvf nginx-0.6.31.tar.gz
# cd nginx-0.6.31
#./ Configure--with-http_stub_status_module–prefix=/opt/nginx
# make
Install

The parameter--with-http_stub_status_module is used to enable the Nginxstatus feature of Nginx to monitor the current state of the Nginx.

After successful installation, the/opt/nginx directory has four subdirectories: conf, HTML, logs, Sbin. The Nginx configuration file is stored in the Nginx file in Conf/nginx.conf,nginx, where only one program file is located in the Sbin directory. Ensure that the system's 80 ports are not occupied by other programs, run the Sbin/nginx command to start Nginx, open the browser to access the machine's IP, if the browser appears Welcome to nginx! Indicates that the Nginx has been installed and running successfully. common Nginx parameters and control Program Run Parameters

Nginx installed only a program file, itself does not provide a variety of management procedures, it is the use of parameters and system signaling mechanism to control the Nginx process itself. Nginx parameters include the following:

-C <path_to_config>: Using the specified configuration file instead of the nginx.conf under the Conf directory.

-T: It is important to test that the configuration file is correct and to reload the configuration at run time, to detect that the modified configuration file has a syntax error.

-V: Displays the Nginx version number.

-V: Displays the Nginx version number, compilation environment information, and compile-time parameters.

For example, to test whether a configuration file is written correctly, we can use the following command

Sbin/nginx–t–c conf/nginx2.conf
control of Nginx by Signal

Nginx supports the signals in the following table:

Signal name function Description
TERM, INT Quickly close the program and abort the request that is currently being processed
QUIT Close the program after you finish processing the current request
HUP Reloads the configuration and opens a new worker process, shutting down the process, which does not interrupt the request
USR1 Reopen the log file to switch logs, such as generating a new log file every day
USR2 Smooth Upgrade Executable Program
Winch Calmly close the worker process

There are two ways to control Nginx through these signals, the first is through the logs directory Nginx.pid view the current running Nginx process ID, through kill–xxx <pid> to control Nginx, where XXX is the signal listed in the table above Name. If you have only one Nginx process in your system, you can also do so through the killall command, such as running Killall–s HUP Nginx to let Nginx reload the configuration. Configure Nginx

Let's take a look at an actual configuration file:

 User nobody;# The main worker_processes 4;# work process number of the worker process, which is equivalent to the CPU kernel number #error_log logs/error.log; 
 #error_log Logs/error.log Notice; 

 #error_log Logs/error.log Info; 

 #pid Logs/nginx.pid;       Events {Use Epoll #Linux the best performance event mode Worker_connections 2048;# each worker process allows maximum number of simultaneous connections} HTTP {include 
    Mime.types; 

    Default_type Application/octet-stream; #log_format Main ' $remote _addr-$remote _user [$time _local] $request ' # ' "$status" $body _bytes_se 

    NT "$http _referer" "$http _user_agent" "$http _x_forwarded_for"; 
    #access_log off; 
    Access_log logs/access.log;# log file name sendfile on; 
    #tcp_nopush on; 

    Tcp_nodelay on; 

    Keepalive_timeout 65; 
    
    Include gzip.conf; 
	 # configuration information for all backend servers in the cluster upstream Tomcats {server 192.168.0.11:8080 weight=10; 
	 Server 192.168.0.11:8081 weight=10; 
	 Server 192.168.0.12:8080 weight=10; Server 192.168.0.12:8081weight=10; 
	 Server 192.168.0.13:8080 weight=10; 
    Server 192.168.0.13:8081 weight=10; 

        server {Listen #HTTP port server_name localhost; 

        CharSet Utf-8; 

	 #access_log Logs/host.access.log Main; 
	 Location ~ ^/nginxstatus/{stub_status on; #Nginx State monitoring configuration access_log off; 
	 } location ~ ^/(Web-inf)/{deny all; } location ~ \.
	 (htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js| 
	    ZIP|JAVA|JAR|TXT|FLV|SWF|MID|DOC|PPT|XLS|PDF|TXT|MP3|WMA) $ {root/opt/webapp; 
        Expires 24h; 
        } location/{Proxy_pass http://tomcats;# reverse proxy include proxy.conf; 

        } error_page 404/html/404.html; 
        # REDIRECT Server error pages to the static page/50x.html # Error_page 502 503/html/502.html; 
        Error_page 504/50x.html; 
        Location =/50x.html {root html; } 
    } 
 }
Nginx Monitoring

Above is an actual Web site configuration instance, where the gray text is the configuration description. In the above configuration, first we define a location ~ ^/nginxstatus/, so that through the http://localhost/NginxStatus/can monitor the operation of Nginx information, the contents of the display are as follows:

Active connections:70 
Server accepts handled requests
 14553819 14553819 19239266 reading:0 writing:3 waiti 
ng:67

The contents of the Nginxstatus display are as follows: Active connections– the number of active connections currently being processed by Nginx. The server accepts handled requests--a total of 14,553,819 connections, a successful 14,553,819 handshake (proving that there was no failure), handled 19,239,266 requests (with an average handshake of 1 each). 3 data requests). Reading--The number of Header information that Nginx reads to the client. Writing-The number of Header information that the Nginx returns to the client. Waiting-When keep-alive is turned on, this value equals active-(reading + writing), meaning that Nginx has finished processing the resident connection waiting for the next request instruction. Static file Handling

With regular expressions, we can let Nginx identify various static files, such as all requests under the images path can be written as:

Location ~ ^/images/{
    root/opt/webapp/images;
}

The following configuration defines how several file types are requested to be processed.

Location ~ \. (Htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt) $ {
    Root/opt/webapp;
    Expires 24h;
}

For example, images, static HTML files, JS script files and CSS style files, and so on, we want Nginx to directly handle and return to the browser, which can greatly accelerate the speed of web browsing. So for this type of file we need to specify the path of the file through the root instruction, and because it is not often modified, the expires instruction is used to control its cache in the browser to reduce unnecessary requests. The expires directive controls the headers of the "Expires" and "Cache-control" in the HTTP answer (plays a role in controlling the page cache). You can write Expires using a format such as the following:

Expires 1 January, 1970, 00:00:01 GMT;
expires 60s;
Expires 30m;
Expires 24h;
Expires 1d;
Expires Max;
Expires off;
dynamic page Request processing

Nginx itself does not support the current popular JSP, ASP, PHP, PERL and other dynamic pages, but it can through the reverse proxy to send requests to the backend server, such as Tomcat, Apache, IIS, etc. to complete the dynamic page request processing. In the previous configuration example, we first defined some static file requests processed directly by Nginx, and all other requests were routed through the Proxy_pass instructions to the backend server (Tomcat in the example above). The simplest proxy_pass usage is as follows:

location/{Proxy_pass http://localhost:8080;
Proxy_set_header X-real-ip $remote _addr; }

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.