Nginx ( "engine x") is a high-performance and HTTP reverse proxy server is also a proxy server IMAP/POP3/SMTP. Nginx by Igor Sysoev to visit Russia in the second volume of Rambler.ru development of the site, which has been the site of more than two and a half years of operation. Igor will be the source code to BSD-type license issued in the form. Despite the still beta, but, Nginx because it has the stability of the rich feature set, and low-profile example of system resources consumption and a well-known.
At present, the official Nginx does not support Windows, you can only include Linux, UNIX, BSD system to install and use;
1. Nginx itself is only a HTTP proxy server and reverse, it can not be the same as Apache by installing a variety of modules to support different page script, such as PHP, CGI, etc.;
2. Nginx support of a simple load balancing and fault-tolerant;
3. As support for basic HTTP server functionality, such as logging, compression, Byte ranges, Chunked responses, SSL, virtual host, and so on, everything.
In order to ensure that Nginx in the use of regular expressions for a more flexible configuration, installation prior to the need to determine whether the system installed PCRE (Perl Compatible Regular Expressions) package. You can ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ download the latest PCRE source package, compiled using the following command to download and install the PCRE package
# 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
# make install
If your OS is CentOS,you can:
yum install pcre
Then install Nginx, Nginx general there are two versions of which are stability and development of version version, you can choose your purpose of the two versions of one of the following is to install Nginx to / opt / nginx detailed directory :
# wget http://sysoev.ru/nginx/nginx-0.6.34.tar.gz
# tar zxvf nginx-0.6.34.tar.gz
# cd nginx-0.6.34
# ./configure –with-http_stub_status_module –prefix=/opt/nginx
# make
# make install
Parameters of which –with-http_stub_status_module to nginx opening of the NginxStatus function to monitor the current state of Nginx.
After the success of the installation /opt/nginx four sub-directory are: conf, html, logs, sbin. Nginx one of the profile stored in conf / nginx.conf, Nginx is only one program file is located in the sbin directory nginx document. The 80 ports to ensure that the system did not have been occupied by other programs, run sbin / nginx order to start Nginx, open the browser to access the machine’s IP, if the browser appears Welcome to nginx! Said Nginx have been installed and running successfully.
Nginx common parameters and control
Running parameters
Nginx only after the installation of a program file itself does not provide a variety of management procedures, it is the use of the system parameters and mechanism of signal Nginx control of the process itself. Nginx parameters include the following:
-c <path_to_config>: the use of specified profile rather than the conf directory nginx.conf.
-t: test configuration file is correct, the need to re-load the run-time configuration, this command is very important to detect the revised profile of whether there is a syntax error.
-v: show nginx version number.
-V: show nginx version of its compiler, as well as environmental information, as well as compile-time parameters.
For example, we have to test whether or not to write a profile in the right, we can use the following command
sbin/nginx – t – c conf/nginx2.conf
Through the signal to the control Nginx
Nginx support of the table signals:
Signal 000
Description of the role of the
TERM, INT TERM, INT
Rapid closure of the program, currently suspended to deal with the request
QUIT
End to deal with the current request to close the program
HUP
Re-configured to load and open a new process, to shut down the process, this will not interrupt request
USR1
Re-open the log file for the switch logs, for example, every day a new generation of log files
USR2
Smooth upgrade executable
WINCH
Easily shut down the work process
There are two ways to control these signals Nginx, the first through the logs directory nginx.pid view the currently running Nginx the process ID, through the kill – XXX <pid> to control Nginx, which is the XXX out on the table The signals were out. If your system is only one Nginx process, you can also killall order to complete, such as running killall -s HUP nginx to let Nginx reload configuration.
A first look at the actual profile:
user nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] $request '
# '"$status" $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log off;
access_log logs/access.log;
sendfile on;
#tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
include gzip.conf;
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:8081 weight=10;
server 192.168.0.13:8080 weight=10;
server 192.168.0.13:8081 weight=10;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location ~ ^/NginxStatus/ {
stub_status on;
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;
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 500 504 /50x.html;
location = /50x.html {
root html;
}
}
}
|
Above is an example of the actual site configuration, in which gray text for that configuration. The above configuration, first of all, we define a location ~ ^ / NginxStatus /, so you can monitor http://localhost/NginxStatus/ through to the run Nginx message that reads as follows:
Active connections: 70 server accepts handled requests 14553819 14553819 19239266 Reading: 0 Writing: 3 Waiting: 67 |
NginxStatus display the contents of the meaning is as follows:
· active connections – currently being processed Nginx to connect the activities of a few.
· server accepts handled requests – handled a total of 14,553,819 connection, the success of the creation of 14,553,819 times to shake hands (no proof of failure in the middle), handled a total of 19,239,266 requests (average of 1.3 shook hands to deal with requests for information).
· reading – nginx read to the client’s Header information number.
· writing – nginx returned to the client’s Header information number.
· waiting – open the keep-alive, equal to the value of the active – (reading + writing), meaning that Nginx have been processed are still waiting for the next request to connect the presence of the command.
Static document processing
Through the regular expression, we can set up Nginx identified a variety of static documents, such as the path to the images under all requests can be written as follows:
location ~ ^/images/ {
root /opt/webapp/images;
}
|
The following is the definition of the configuration files of several types of requests for treatment.
location ~ \.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
root /opt/webapp;
expires 24h;
}
|
For example, pictures, static HTML files, js script files and documents, such as css style, we hope to deal directly with Nginx and return to the browser, which can greatly speed up the web browsing speed. Therefore we need to file for this type of instruction to root through the designated path of the file stored at the same time as such documents are not often modified by expires in order to control the browser’s cache, so as to reduce unnecessary requests. expires order to control HTTP response in the "Expires" and "Cache-Control" of the header (to control play the role of the page cache). For example, you can use the following form to write Expires:
expires 1 January, 1970, 00:00:01 GMT; expires 60s; expires 30m; expires 24h; expires 1d; expires max; expires off; |
Dynamic page request to deal with
Nginx does not support the popular JSP, ASP, PHP, PERL, and other dynamic page, but it can reverse proxy will be sent to the request of the back-end servers, such as Tomcat, Apache, IIS, and other dynamic page to complete the deal with the request. In front of the configuration example, we first defined by the Nginx to deal directly with a number of requests for static files, all other requests through the proxy_pass instructions sent to the back-end server (in the above example is the Tomcat). The simplest proxy_pass use are as follows:
location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
}
|
Here we do not have to use the cluster, but the request will be sent directly to run in the 8080′s Tomcat port services up to complete a similar JSP and Servlet to deal with the request.
When the page is visited very often require multiple application servers to share the dynamic operation of the implementation of the page, then we need to use the structure of the cluster. Nginx through upstream order to define a cluster server, the first complete example of our definition of a cluster of tomcats, this cluster of three servers, including a total of 6 Tomcat service. The wording of the directive proxy_pass become:
location / {
proxy_pass http://tomcats;
proxy_set_header X-Real-IP $remote_addr;
}
|
Nginx in the cluster configuration, Nginx use of the simplest rules of the average distribution to each cluster node allocation requests. Once a node fails, or re-onset time, Nginx will be very timely to deal with changes in the state to ensure that the user will not affect the visit.
Nginx official functions of the various modules to provide everything that these modules can be combined with the full requirements of a wide range of configurations, such as: compression, anti-Daolian, clusters, FastCGI, streaming media server, Memcached support, URL rewriting, and so on, but also the key Nginx that have HTTP server Apache and other high-performance unmatched. You can even without changing the structure of the original Web site, through the introduction of front-end Nginx to increase the speed of site visits.
This article is a brief introduction Nginx common, as well as the installation of the basic configuration and use more information on Nginx read the article refer to the back of resources. Here are very grateful to my friend – Chen Lei (chanix@msn.com), he has been doing the Nginx WIKI(http://wiki.codemongers.com/Main), at the same time he introduced me so well Of a software.
If your site is running on Linux, if you do not have to use some of the very complex and can not be completed to determine the function of Nginx, and that you should try Nginx.
· Access to Nginx English site.
· Access to Nginx Chinese WIKI.
· Access to Nginx English WIKI.
· Another lightweight HTTP server lighttpd.
Access to products and technologies
· Download the latest version of Nginx.
· Download PCRE.
I do believe all of the ideas you have presented on your post. They are very convincing and will definitely work. Still, the posts are very quick for novices. May you please prolong them a little from subsequent time? Thanks for the post.
[...] my previous article “Using Nginx to enhance the speed of site visits,” introduced Nginx the HTTP server and how to accelerate through its Web site access. In the actual [...]