Main directives

The first set of directives will allow you to establish a basic configuration such as the location of the backend server, information to be passed, and how it should be passed:

Directive

Description

proxy_pass

Context: locationif

Specifies that the request should be forwarded to the backend server by indicating its location:

  • For regular HTTP forwarding, the syntax is proxy_pass http://hostname:port;
  • For Unix domain sockets, the syntax is proxy_pass http://unix:/path/to/file.socket;
  • You may also refer to upstream blocks proxy_pass http://myblock;
  • Instead of http://, you can use https:// for secure traffic. Additional URI parts as well as the use of variables are allowed.

Examples:

  • proxy_pass http://localhost:8080;
  • proxy_pass http://127.0.0.1:8080;
  • proxy_pass http://unix:/tmp/nginx.sock;
  • proxy_pass https://192.168.0.1;
  • proxy_pass http://localhost:8080/uri/;
  • proxy_pass http://unix:/tmp/nginx.sock:/uri/;
  • proxy_pass http://$server_name:8080;

Using an upstream block:

upstream backend {   
    server 127.0.0.1:8080;   
    server 127.0.0.1:8081;   
}   
location ~* .php$   
{   
    proxy_pass http://backend;   
}   

proxy_method

Context: httpserverlocation

Allows the overriding of the HTTP method of the request to be forwarded to the backend server. If you specify POST, for example, all requests forwarded to the backend server will be POST requests.

Syntax: proxy_method method;

Example: proxy_method POST;

proxy_hide_header

Context: http, server, location

By default, as Nginx prepares the response received from the backend server to be forwarded back to the client, it ignores some of the headers, such as Date, Server, X-Pad, and X-Accel-*. With this directive, you can specify an additional header line to be hidden from the client. You may insert this directive multiple times with one header name for each.

Syntax: proxy_hide_header header_name;

Example: proxy_hide_header Cache-Control;

proxy_pass_header

Context: http, server, location

Related to the previous directive, this directive forces some of the ignored headers to be passed on to the client.

Syntax: proxy_pass_header headername;

Example: proxy_pass_header Date;

proxy_pass_request_body

proxy_pass_request_headers

Context: http, server, location

Defines whether or not, respectively, the request body and extra request headers should be passed on to the backend server.

Syntax: on or off;

Default: on

proxy_redirect

Context: http, server, location

Allows you to rewrite the URL appearing in the Location HTTP header on redirections triggered by the backend server.

Syntax: off, default, or the URL of your choice

off: Redirections are forwarded as it is.

default: The value of the proxy_pass directive is used as the hostname and the current path of the document is appended. Note that the proxy_redirect directive must be inserted after the proxy_pass directive as the configuration is parsed sequentially.

URL: Replace a part of the URL by another.

Additionally, you may use variables in the rewritten URL.

Examples:

  • proxy_redirect off;
  • proxy_redirect default;
  • proxy_redirect http://localhost:8080/ http://example.com/;
  • proxy_redirect http://localhost:8080/wiki/ /w/;
  • proxy_redirect http://localhost:8080/ http://$host/;

proxy_next_upstream

Context: http, server, location

When proxy_pass is connected to an upstream block, this directive defines the cases where requests should be abandoned and resent to the next upstream server of the block. The directive accepts a combination of values among the following:

  • error: An error occurred while communicating or attempting to communicate with the server
  • timeout: A timeout occurs during transfers or connection attempts
  • invalid_header: The backend server returned an empty or invalid response
  • http_500, http_502, http_503, http_504, http_40: In case such HTTP errors occur, Nginx switches to the next upstream
  • off: Forbids you from using the next upstream server

Examples:

  • proxy_next_upstream error timeout http_504;
  • proxy_next_upstream timeout invalid_header;

proxy_next_upstream_timeout

Context: http, server, location

Defines the timeout to be used in conjunction with proxy_next_upstream. Setting this directive to 0 disables it.

Syntax: Time value (in seconds)

proxy_next_upstream_tries

Context: http, server, location

Defines the maximum number of upstream servers tried before returning an error message, to be used in conjunction with proxy_next_upstream.

Syntax: Numeric value (default: 0)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.169