Nginx as a simple application of reverse proxy

Source: Internet
Author: User
Tags time interval nginx server nginx reverse proxy

Original: http://zhou123.blog.51cto.com/4355617/840837

The reverse proxy for Nginx is mainly in four aspects:
1, url rewrite url rewirte
2, reverse proxy reverse proxy and enable caching function
3. Load balance
4, the installation of Third-party modules to achieve health status detection.
The application of some nginx can be seen on the official website of Nginx, and the content of the introduction is quite detailed. Here I only talk about some of my nginx server learning and some understanding. Website address: Http://wiki.nginx.org/Modules
One, url rewrite url rewirte
To make Nginx have Rewirte function, to compile the nginx to use--with-http_rewirte_module This module, this function is also the default when compiling, if you do not want to use this feature--without-http_ Rewirte_module
This feature also supports URI rewriting.
In this feature there is an if (URL rewrite under what conditions) set Rewirte and other related applications that are interested in the available to its official website to see.
Here's a look at the use of Rewirte and if.
1, Rewirte
This is a pattern match as follows:
Rewrite regex replacement Flag
To simply explain the meaning of this sentence: Rewirte is the keyword, regex is the pattern of matching, replacement is to replace the matching to what, flag is a sign bit.
For example: Will htt://www.zhou123.com/attatch.php. value=111223 rewritten as Http://www.zhou123.com/111223/attatch
In the form shown below:
Rewirte ^/(Attatch) \.php\?value= (. *) $/$2/$1 last;
Here's a bit of sign:
Last: The current content is complete and you can continue with subsequent statements (if there are more than one statement)
Break: (Immediately after the execution of the rewrite statement, jump out of the statement behind the branch)
Redirect: Redirect to a complete URL such as: http://. This is temporary, says 302来.
Permanent: 301 for permanent redirects;
Here's a little example:
such as user access: www.zhou123.com/forum/rewritten as www.zhou123.com/bbs/
Rwirte ^/forum/?$/bbs/permanent;

Edit the Nginx configuration file to test:
Vim/etc/nginx/nginx.conf
Navigate to the server this row is added to this server as follows:
Location/{
root HTML;
Rwirte ^/forum/?$/bbs/permanent; # # # # # #这行是新加的内容.
Index index.html;
}
Enter in the browser: http://www..zhou123.com/forum/will change the address to www.zhou123.com/bbs/.
Rewirte is relatively simple here is not the test.
2, about the application of If
Syntax: if (condition) {...}
application environment; server,location
Conditions:
1, variable name: false values Are:empty string ("", or any string starting with "0";)
2, for the variable to compare the expression, you can use = or. = for testing;
3. Pattern matching of regular expressions:
~ Pattern matching with size
~* pattern matching that does not distinguish between uppercase and lowercase letters
。 ~ and. ~* respectively to the above two kinds of tests to take the opposite
4. Whether the test file exists-F or. -F
5, the test directory exists-D or. -D
6, the existence of test directory, file or link file-e or. -E
7, check the execution permissions of a file-X or. -X
In regular expressions, you can use the parentheses to mark the string that you want to match, and you can use $, $ ... for each.

3, the realization domain name jumps:
To be modified in the configuration file as shown below:
Server
{
Listen 80;
server_name jump.magedu.com;
Index index.html index.php;
Root/www/htdocs;
Rewrite ^/http://www.magedu.com/;
}
So you can change the jump.magedu.com to http://www.magedu.com/this domain name.
4, the realization of the domain name mirror
Server
{
Listen 80;
server_name mirror.magedu.com;
Index index.html index.php;
Root/www/htdocs;
Rewrite ^/(. *) $ http://www.magedu.com/$1 last;
}
The URL rewrite for nginx is simply introduced here.
Two, the following to talk about Nginx reverse proxy function:

1. Proxy
This module is also turned on by default when Nginx is compiled.
Usage: Proxy_pass URL address.
said that through Nginx to the reverse proxy to where to go, the following URL is to proxy the backend server address.
Here is a case to illustrate:
The IP for the nginx server is: 192.168.35.1/24
Background of the server IP is: 172.16.35.3/16 installed Apachen server, provide Web pages.
In the Nginx configuration file, just add the following:
Location/{
#root html;
#index index.html;
Proxy_pass http://172.16.35.3; # # Add this piece of content.
}

In the background of the Apache Server Web page content:
# echo "172.16.35.3" >/var/www/html/index.html
Enter in the browser: http://192.168.35.1 will be the result shown in the following figure:

At this point, the reverse proxy of the Nginx does not have any practical meaning, just add a server between the user and the server. To add caching for Nginx This is the advantage of Nginx.

2. Enable caching for reverse proxy:
Add the following in the configuration file:

