# inspired by http://weichhold.com/2008/09/12/django-nginx-memcached-the-dynamic-trio/ # /etc/nginx/sites-enabled/default upstream backend_get { server 127.0.0.1:8080 weight=1; # any number of servers can be added here to distribute load. } upstream backend_post { server 127.0.0.1:8081 weight=1; } server { listen 80; server_name localhost; access_log /var/log/nginx/localhost.access.log; location / { proxy_read_timeout 300; if ($request_method = POST) { proxy_pass http://backend_post; break; } # update: after http://simonwillison.net/2008/Oct/14/django/ # use master while the cookie lasts if ($http_cookie ~* "force_master=([^;]+)(?:;|$)") { proxy_pass http://backend_post; break; } proxy_pass http://backend_get; } } # PostCookieMiddleware class PostCookieMiddleware(object): """ cookie force_master to take care of replication lag """ def process_response(self, request, response): if request.method == "POST": # for next 10 seconds send all requests for this user to master response.set_cookie("force_master", "42", max_age=10) return response