Using ngx_http_memcached_module in Nginx

nginx ngx_http_memcached_module memcached_next_upstream
18 July 2025

Introduction to the ngx_http_memcached_module in Nginx


The ngx_http_memcached_module module in Nginx provides you with the ability to use Memcached for storing information. Memcached is a distributed caching system that can effectively improve the performance of websites. Using this module allows developers to send HTTP requests to the Memcached server and receive faster cached responses.


Now, let's explore how we can utilize this module, along with the configurations that need to be performed. One important point to keep in mind is that for using this module, Memcached must be installed and running on your server.


One of the key parameters you should be familiar with is the memcached_next_upstream. This directive determines what actions Nginx should take if there is a problem connecting to Memcached. For example, you can specify whether to send the request to the next server.


Now that we are familiar with the basic concepts, let's take a look at the necessary configurations and directives needed to use this module.


ngx_http_memcached_module Configurations


location /memcached {
default_type application/octet-stream;
memcached_pass 127.0.0.1:11211;
error_page 404 =200;
}

Code Explanation:


location /memcached

This line specifies that any request sent to the URI «/memcached» will be handled by this location block.


default_type application/octet-stream;

This line sets the default response type, which in this case is configured to be «application/octet-stream».


memcached_pass 127.0.0.1:11211;

This line specifies the address and port of the Memcached server, here referencing the local address (localhost) and port 11211.


error_page 404 =200;

This line indicates that if a 404 error (meaning the resource was not found) occurs, Nginx will return a successful response (200) instead of an error message.

FAQ

?

What is the purpose of the ngx_http_memcached_module?

?

How can I configure the Memcached server address in Nginx?

?

Can I modify Nginx's behavior when encountering errors?