This article is an entry-level introduction to nginx, including installation, basic usage, Server Load balancer, dynamic/static separation, reverse proxy, and cache applications.
Dependency preparation
You can download the dependent libraries from the official website as follows:
PCRE
Http://www.pcre.org/
Zlib
Http://zlib.net
Http://sourceforge.net/projects/libpng/files/zlib/
Zlib.net cannot be accessed... Is it damaged? We can use another link zlib to be the dependent library of LibPNG.
OpenSSL
Http://www.openssl.org/
Note: The above libraries do not need to be installed. decompress them and the links will be linked during compilation. Some systems can be ignored if they have been installed.
The plug-in modules used in this article:
Https://github.com/agentzh/srcache-nginx-module
Https://github.com/agentzh/memc-nginx-modules
Install
Although nginx has been supported for windows for a long time, this article only introduces the installation of Linux
Download the source package (http://www.nginx.org/) on the official nginx website and unzip it.
At the time of this article, the latest version 1.3.0 was just released. We recommend that you use 1.2.0. There are always a bunch of open-source Dongdong versions. According to the Chinese piracy thinking, many people prefer the latest version. In fact, many of the latest versions are unstable and there are various inexplicable bugs, even some cannot be compiled. Therefore, it is generally recommended to use the stable version of the latest version (stable is usually identified ).
Execute./configure
. /Configure-with-PCRE =/usr/local/pcre-8.30/-with-zlib =/usr/local/zlib-1.2.3/-with-OpenSSL =/usr/local/openssl-1.0.1c/-Prefix =/usr/local/nginx-add-module =/usr/local/agentzh-MEMC-nginx-module-add-module =/usr/local/agentzh-srcache-nginx- module
Parameter introduction:
-With-PCRE =/usr/local/pcre-8.30/
Point to the PCRE decompression path or installation path
-With-zlib =/usr/local/zlib-1.2.3/
Point to zlib decompression path or installation path
-With-OpenSSL =/usr/local/openssl-1.0.1c/
Point to the OpenSSL decompression path or installation path
-Prefix =/usr/local/nginx
Path for nginx Installation
-Add-module =/usr/local/agentzh-MEMC-nginx-Module
-Add-module =/usr/local/agentzh-srcache-nginx-Module
The above two modules are mainly used to call memcached. For details, refer to the cache section.
Compile and install
Make & make install
Test
After successful installation, run
Sbin/nginx-V
The nginx version is displayed, indicating that the installation is complete.
Basic usage
Parameters
-V: displays the current nginx version number and compilation information.
-V only displays the current nginx version.
-T test the correctness of the configuration file
-C <configuration file path> does not use the system's CONF/nginx. conf, but uses the user-specified configuration file path.
Example:
Sbin/nginx-V
The preceding command execution result shows "Compilation Information". You can view the built-in modules and third-party modules used. Up to now, nginx has abundant third-party modules.
Sbin/nginx-t-c myconfig. conf
The preceding command is used to test the correctness of the configuration file myconfig. conf specified by the user.
Signal Control
Strong term retreat, ignoring everything
Close the program after the current quit request is processed
Hup reloads the configuration file, which is used to modify the configuration file. This operation will open a new working process and close the old
The current request is not interrupted.
In logs \ nginx. PID, the ID of the currently running nginx process is recorded,
You can use kill-signal namecat logs/nginx.pid
.
For example:
Kill-HUPcat logs/nginx.pid
Run the preceding command to reload the configuration file.
Monitoring
Configuration:
Location ~ ^/Mystatus /{
stub_status on; access_log off;
}
You can directly access http: // mystatus/to display some running status information of nginx.
Server Load balancer
Configuration:
Netty Server Service
Upstream nettysrv {
server 127.0.0.1:51074 weight=1 max_fails=2 fail_timeout=10s; server 127.0.0.1:14174 weight=1 max_fails=2 fail_timeout=10s; server 127.0.0.1:17917 backup;
}
Parameter description:
Weight weight. The larger the value, the larger the load.
Backup ID
The above three servers are configured for load balancing, and 51074 and 14174 are executed in weight mode. 17917 as a backup service, it is enabled only when both services fail.
Dynamic/static separation and reverse proxy
Static access configuration:
Static access folder
Location ^ ~ /Static /{
root /home/nginx/webroot/;
}
Use root to direct the link to the local path/home/nginx/webroot
Dynamic Access Configuration:
Dynamic access to my netty Serv
Location ^ ~ /Dynamic /{
proxy_pass http://nettysrv/; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr;
}
Use proxy_pass to direct to the service nettysrv defined in the previous section of Server Load balancer.
Proxy_set_header
When multiple virtual hosts are configured on the backend web server, you need to use this header to identify the Host Name of the reverse proxy.
Proxy_set_header
If the program on the backend web server needs to obtain the user IP address, obtain it from the header
Note: location can be a regular expression that matches the URI. However, after using a regular expression, you can only use rewrite to redirect the URI, instead of using root and proxy_pass.
Cache
Use the built-in cache proxy_cache
HTTP configuration:
Proxy_temp_path proxy_temp_dir;
The cache size is 2000 mb. The content not accessed within one day is automatically cleared. the maximum size of a single file cached on the hard disk is 5 MB.
Proxy_cache_path proxy_cache_dir levels = 1:2 keys_zone = cache_one: 2000 m inactive = 5 m max_size = 5 m;
Configure the call cache under location:
Location ^ ~ /Proxytest /{
proxy_cache cache_one; proxy_cache_valid any 5m; proxy_pass http://nettysrv/;
}
Use a plug-in to call memcached
To use memcached, use the third-party plug-in mentioned earlier.
Agentzh-MEMC-nginx-Module
Agentzh-srcache-nginx-Module
To install the plug-in, add the-add-module parameter during nginx Compilation:
-Add-module =/usr/local/agentzh-MEMC-nginx-module-add-module =/usr/local/agentzh-srcache-nginx-Module
For more information, see the installation section of nginx.
The configuration is as follows:
Memcached service address
Upstream memcachesrv {
server 127.0.0.1:11211;
}
Call the location of memcached
Location ^ ~ /Memcachetest {
set $key $uri$args; #set_md5 $key; srcache_fetch GET /mcache $key; srcache_store PUT /mcache $key; #more_clear_headers ‘Accept-Encoding‘; proxy_pass http://nettysrv/;
}
In the above example, you can use srcache to enter the local location for caching. If cache data exists, you can use it. If no cache data exists, you can continue to call it and cache the data.
Location/mcache {
internal; memc_connect_timeout 1000ms; memc_send_timeout 1000ms; memc_read_timeout 1000ms; set $memc_key $query_string; set $memc_exptime 300; memc_pass memcachesrv;
}
In ^ ~ $ URI $ ARGs is input to/mcache in/memcachetest configuration. mcache uses $ QUERY_STRING as the key of memcache.
Reference
Http://www.nginx.org/
Http://wiki.nginx.org/Main
This article from the "jump out of three" blog, please be sure to keep this source http://quickmove.blog.51cto.com/5025502/873337