HTTP {
Proxy_cache_path/data/nginx/cache levels=1:2 keys_zone=static:10m inactive=24h max_size=1g;
server {
Location/{
Proxy_pass http://172.16.35.3;
Proxy_set_header Host $host;
Proxy_cache STATIC;
Proxy_cache_valid 1d;
Proxy_cache_use_stale Error timeout invalid_header updating http_500 http_502;
}
}
}
The following environment is to be implemented in the same way as the example mentioned above.
The IP for the nginx server is: 192.168.35.1/24
Background of the server IP is: 172.16.35.3/16 Apache Service
Add finish then take a stress test:
On a different server, use the command:
Ab-c 100-n 10000 http://192.168.35.1/index.html # # #通过nginx服务器 to access the Apache server.
Find the following line in the result:
Requests per second:7354.40 [#/sec] (mean) ####### 7354 indicates the number of requests Received/sec

Ab-c 100-n 10000 http://172.16.35.3/index.html # # #来访问apache服务器.
Also find the following line in the result:
Requests per second:1489.96 [#/sec] (mean)
From the two results you can see clearly how powerful the Nginx reverse proxy and caching function are. Almost 10 times times the size of the Apache server.

Three, reverse proxy multiple servers to achieve load balancing:
Add a backend server: IP:172.16.35.2/16 also provides Apache servers.
The experimental environment is: three virtual machines

Nginx:192.168.35.1/24
The two Apache server IP are:
172.16.35.2/16
172.16.35.3/16
To ensure that the network between the three servers can be normal communication yo ...

The newly added Server Web page content is: "172.16.35.2"
# echo '
Add the following as shown in the Nginx configuration file:

HTTP {
...
...

Proxy_cache_path/data/nginx/cache levels=1:2 keys_zone=static:10m inactive=24h max_size=1g;
Upstream Myload {
Server 172.16.35.2;
Server 172.16.35.3 weight=2;
#################### defines the two backend servers and adds weights.
}

server {
Listen 80;
server_name localhost;
Location/{
Proxy_pass Http://myload; The name must be the same as the previous definition.
}

Enter in the browser: http://192.168.35.1 the results shown in the picture:


The contents of the refreshed Web page are toggled back and forth in the result shown in this illustration. This is to display the results so that a different Web page is set up for two servers.
Iv. Install and configure the third party module to realize the upstream in the back-end HTTP
Health status detection for server:
Description: This needs to be patched for Nginx to apply, the source code to compile nginx.

Module Download Address: Https://github.com/cep21/healthcheck_nginx_upstreams Module Name: Ngx_http_healthcheck_module

Installation Configuration method:
1, first decompression Healcheck module to a certain path, where the assumption is/tmp/healthcheck_nginx_upstreams

2, to the Nginx dozen patches

First unpack the Nginx and enter the Nginx source directory:
# Tar XF nginx-1.0.11.tar.gz
# CD nginx-1.0.11
# PATCH-P1 </tmp/healthcheck_nginx_upstreams/nginx.patch

Then compile nginx and add an option similar to the following when performing configure:
--add-module=/tmp/healthcheck_nginx_upstreams

So here's the command to use the following:
#./configure \
--PREFIX=/USR \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/\
--http-proxy-temp-path=/var/tmp/nginx/proxy/\
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\
--with-pcre \
--add-module=/tmp/healthcheck_nginx_upstreams
# Make && make install

How to use the Ngx_http_healthcheck_module module:

1, this module supports the instructions are:
Healthcheck_enabled
Enable this module

Healthcheck_delay
For the same backend server two time interval between detection, the unit millisecond, the default is 1000;

Healthcheck_timeout
Timeout for a health check, in milliseconds, with a default value of 2000;

Healthcheck_failcount
The number of successes or failures detected by a single back-end server is determined to be a success or failure, and enables or disables the server;

Healthcheck_send
Detection requests sent to detect the health status of the backend server, such as: Healthcheck_send "Get/health http/1.0" ' Host:www.zhou123.com ';

healthcheck_expected
The response content that is expected to be received from the backend server, or, if not set, the 200 status code received from the back-end server is correct;

Healthcheck_buffer
The size of the buffer space used in the health check;

Healthcheck_status
Output detection information in a similar stub_status manner, using the following methods:
Location/stat {
Healthcheck_status;
}

An example:
Write the following in the configuration file:
HTTP {
...
...

Proxy_cache_path/data/nginx/cache levels=1:2 keys_zone=static:10m inactive=24h max_size=1g;
Upstream Myload {
Server 172.16.35.2;
Server 172.16.35.3 weight=2;
healthcheck_enabled;
Healthcheck_delay 1000;
Healthcheck_timeout 1000;
Healthcheck_failcount 2;
Healthcheck_send "Get/.health.html http/1.0"; # # This is the test page, the content of this page is not required. As long as the page exists.
}

server {
Listen 80;
server_name localhost;

Location/{
Proxy_pass Http://myload;
}

Location/stat {
Healthcheck_status;
} ############# #这个可以查看状态;
Enter in Browser: Http://192.168.35.1/stat
The following figure is the result of the test:

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.