Nginx implements reverse proxy, load balancer-technology flow Ken

Source: Internet
Author: User
Tags sendfile nginx server nginx load balancing

1. Introduction

This blog post is the "Nginx implementation of dynamic/static file caching-technology flow Ken" two. This article will detail how Nginx implements reverse proxy and load balancing technology, supplemented by practical cases.

Reverse proxy--"reverse proxy" means to accept a connection request on the Internet as a proxy server, then forward the request to a server on the internal network and return the results from the server to the client requesting the connection on the Internet, Reverse. At this point the proxy server appears as a reverse proxy server externally. ”

Load Balancing-"network-specific terminology, load balancing based on the existing network structure, it provides a cheap and effective transparent way to expand network equipment and server bandwidth, increase throughput, enhance network data processing capacity, improve network flexibility and availability." ”

2.nginx implementing reverse Proxy 1. Several concepts

Reverse proxy: After receiving the client request, the target will be repaired IP address and Port

forward Proxy: After receiving the client request, the source will be repaired IP address and Port

Upstream servers: Which nodes in the back-end of the proxy server are actually serving clients, such servers are called upstream servers

Downstream server: The client is the downstream node

2. Reverse proxy Instructions
module: nginx_http_proxy_module    directive    proxy_pass: Specifies the IP and port Proxy_set_header of the upstream server    : Specifies that when the request packet is re-encapsulated, Add a new header    Syntax:     proxy_pass URL;    Default:     -    Context:     if  in location, limit_except    Example: Proxy_pass http://10.220.5.200:80;     Syntax:     proxy_set_header field value;    Default:     proxy_set_header Host $proxy _host;    Context:     http, server, location
3. Reverse Proxy Simple Example
Location/ {        proxy_pass http://10.220.5.180;        Proxy_set_header x-real-IP $remote _addr          proxy_set_header Host $proxy _host;    }
4. Reverse proxy actual case 1. Environmental preparedness

centos7.5

Reverse Proxy Server Ip:172.20.10.7/28

Web1 Server Ip:172.20.10.8/28

WEB2 Server Ip:172.20.10.9/28

2. Configure the reverse proxy server side

Yum install NIGNX need to configure the network source, copy the following code into your Yum repository

[Ken]name=kenenabled=1gpgcheck=0baseurl=https://  mirrors.aliyun.com/epel/7server/x86_64/

Installing Nginx

[email protected] ~]# Yum install nginx-y

Configure Nginx file, we achieve such an effect, static files are agents to 172.20.10.8, dynamic files are dispatched to 172.20.10.9, to achieve static and passive separation.

[Email protected] ~]# vim/etc/nginx/nginx.conf# For more information on configuration, see:#* Official 中文版 Documentation:http://nginx.org/en/docs/# * Official Russian Documentation:http://nginx.org/ru/docs/User nginx;worker_processes Auto;error_log/var/log/nginx/Error.log;pid/run/nginx.pid;# LoadDynamicModules. See/usr/share/nginx/readme.Dynamic. Include/usr/share/nginx/modules/*. conf;events {worker_connections 1024;} HTTP {log_format main ' $remote _addr-$remote _user [$time _local] "$request" ' $status $body _by Tes_sent "$http _referer" "$http _user_agent" "$http _x_forwarded_for"; access_log/var/log/nginx/ac    Cess.log main;    Sendfile on;    Tcp_nopush on;    Tcp_nodelay on;    Keepalive_timeout 65;    Types_hash_max_size 2048;    Include/etc/nginx/mime.types;       Default_type Application/octet-stream;    # include/etc/nginx/conf.d/*.conf;        server {Listen default_server;        Listen [::]:80 default_server;        server_name _;        root/var/www/html;        Index index.html index.php;   # Load configuration files for the default server block. Location/{Proxy_passhttp://172.20.10.8;        Proxy_set_header host $proxy _host;        Proxy_set_header Realip $remote _addr; } location ~^/.* (\.php) $ {proxy_passhttp://172.20.10.9;        Proxy_set_header host $proxy _host;        Proxy_set_header Realip $remote _addr;            } error_page 404/404.html;            Location =/40x.html {} error_page 502 503 504/50x.html; Location =/50x.html {} }}

For syntax detection

[Email protected] ~]# nginx -IS is successful

Restart after checking for no problems

[[email protected] ~]# systemctl start Nginx
3. Configure the Web server side

Installing Apache

[email protected] ~]# Yum install httpd-y

Prepare the test file, 172.20.10.8 prepare the static file

" This was 172.20.10.8 for static test ">/var

172.20.10.9 need to download PHP to support dynamic files

[email protected] html]# Yum install php-y

172.20.10.9 preparing the dynamic file,

[Email protected] ~]# CD/var/www/html/[[email protected] html]# vim index.php< phpphpinfo ();? >
4.web Server Restart
[Email protected] html]# systemctl restart httpd
5. Turn off security services
[Email protected] ~]# iptables-f
6. Browser Testing

Request static file test

A static file request was successfully forwarded to 172.20.10.8.

Test success!

Request a dynamic file test

