Ma Ge study notes 28-nginx reverse proxy, load balancing, caching, URL rewriting and read/write splitting

Source: Internet
Author: User
Tags nginx reverse proxy

Nginx reverse proxy

Nginx uses the proxy module to implement reverse proxy. As a web reverse proxy server, nginx is responsible for receiving customer requests and dispatching user requests to the upstream Server Based on Uri, client parameters, or other processing logic ). The most important command for nginx to implement the reverse proxy function is proxy_pass, which can proxy a URI defined by location to a specified upstream server (group. In the following example, the/uri of location is replaced with the/newuri on the upstream server.

Location/uri {

Proxy_pass http://www.magedu.com: 8080/newuri;

}

However, there are two exceptions to this processing mechanism. One is that if the URI of location is defined through pattern matching, its URI will be passed directly to the game server, rather than the specified conversion Uri. For example,/forum in the following example will be represented as http://www.magedu.com/forum.

Location ~ ^/BBS {

Proxy_pass http://www.magedu.com;

}

The second exception is that if URL redirection is used in loation, nginx will use the redirected URI to process the request without considering the URI defined on the upstream server. In the example shown below, the URI sent to the upstream server is/index. php? Page = <match> instead of/index.

Location /{

Rewrite/(. *) $/index. php? Page = $1 break;

Proxy_pass http: // localhost: 8080/index;

}

Proxy Module Instructions

There are many available configuration commands for the proxy module. They are used to define many attributes when the proxy module is working, such as the connection timeout duration and the HTTP protocol version used for proxy. The following is a simple description of commonly used commands.

Proxy_connect_timeout: Maximum waiting time before nginx sends a request to the upstream server;

Proxy_cookie_domain: modifies the domain attribute set by upstream server through the set-Cookie header to a specified value. The value can be a string, a regular expression pattern, or a referenced variable;

Proxy_cookie_path: modifies the path attribute set by upstream server through the set-Cookie header to a specified value. The value can be a string, regular expression mode, or a referenced variable;

Proxy_hide_header: Set the header to be hidden in the message sent to the client;

Proxy_pass: Specifies the URL path from the request proxy to upstream server;

Proxy_set_header: rewrite the header of the message sent to the upsream server;

Proxy_redirect: overwrite the location and refresh the header of the packet received from upstream server;

Proxy_send_timeout: maximum interval between write operations sent to upstream server two times before the connection is disconnected;

Proxy_read_timeout: maximum interval between two read operations received from the upstream server before the connection is disconnected;

The upstream module has three load balancing algorithms:

Round Robin)

IP Hash (ip_hash)

Least connections (least_conn)

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.