Nginx memcached with backup remote service

I cannot get Nginx to work with the memcached module, the requirement is to request a remote service, cache data in memcached and never retrieve the remote endpoint until the backend invalidates the cache. I have 2 containers with memcached v1.4.35 and one with Nginx v1.11.10.

The configuration is as follows:

upstream http_memcached {
  server 172.17.0.6:11211;
  server 172.17.0.7:11211;
}

upstream remote {
  server api.example.com:443;
  keepalive 16;
}

server {
  listen 80;

  location / {
    set $memcached_key "$uri?$args";
    memcached_pass http_memcached;
    error_page     404 502 504 = @remote;
  }

  location @remote {
    internal;
    proxy_pass https://remote;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
  }
}

I tried to install memcached upstream incorrectly, but instead I get HTTP 499 and warnings:

* 3 upstream server is temporarily disconnected when connected to upstream

It seems that with the configuration described, Nginx can successfully get memcached, but cannot write or read. I can write and read memcached with telnet successfully.

Can you help me?

+6
1

,

1. 499

HTTP 499 - nginx, (http://lxr.nginx.org/source/src/http/ngx_http_request.h#0120)

,

nc -k -l 172.17.0.6 172.17.0.6:11211

- , Ctrl + C -

2.

, nginx memcached . memcached, ( error_log ... info). , , nginx memcached, , .

http://nginx.org/en/docs/http/ngx_http_memcached_module.html#memcached_bind -b telnet, , memcached telnet

3. nginx memcached ,

Nginx memcached (http://nginx.org/en/docs/http/ngx_http_memcached_module.html):

ngx_http_memcached_module memcached. $memcached_key. memcached , Nginx.

4.

, .

  • nginx upstream . , memcached . , memcached_next_upstream not_found, , . , 2 , , 20

  • memcached - = > ,

5.

10 - ​​ , . , , , 2 memcached -vv, , (memcached -p 11211 -U o -vv), - f , , .

6.

nginx config:

https http/1.1 , .

upstream http_memcached {                                                       
    server 127.0.0.1:11211;                                                     
    server 127.0.0.1:11212;                                                     
}                                                                               

upstream remote {                                                               
    server 127.0.0.1:8080;                                                      
}                                                                               

server {                                                                        
    listen 80;                                                                  
    server_name server.lan;                                                     
    access_log /var/log/nginx/server.access.log;                                
    error_log /var/log/nginx/server.error.log info;                             

    location / {                                                                
        set $memcached_key "$uri?$args";                                        
        memcached_next_upstream not_found;                                      
        memcached_pass http_memcached;                                          
        error_page     404 = @remote;                                           
    }                                                                           

    location @remote {                                                          
        internal;                                                               
        access_log /var/log/nginx/server.fallback.access.log;                   
        proxy_pass http://remote;                                               
        proxy_set_header Connection "";                                         
    }                                                                           
}            

server.py

(python):

from random import randint

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello: {}\n'.format(randint(1, 100000))

( )

FLASK_APP=server.py [flask][2] run -p 8080    

memcached:

$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set /? 0 900 5
cache
STORED
quit
Connection closed by foreign host.

:

, ,

$ curl http://server.lan && echo                                  
cache

$ curl http://server.lan  && echo                                 
cache

$ curl http://server.lan && echo                                  
cache

, server.py

$ curl http://server.lan/?q=1 && echo
Hello: 32337

:

2

memcached -p 11211 -U o -vv

memcached -p 11212 -U o -vv

enter image description here

+2

Source: https://habr.com/ru/post/1015814/


All Articles