Linux Notes - Chapter Twenty configure Nginx reverse proxy and load balancing

I. Introduction

Because of Nginx reverse proxy and load balancing is often mentioned, so these two features singled out to explain.

In fact, just as Nginx Nginx Proxy reverse proxy use, because the effect of the performance reverse proxy functionality is load balancing cluster effect, so call it Nginx load balancing. Then, reverse proxy and load balancing What difference does it make?

Normal Load balancing software, such as the famous the LVS, but in fact, function to forward the request packet (packet data may also be rewritten), transmission, wherein the DR mode are obvious from the features of the following node server load balancing perspective, the received request access to or from the load balancer client's real user, and is not the same reverse proxy, reverse proxy access to a user's request is received, the user agent will re-initiate under the node server request broker, and finally the data back to the client end user, the server node view, the client user node access to the server is a reverse proxy server, rather than a real access to the user's website.

Therefore, like the LVS load balancing packet transfer is requested by the user, and Nginx reverse proxy is receiving a user request and then initiate a request to the requesting node again behind it.

Nginx components to achieve load balancing two main reasons:

. 1) proxy_pass instruction belongs ngx_http_proxy_module module, which may forward the request to another server, in the actual work in reverse proxy, the function will be designated by the location URI matching, then the request meet the matching URI received by proxy_pass thrown defined upstream node pool.

2) Nginx load balancing function relies on ngx_http_upsteam_module module, supports proxy mode include proxy_pass, fastcgi_pass, memcached_pass, etc., the new software supports the way Nginx has increased. This article explains proxy_pass proxy mode.
ngx_http_upstream_module Nginx module allows one or more groups defined in the node group server, can send a request to the site corresponding to the pre-defined group names Upstream proxy_pass proxy mode by using the specific wording as "proxy_pass http: // www_server_pools", Upstream www_server_pools which is a node server group name.

Nginx reverse proxy important parameters:

Two, Nginx reverse proxy

Nginx reverse proxy means that when a user accesses the Web server, the network may not communicate, because most Web servers only private IP, if you want to access the Web server, you can find a server, so that the Web server and can only server interoperability, but also to communicate and users, this server is a reverse proxy server, because it is a proxy server, not the client.

First yum install nginx, and then enter the / etc / nginx directory, create a virtual host directory vhost, and create a new virtual host configuration file proxy.conf, as follows:

[root@masternode nginx]# mkdir /etc/nginx/vhost
[root@masternode nginx]# vim /etc/nginx/vhost/proxy.conf
server {
    listen       80;
    server_name      www.baidu.com;

    location / {
        proxy_pass   http://14.215.177.39;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Indicates that the agent to the above configuration www.baidu.com, proxy_pass instruction belongs ngx_http_proxy_module module, which may forward the request to another server, in the actual work in reverse proxy, the function will be designated by the location matches the URI, then in line with the request received matches the URI proxy_pass thrown defined by upstream node pool.

Then, modify the configuration file nginx.conf nginx master, as follows:

[root@masternode nginx]# vi /etc/nginx/nginx.conf

} End symbol in the top row is added:

include vhost/*.conf;

This represents nginx at boot time, loads vhost conf directory of all the files.

Check the nginx configuration file for errors, as follows:

[root@masternode nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Start nginx, and access to, verify the following:

Normal access to www.baidu.com by nginx reverse proxy.

While access to other domain names, because there is no agency to another domain, therefore, can only return to the default home page Nginx.

Three, Nginx Load Balancing

Proxy server called a proxy, on two or more proxy servers referred to as load balancing. Behind a proxy server can be multiple Web servers, multiple Web servers to achieve load balancing function to provide services. Under normal circumstances, if not the proxy service, the user accesses a Web server platform to only one request or specify IP, or the DNS server to multiple, such that a user A to access a server, two user access server B, and so on. While this may be, but not very friendly operation, if the A server is down, because a user to resolve the server A, not be able to access the site.

With Nginx load balancing, if A server is down, the proxy server will not request to server A. Nginx load balancing means upstream module, it can not be defined in a plurality of IP at proxy_pass, but may be defined in a plurality of IP upstream below.

Www.baidu.com below to verify, use the dig command to view the IP address of the DNS, is not installed, you can use yum to install, as follows:

[root@masternode vhost]# yum install -y bind-utils
[root@masternode vhost]# dig www.baidu.com
......
;; ANSWER SECTION:
www.baidu.com.          46      IN      CNAME   www.a.shifen.com.
www.a.shifen.com.       157     IN      A       14.215.177.38
www.a.shifen.com.       157     IN      A       14.215.177.39
......

Dns can be seen in the parsing process, as www.baidu.com cname record (corresponding alias) configured to jump to the www.baidu.com www.a.shifen.com mapping records dns server. And www.a.shifen.com as A (address) record, is mapped to the real IP information.

Create a load balancing configuration file load-balance.conf, as follows:

[@ masternode the root Vhost] # Vim / etc / nginx / Vhost / load- balance.conf 
upstream baidu_com { 
    #ip_hash nginx is a load balancing algorithm to make the same server with the same client request ip 
    ip_hash; 
    Server 14.215 . 177.38: 80 ; # define the real server address 
    server 14.215 . 177.39: 80 ; 
} 
server { 
    the listen        80 ; # listening port 
    server_name www.baidu.com; # domain 

    LOCATION / { 
        proxy_pass HTTP: // baidu_com; 
        proxy_set_header $ HOST Host; 
        proxy_set_header the X- -Real- IP $ REMOTE_ADDR; 
        proxy_set_header X--Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Stop nginx service, before removing the reverse proxy configuration file proxy.conf, and then restart nginx, under normal circumstances, curl -x127.0.0.1: 80 www.baidu.com will return to the default page nginx, but heavy-duty finish after the load-balance.conf, revisit Baidu, Baidu home page appears normal, as follows:

Nginx does not support proxy HTTPS, behind the server if the write port 443 is not supported, Nginx proxy only HTTP and TCP. If you want to proxy HTTPS, allowing users to see HTTPS, back-end server must be a 80-port, listening on port 443 on the proxy server, port 443 is returned to the user with the proxy server.

Guess you like

Origin www.cnblogs.com/cnjavahome/p/11483641.html