NGINX Reverse Proxy for Upstream Django / Gunicorn Backend

I posted this on the nginx mailing list but didn't hear anything from anyone, so I thought I'd let him hack here on stackoverflow :)

I currently have a Django app hosted on Amazon EC2. All my data is served through Gunicorn on port 8000 (Python WSGI HTTP Server for UNIX. This is a pre-fork working model ported from the Ruby Unicorn project). I do not need to worry about transferring static content (images) to the client, because all this is handled by Amazon S3. Django passes the URL of the content through Gunicorn to the client via json. Then the client can download it.

My Django application is hosted on t1.micro instance. Here are the specifications provided by Amazon Web Services:

Processor: up to 2 EC2 processing units (for short periodic bursts). Virtual Cores: 1 Memory: 615 MiB Platform: 32-bit and 64-bit

I have 3 Gunicorn workers working next to my Django application on this instance.

For my Nginx Reverse proxy, I also use t1.micro instance. I set it up and everything works fine. Here is my etc / nginx / sites-enabled / default configuration as follows:

# Gunicorn server upstream django { server 10.0.10.0:8000; } # Serve static files and redirect any other request to Gunicorn server { listen 80; server_name 23.0.23.23/; #root /var/www/domain.com/; #access_log /var/log/nginx/domain.com.access.log; #error_log /var/log/nginx/domain.com.error.log; # Check if a file exists at /var/www/domain/ for the incoming request. # If it doesn't proxy to Gunicorn/Django. #try_files $uri @django; # Setup named location for Django requests and handle proxy details location @django { proxy_pass http://django; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

This setting is great, but it does not take into account proxy buffering for slow clients. It also does not take into account caching and does not take into account the number of working nginx that I need. How to configure compression? I found resources that say there is something called gzip, does it support json? How can I fine tune my nginx configuration to the specifications of the t1.micro instance?

If you are in my scenario, what settings would you use? Your answers and examples are greatly appreciated. Thanks:)

+8
django nginx gunicorn
source share
1 answer

Proxy Buffering

Typically, proxy buffering will only help you if you create very large web pages or send large files. it is fairly easy to set up, regardless, but you will need to adjust the buffer size to the size of your largest pages + 20% (any page that does not fit in the buffer is written to disk) or selectively includes proxy buffering your largest pages.

docs: http://wiki.nginx.org/HttpProxyModule#proxy_buffering

Caching

I know little about your application and how dynamic its content is, but setting up the correct generation of Cache Control / ETAG headers on your application will be the first thing you want to take a look at. This is what will allow Nginx to know what is safe for proxies. In addition, you can configure several cache zones to control the amount of space that your caches occupy on disk.

 proxy_cache one; proxy_cache_path /data/nginx/cache/one levels=1:2 max_size=1G keys_zone=one:1000m; 

You will need rules to bypass the cache (for debugging or programmatically)

 proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment; proxy_cache_bypass $http_pragma $http_authorization; 

You will also want your application to unconditionally serve the cache when your application throws errors:

 proxy_cache_use_stale error timeout invalid_header; 

docs:

Gzip

Enabling gzip on your site is always a trade-off between processor time and bandwidth. True, you can reduce the amount of data transmitted over the cable if you upload your content, but if you work in T1 Micro, you will greatly limit your bandwidth for proxying requests due to the use of the CPU. Generally, gzip is a much better idea for static content that you can pre-archive and then serve again and again.

(Yes, gzip supports json, but this is because gzip becomes a wire format and is transparently unpacked by the client. You should read Content-Encoding: gzip )

docs: http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

miscellanea

You will also want to set several different settings:

 # Directives turn off 404 error logging. location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { log_not_found off; } 
+5
source share

All Articles