Mastering Nginx

(Ron) #1

NGINX as a Reverse Proxy


[ 70 ]

proxy_connect_timeout 30;

proxy_send_timeout 15;

proxy_read_timeout 15;

proxy_send_lowat 12000;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

We are setting a number of common directives to values that we think would be
useful for reverse-proxying scenarios:



  • The proxy_redirect directive has been set to off because there is no
    need to rewrite the Location header in most situations.

  • The Host header is set so the upstream server can map the request to a
    virtual server or otherwise make use of the host portion of the URL the
    user entered.

  • The X-Real-IP and X-Forwarded-For headers serve similar purposes—
    to relay the information about the connecting client's IP address to the
    upstream server.


° The $remote_addr variable used in the X-Real-IP header is the
IP address of the client as NGINX perceives it.

° The $proxy_add_x_forwarded_for variable contains the contents
of the X-Forwarded-For header field from the client's request,
followed by the $remote_addr variable.


  • The client_max_body_size directive, while not strictly a proxy module
    directive, is mentioned here because of its relevance to proxy configurations.
    If this value is set too low, uploaded files will not make it to the upstream
    server. When setting this directive, keep in mind that files uploaded via a web
    form will usually have a larger file size than that shown in the filesystem.

  • The proxy_connect_timeout directive indicates how long NGINX will wait
    when establishing initial contact with the upstream server.

Free download pdf