Nginx Reverse Proxy module

Source: Internet
Author: User
Tags response code nginx reverse proxy
Nginx's reverse proxy module has a number of configurations, the following are some common configuration examples:

1.proxy_pass
Syntax: Proxy_pass URL
Configuration block: location,if
Explanation: This configuration item reverses the current request to the server specified by the URL parameter, which can be the hostname or IP address plus port form. For example:
View plain print?      nginx.conf configuration file//configuration URL address Proxy_pass http://www.54rd.net/html/webserver/;      You can also configure UNIX handles Proxy_pass http://unix:/path/to/backend.sock:/webserver/; HTTP can also be converted to a more secure https proxy_pass https://192.168.0.1;
By default, the reverse proxy is not forwarding the host header in the request. If forwarding is required, then the Set_header configuration must be added:
View plain print? Proxy_set_header Host $host;
2.proxy_method
Syntax: Proxy_method method;
Configuration block: http,server,location
Explanation: This configuration item represents the protocol method name at the time of forwarding, for example:
View plain print? After the configuration, the client sends a GET request when forwarding the method name will be changed to post Proxy_method post;
3.proxy_hide_header
Syntax: Proxy_hide_header the_header;
Configuration block: http,server,location
Description: Nginx forwards the response of the upstream server to the client, but the following HTTP header fields are not forwarded by default: Date,server,x-pad and x-accel-*. You can specify which HTTP header fields cannot be forwarded after you use Proxy_hide_header. For example:
View plain print? For example do not forward cache control Proxy_hide_header Cache-control;
4.proxy_pass_header
Syntax: Proxy_pass_header the_header;
Configuration block: http,server,location
Explanation: In contrast to the Proxy_hide_header function, Proxy_pass_header will set the header of the original forbidden forwarding to allow forwarding. For example:
View plain print? Allow redirection of Proxy_pass_header x-accel-redirect;
5.proxy_pass_request_body
Syntax: Proxy_pass_request_body on|off;
Default: Proxy_pass_request_body on;
Configuration block: http,server,location
Explanation: The role is to determine whether to send the HTTP package body to the upstream server part.

6.proxy_pass_request_headers
Syntax: Proxy_pass_request_headers on|off;
Default: Proxy_pass_request_headers on;
Configuration block: http,server,location
Explanation: The role is to determine whether to forward HTTP headers.

7.proxy_redirect
Syntax: Proxy_redirect [default|off|redirect replacement];
Default: Proxy_redirect default;
Configuration block: http,server,location
Explanation: When the response returned by the upstream server is redirected or a refresh request (such as an HTTP response code of 302 or 301), Proxy_redirect can reset the location or refresh field of the HTTP header, for example:
View plain print?   If the response code emitted by the upstream server is http://www.54rd.net/html/webserver/, the URL of the 302,location field is actually forwarded to http://www.54rd.net/html/php/      Proxy_redirect http://www.54rd.net/html/webserver/http://www.54rd.net/html/php/; You can also use the variables provided by Ngx-http-core-module to set Proxy_redirect http://www.54rd.net/html/webserver/http://$host: $server _port/      ; The host part of the Repalcement parameter can also be omitted, and the virtual host name will be used to populate the Proxy_redirect http://www.54rd.net/html/webserver//html/php/;

======================

When using the Nginx reverse proxy function, sometimes the URL of the redirect is not the URL I want, such as the following example: The front-end Nginx is responsible for the http:www 54rd net Yum Server beginning of the URL reverse proxy to the backend http:192 168 1 1 S On the erver. For the full use of the Nginx reverse proxy function, sometimes the URL of the redirect is not the URL I want, such as the following example:

The front-end Nginx is responsible for the http://www.54rd.net/yum/Server/beginning of the URL reverse proxy to the back end of the http://192.168.1.1/Server/.

For a proxy that has a complete path, such as http://www.54rd.net/yum/Server/, the server corresponds to a directory of the background server.

But when the http://www.54rd.net/yum/Server is accessed, the backend Nginx sends a 301 to/on, and then returns to the front end after the URL becomes http://www.54rd.net/Server/, This URL is obviously not what we want.

There is a proxypassreverse parameter in Apache that can be used to adjust the URL of the HTTP reply header sent by the reverse proxy server to resolve this problem.

After checking the Nginx manual, finally found the Proxy_redirect this parameter, it implements the function and Proxypassreverse similar, for example, add the following configuration:

View plain print?                  Location ^~/yum {Proxy_pass http://192.168.1.1/;      Proxy_redirect http://www.54rd.net//yum/; }
This way, when the http://www.54rd.net/yum/Server is accessed, it will be 301 to http://www.54rd.net/yum/Server/.

====================


8.proxy_next_upstream
Syntax: Proxy_next_upstream [Error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off];
Default: Proxy_next_upstream error timeout;
Configuration block: http,server,location
Explanation: This configuration item indicates that when a request is forwarded to an upstream server, the request continues to be processed by an upstream server, which is a better guarantee that the client will only receive an answer from an upstream server. The Proxy_next_upstream parameter is used to indicate under what circumstances the next upstream server will continue to be selected to forward the request: error: When a connection is initiated to the upstream server, a request is sent, and a response is read. Timeout: Sending a request or reading a response occurs over a timeout. Invalid_header: When an upstream server sends a response that is not valid. HTTP_500: The HTTP response code returned by the upstream server is 500. HTTP_502: The HTTP response code returned by the upstream server is 502. http_503: The HTTP response code returned by the upstream server is 503. HTTP_504: The HTTP response code returned by the upstream server is 504. HTTP_404: The HTTP response code returned by the upstream server is 404. Off: Turn off Proxy_next_upstream function One error, select another upstream server to forward again.
Summary: Nginx reverse proxy is still very convenient to use, the appropriate configuration can be modified.

Original address: https://my.oschina.net/u/1038053/blog/619993

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.