Mastering Nginx

(Ron) #1
Chapter 7

[ 163 ]

If your application reads request arguments to construct a page, then the $memcached_


key should include these as well:


location / {

set $memcached_key "$uri?$args";

memcached_pass 127.0.0.1:11211;

}

If the key is not present, NGINX will need a means of requesting the page from
the application. Hopefully, the application will then write the key/value pair into


memcached so that the next request can be directly served from memory. NGINX


will report a "Not Found" error if the key couldn't be found in memcached, so
the best way to then pass the request to the application is to use the error_page


directive and a location to handle the request. We should also include the error
codes for a "Bad Gateway" error and a "Gateway Timeout" error, in case memcached


does not respond to our key lookup:


server {

location / {

set $memcached_key "$uri?$args";

memcached_pass 127.0.0.1:11211;

error_page 404 502 504 = @app;

}

location @app {

proxy_pass 127.0.0.1:8080;

}

}

Remember that by using the equals sign (=) in the arguments to error_page, NGINX


will substitute in the return code from the last argument. This enables us to turn an
error condition into a normal response.

Free download pdf