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)