Nginx reverse proxy and Server Load balancer

Source: Internet
Author: User
Tags nginx reverse proxy

Nginx reverse proxy and Server Load balancer

I. Introduction

Reverse Proxy(Reverse Proxy) means to use a Proxy server to accept connection requests on the internet, and then forward the requests to the server on the internal network, return the result obtained from the server to the Client Requesting connection from the internet. The proxy server is displayed as a server.

Reverse Proxy Load Balancing TechnologyIt is to dynamically forward connection requests from the internet to multiple servers on the internal network for processing in the form of reverse proxy, so as to achieve the purpose of load balancing.

Ii. Common Use Cases

1.Reverse Proxy(2-3)

A. the Web browser sends an HTTP request to the front-end Nginx

B. If the requested resource is a static resource, the front-end Nginx server directly returns the static resource to the Web browser;

C. if the requested resource is a dynamic page, the front-end Nginx server forwards the request to the upstream server (Tomcat \ Apache, etc.) through reverse proxy ), after the upstream server completes processing the dynamic request, it returns it to the front-end Nginx server. The front-end Nginx server then forwards the dynamic page response to the Web browser.

2.Server Load balancer application scenarios:

After receiving a large number of HTTP requests, the Nginx front-end tries its best to evenly distribute requests to the game servers on each server through certain policies to avoid the pressure on a single server.

Iii. configuration instructions:

1. Basic configuration of Server Load balancer

1) upstream Block

Syntax: Upstream name {...}

Configuration block: http

The upstream block defines a cluster of upstream servers to facilitate proxy_pass in the reverse proxy. For example:

Upstream backend{Server backend1.example.com;Server backend2.example.com;Server backend3.example.com;}Server {Location / {Proxy_pass http://backend}}

2) server

Syntax: Server name [parameters];

Configuration block: upstream

The server configuration item specifies the name of an upstream server. The name can be a domain name, IP address port, Unix handle, and other parameters.

·Weight = number: Sets the weight forwarded to this upstream server. The default value is 1.

·Max_fails = number: This option is used with fail_timeout. If the number of failed forwarding attempts to the current upstream server exceeds the number within the fail_timeout period, the upstream server is considered unavailable during the current fail_timeout period. Max_fails is 1 by default. If it is set to 0, it indicates the number of failed checks.

·Fail_timeout= Time: indicates the number of failed forwarding times in the time range. The upstream server is considered unavailable for the time being and used to optimize the reverse proxy function. Fail_timeout is 10 seconds by default.

·Down: Indicates that the upstream server is permanently deprecated. It is only useful when ip_hash is used.

·Backup: The ip_hash configuration item is invalid. It indicates that the upstream server is only a backup server. Requests are forwarded only when all non-Backup upstream servers are invalid.

For example:

upstream backend{server backend1.example.com weight=5;server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;server unix:/tmp/backend3}

3) ip_hash

Syntax: ip_hash

Configuration block: upstream

In some scenarios, we may expect requests from a user to always be sent to a fixed upstream server. For example, if the upstream Server caches some information and requests from the same user are forwarded to any server on the cluster, therefore, each upstream server may cache the same copy of information, which may cause a waste of resources and make it difficult to effectively manage the cache information. Ip_hash is used to solve the above problem.

The ip_hash and weight configurations cannot be used at the same time. If an upstream server in the upstream cluster is temporarily unavailable, you cannot directly Delete the configuration. Instead, you need to identify the down parameter to ensure the consistency of the forwarding policy. For example:

upstream backend{ip_hash;server backend1.example.com;server backend2.example.com;server backend3.example.com down;server backend4.example.com;}

4) variables supported when logging

If you need to record some information about Server Load balancer to the access_log log, you can use the variables provided by the Server Load balancer function when defining the log format, as shown in the following table.

Variable name

Meaning

$ Upstream_addr

Upstream server address for Request Processing

$ Upstream_cache_status

Indicates whether the cache is HIT. Value Options: MISS, EXPIRED, UPDATING, STALE, and HIT

$ Upstream_status

HTTP response code returned by the upstream Server

$ Upstream_response_time

Response time of the upstream server, accurate to milliseconds

$ Upstream_http _ $ HEADER

HTTP header, such as upstream_http_host

2. Basic configuration of reverse proxy

1) proxy_pass

Syntax: proxy_pass URL;

Configuration block: location, if

This configuration item reversely proxies the current request to the server specified by the URL parameter. The URL can be a host name or an IP address port, for example:

Proxy_pass http: // localhost: 8000/uri /;

You can also directly use upstream blocks, for example:

upstream backend{……}server {location / {proxy_pass http://bakend}}

By default, reverse proxy does not forward the host Header in the request. If Forwarding is required, the following configuration must be added:

Proxy_set_headerHost $ host;

2) proxy_method

Syntax: proxy_methodmethod

Configuration block: http, server, location

This configuration item indicates the Protocol method name for forwarding. For example:

Proxy_method POST;

The method name will also be changed to POST when the GET request sent from the client is forwarded.

3) proxy_hide_header

Syntax: proxy_hide_header the_header;

Configuration block: http, server, location

Nginx will forward the response from the upstream Server to the client, but it will not forward the HTTP header fields by default: Date, Server, X-Pad, and X-Accel -*. After proxy_hide_header is used, you can specify which HTTP header fields cannot be forwarded. For example:

Proxy_hide_headerCache-Control;

Proxy_hide_headerMicrosoftOfficeWebServer;

3) proxy_pass_header

Syntax: proxy_pass_headerthe_header;

Configuration block: http, server, location

In contrast to the proxy_hide_header function, proxy_pass_header sets the headers that are originally prohibited from forwarding to allow forwarding.

4) proxy_pass_request_body

Syntax: proxy_pass_request_bodyon | off;

Configuration block: http, server, location

Determines whether to forward the HTTP packet to the upstream server.

5) proxy_pass_requst_headers

Syntax: proxy_pass_requst_headerson | off;

Configuration block: http, server, location

Determines whether to forward the HTTP header to the upstream server.

6) proxy_redirect

Syntax: proxy_redirect [default | off | redirect replacement];

Configuration block: http, server, location

When the response returned by the upstream Server is a redirection or refresh request (such as 301 or 302), proxy_direct can reset the location or refresh field in the HTTP header. For example:

Proxy_direct http: /localhost: 8000/two http: // frontend/one;

If the upstream server sends a 302 redirect request and the location field URL is http: // localhost: 8000/two/some/uri/, after the above configuration, the location actually forwarded to the client is http: // frontend/one/some/uri /.

3. Configuration instance files for reverse proxy and Server Load balancer:

Server Load balancer:

Worker_processes 1; events {worker_connections 1024;} http {upstream lxx {// default port 80: server 192.168.0.62 weight = 2; server 192.168.0.161 weight = 3;} server {listen 80; location/{proxy_pass http: // lxx ;}}}

Reverse Proxy:

 

Worker_processes 1; events {worker_connections 1024;} http {upstream lxx {// default port 80: server 192.168.0.62 weight = 2; server 192.168.0.161 weight = 3;} server {listen 80; location/{proxy_pass http: // lxx; # Proxy closed off; proxy_set_header Host $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ response; proxy_next_upstream errortimeout invalid_header http_500 http_502 http_503 http_504; Limit 0; proxy_connect_timeout 90; limit; Limit 90; limit 4 k; proxy_buffers 4 32 k; Limit 64 k; Limit 64 k ;}}}

 

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.