web2py/web2py — pull request #35 by rochacbruno (2012-09-22 14:10:33+00:00)
Title: Optimized script for nginx and uwsgi on ubuntu 12.04

I installed this script on several machines and after a month of testing I figured out the basic configuration for a performatic uwsgi.

Without this settings uwsgi can be a memory eater. (see the thread about wsgi by Bruce Wade on the list, this script was based on Bruce tips)

Explanations:

1. Use unix socket instead of tcp
2. set the app as master **master** it is useful for monitoring with uwsgitop
3. set the **processes** to 4 (but this depends on the installed machine, for web2py 4 is the minimum(
4. **harakiri** 60 - Every request that will take longer than the seconds specified in the harakiri timeout will be dropped and the corresponding worker recycled.
5. set the maximum amount of seconds to wait for a worker death during a graceful reload
6. **cpu-affinity** - http://lists.unbit.it/pipermail/uwsgi/2011-March/001594.html
7. **Stats** - output the stats on tmp so you can use $uwsgitop /tmp/stats.socket to monitoring the workers

## THE MOST IMPORTANT FOR WEB2PY
8. **limit-as** limit the address space usage of each uWSGI process using POSIX/UNIX setrlimit()
9. **max-requests** 2000 - set the maximum number of requests for each worker. When a worker reaches this number it will get recycled. You can use this option to dumb fight memory leaks (even if reload-on-as and reload-on-rss are more useful for this kind of problem)
10. **reload-on-as** recycle a workers when its address space usage is over the limit specified
11. **reload-on-rss** Works as reload-on-as but it control the physical unshared memory. You can enable both
12 **no-orphans** automatically kill workers without a master process
13 **vacuum** - automatically remove unix socket and pidfiles on server exit

I did a lot of tests and on a Linode 1024 machine with Ubuntu 12.04 that is the best setup.
If running a Linode 2048 (or any machine with 2GB RAM) the config can be doubled to:

**max-requests** - 4000, **reload-on-as** 512, **reload-on-rss** 192, **limit-as** 1024 , **processes** 8, **cpu-affinity** 3

It is important: http://lists.unbit.it/pipermail/uwsgi/2011-March/001594.html

Also on nginx.conf it is usefull to set the number of processes/workers - I am using 12

Code

```xml

python
/run/uwsgi/app/web2py/web2py.socket
/home/www-data/web2py/

4
60
8
1
/tmp/stats.socket
2000
512
256
192

```

and

```javascript
location / {
uwsgi_pass unix:///run/uwsgi/app/web2py/web2py.socket;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/$nginx_version;
}
```