The dynamic file request has been successfully forwarded to 172.20.10.9.

Test success!

7. Supplementary Supplement One
Supplement 1: Location is    as        follows /admin {            proxy_pass http://            proxy_pass http: // www.ken.com;         The requested URL is http://www.ken.com/admin/a.html     If the proxy method is Proxy_ Pass http:///www.ken.com/; then go to www.ken.com with the directory to find a.html,/representative full agent.     If the proxy mode is Proxy_pass http://www.ken.com, then go to www.ken.com directory with the admin to find a.html
Add two
Supplement 2: If a pattern match (regular) is used in the location, then the URL in the location is directly appended to the broker node. At this point, there can be no content behind the upstream server, including/ Location~\.php$ {proxy_pass http://www.ken.com; [Regular expression Proxy_pass the forwarding address is not added] <<< correct notationProxy_pass http://www.ken.com:80; <<< correct wordingProxy_pass http://www.ken.com/; <<< Error notationProxy_pass http://www.ken.com/img; <<< Error notationat this point, if the requested URL is http://www.baidu.com/book/stu/a.php, it will act as ahttp://www.ken.com/book/stu/a.php
Add three
Add 3:    If there is a redirect in the location, then replace the URI        in the proxy node withthe redirected URI/ {            /(. *) $/index.php?name=$1  break;            Proxy_pass http://www.baidu.com:80/img;     At this point, if the requested URL is http://Www.ken.com/bajie, it will be proxied to Www.baidu.com/index.php?name =bajie
3.nginx for load balancing 1. Several concepts

Scheduler: Distributing the user's request to a back-end node

Upstream server ( Real Server ) : Each node that is really used to process user requests is an upstream server

CIP: IP address of the client

RIP: The IP address of the real server

VIP: Virtual IP, what the user sees is also the virtual IP

2. Directives
directive: Upstream    function: Define an upstream server group    format        upstream name {            Server  upstream server 1  parameter parameter;            Server  1 parameter parameters for upstream servers  ;            Server  1 parameter parameters for upstream servers  ;        }
3. Important Parameters
    weight=#: Set the weight of the server (the larger the number, the higher the weight)    backup: Set the server in standby (other node fails, the standby node starts to work)    Down: Sets a node to be offline (often used to maintain a node)    max_fails= number: Set a number ofsuccessive forwarding failures to assume that the node failed, and then no longer forwarded the user request    to that node Fail_timeout=time: Used in combination with the previous parameter to set the waiting time for the upstream server response timeout
4.nginx Load Balancing practical case 1. Environmental preparedness

centos7.5

Nginx Server Ip:172.20.10.7/28

Web1 Server-Side IP:172.20.10.8/28

WEB2 Server-Side IP:172.20.10.9/28

2. Configure the Nginx server side

Install Nginx slightly

Configuring Nginx Files

[Email protected] ~]# vim/etc/nginx/nginx.conf# For more information on configuration, see:#* Official 中文版 Documentation:http://nginx.org/en/docs/# * Official Russian Documentation:http://nginx.org/ru/docs/User nginx;worker_processes Auto;error_log/var/log/nginx/Error.log;pid/run/nginx.pid;# LoadDynamicModules. See/usr/share/nginx/readme.Dynamic. Include/usr/share/nginx/modules/*. conf;events {worker_connections 1024;} HTTP {log_format main ' $remote _addr-$remote _user [$time _local] "$request" ' $status $body _by Tes_sent "$http _referer" "$http _user_agent" "$http _x_forwarded_for";    Access_log/var/log/nginx/access.log main;    Sendfile on;    Tcp_nopush on;    Tcp_nodelay on;    Keepalive_timeout 65;    Types_hash_max_size 2048;    Include/etc/nginx/mime.types;       Default_type Application/octet-stream;    # include/etc/nginx/conf.d/*.conf;        Upstream Ken {server 172.20.10.8 weight=1 max_fails=3 fail_timeout=5;    Server 172.20.10.9 weight=2 max_fails=3 fail_timeout=5;        } server {Listen default_server;        Listen [::]:80 default_server;        server_name _;  root/var/www/html;        Index index.php index.html;        # Load configuration files for the default server block. # Include/etc/nginx/default.d/*.conf;  Location/{Proxy_passhttp://ken/;        Proxy_set_header host $proxy _host;        Proxy_set_header Realip $remote _addr;            } error_page 404/404.html;            Location =/40x.html {} error_page 502 503 504/50x.html; Location =/50x.html {} }}

Grammar detection

[Email protected] ~]# nginx -IS is successful

Re-start Nginx

[Email protected] ~]# systemctl restart Nginx
3. Configure the Web server side

The same as the reverse proxy configuration above.

4. Browser Testing

Enter the IP address of the Nginx server side

Because the weight of the 172.20.10.9 is 2, that is, two times 172.20.10.9 will appear once 172.20.10.8. Perform a refresh test

Test success!

Nginx three major functions, caching, reverse proxy, load balancing, has been fully explained, whether the Nginx has a new understanding of that? Do your own experiment right now.

Nginx implements reverse proxy, load balancer-technology flow Ken

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.