Nginx with jar and memcached returns some garbled characters

I am trying to cache Python / flask responses with memcached. Then I want to use the cache using nginx. I am using flag code that looks something like this:

from flask import Flask, render_template from werkzeug.contrib.cache import MemcachedCache app = Flask(__name__) cache = MemcachedCache(['127.0.0.1:11211']) @app.route('/') def index(): index = cache.get('request:/') if index == None: index = render_template('index.html') cache.set('request:/', index, timeout=5 * 60) return index if __name__ == "__main__": app.run() 

and the nginx site configuration, which looks something like this:

 server { listen 80; location / { set $memcached_key "request:$request_uri"; memcached_pass 127.0.0.1:11211; error_page 404 405 502 = @cache_miss; } location @cache_miss { uwsgi_pass unix:///tmp/uwsgi.sock; include uwsgi_params; error_page 404 /404.html; } } 

However, when it pulls out of the cache, the html code has the prefix V, contains the characters \ u000a (linear channels) and distorted local characters and the suffix "p1". as such:

 V<!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\u000a<head>\u000a <meta http-equiv="content-type" content="text/html; charset=UTF-8" />\u000a <meta http-equiv="content-language" content="no">\u000a\u000a <title> [...] \u000a\u000a</body>\u000a</html> p1 . 

Even though Content-Type is "text / html; charset = utf-8". Presumably V [...] p1. a thing may have something with a chunk encoding something, a flag that is not in the response header. What should I do?

+6
source share
1 answer

I, I fixed it! The nginx configuration was correct, before I changed chunked, the python / flask code should have been:

 @app.route('/') def index(): rv = cache.get('request:/') if rv == None: rv = render_template('index.html') cachable = make_response(rv).data cache.set('request:/', cachable, timeout=5 * 60) return rv 

That is, I should only cache the data, and this can only be done, afaik, if I do make_response first

+4
source

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


All Articles