Mastering Nginx

(Ron) #1
Chapter 7

[ 177 ]


  • $r->header_in(header): The value of the specified request header.

  • $r->header_only: Instructs NGINX to return only the header to the client.

  • $r->header_out(header, value): Sets the specified response header to
    this value.

  • $r->internal_redirect(uri): Makes an internal redirect to the specified
    URI once the Perl handler has completed execution.

  • $r->print(text): Prints the specified text out to the client.

  • $r->request_body: The body of the request, if it fits in memory.

  • $r->request_body_file: The body of the request, if written out to a
    temporary file.

  • $r->request_method: The HTTP method of the request.

  • $r->remote_addr: The client's IP address.

  • $r->flush: Immediately send data to the client.

  • $r->sendfile(name[, offset[, length]]): Sends the specified file to
    the client, with an optional offset and length, once the Perl handler has
    completed execution.

  • $r->send_http_header([type]): Sends the response headers to the client,
    with an optional content type.

  • $r->status(code): Sets the HTTP status of the response.

  • $r->sleep(milliseconds, handler): Sets a timer to execute the handler
    after having waited the specified number of milliseconds. NGINX will
    continue processing other requests while the timer is running.

  • $r->unescape(text): Decodes URI-encoded text.

  • $r->uri: The URI in the request.

  • $r->variable(name[, value]): Either returns a named, request-local
    variable or sets one to the specified value.


The perl module may also be used within Server Side Includes. An SSI command


using Perl has the following format:


<!--# perl sub="module::function" arg="parameter1" arg="parameter2"
... -->

Let's take a look at an example of using the perl module. Our goal is to pass requests


to a different upstream server, as determined by the first letter of the request URI. We


could implement this as a series of locations in NGINX, but it will be more concise
expressed as a Perl handler.

Free download pdf