1 | | I just recently grabbed a hosting setup on VPSLink.com and wanted to get Django up and running. I'd heard good things about nginx, but had never set it up or configured it before, so I figured I'd give it a try. There were a few little bumps I ran into, but in the end the setup worked pretty well so I wanted to share my setup and configuration experience with everyone here. |
| 1 | nginx is a reverse proxy and needs to connect to a server that contains django. This guide is for using [https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/ fastcgi], but you could instead use |
| 2 | [http://bartek.im/blog/2009/04/06/setting-up-nginx-django.html apache+mod_wsgi], |
| 3 | [http://senko.net/en/django-nginx-gunicorn/ gunicorn], |
| 4 | [https://www.google.com/search?q=nginx+uwsgi+django uwsgi], or |
| 5 | [http://www.rkblog.rk.edu.pl/w/p/django-nginx/#3 SCGI+Cherokee] |
12 | | aptitude install subversion |
13 | | cd / |
14 | | mkdir django |
15 | | cd django |
16 | | svn co http://code.djangoproject.com/svn/django/trunk/ |
17 | | cd trunk |
18 | | python setup.py install |
19 | | }}} |
20 | | |
21 | | Next, to run Django through fastcgi, we need python-flup: |
22 | | {{{ |
23 | | aptitude install python-flup |
24 | | }}} |
25 | | |
26 | | Let's start a sample project just to make things easy: |
27 | | (Note: django-admin.py *should be* in your path and executable after a proper install, if this is not the case you may need to fix your installation or environment) |
28 | | {{{ |
29 | | cd / |
30 | | mkdir projects |
31 | | cd projects/ |
32 | | django-admin.py startproject sample_project |
33 | | }}} |
34 | | |
35 | | Also, create a media directory: |
36 | | {{{ |
37 | | cd / |
38 | | mkdir media |
39 | | }}} |
40 | | |
41 | | Let's start up the fastcgi setup: |
42 | | (Note: I didn't need any extra stuff on my pythonpath, but if you do, just add --pythonpath=/path/to/add) |
43 | | (Note: Nothing should print here) |
44 | | {{{ |
45 | | cd /projects/sample_project/ |
46 | | python manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings |
| 17 | python ./manage.py runfcgi host=127.0.0.1 port=8080 |
74 | | worker_processes 2; |
75 | | |
76 | | error_log /var/log/nginx/error_log info; |
77 | | |
78 | | events { |
79 | | worker_connections 1024; |
80 | | use epoll; |
81 | | } |
82 | | |
83 | | http { |
84 | | include /etc/nginx/mime.types; |
85 | | default_type application/octet-stream; |
86 | | |
87 | | log_format main |
88 | | '$remote_addr - $remote_user [$time_local] ' |
89 | | '"$request" $status $bytes_sent ' |
90 | | '"$http_referer" "$http_user_agent" ' |
91 | | '"$gzip_ratio"'; |
92 | | |
93 | | client_header_timeout 10m; |
94 | | client_body_timeout 10m; |
95 | | send_timeout 10m; |
96 | | |
97 | | connection_pool_size 256; |
98 | | client_header_buffer_size 1k; |
99 | | large_client_header_buffers 4 2k; |
100 | | request_pool_size 4k; |
101 | | |
102 | | gzip on; |
103 | | gzip_min_length 1100; |
104 | | gzip_buffers 4 8k; |
105 | | gzip_types text/plain; |
106 | | |
107 | | output_buffers 1 32k; |
108 | | postpone_output 1460; |
109 | | |
110 | | sendfile on; |
111 | | tcp_nopush on; |
112 | | tcp_nodelay on; |
113 | | |
114 | | keepalive_timeout 75 20; |
115 | | |
116 | | ignore_invalid_headers on; |
117 | | index index.html; |
118 | | |
119 | | server { |
120 | | listen 80; |
121 | | server_name localhost; |
122 | | location /site_media { |
123 | | root /media/; # Notice this is the /media folder that we create above |
124 | | |
125 | | location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) { |
126 | | access_log off; |
127 | | expires 30d; |
128 | | } |
129 | | } |
130 | | |
131 | | location / { |
132 | | # host and port to fastcgi server |
133 | | fastcgi_pass 127.0.0.1:8080; |
134 | | fastcgi_param PATH_INFO $fastcgi_script_name; |
135 | | fastcgi_param REQUEST_METHOD $request_method; |
136 | | fastcgi_param QUERY_STRING $query_string; |
137 | | fastcgi_param CONTENT_TYPE $content_type; |
138 | | fastcgi_param CONTENT_LENGTH $content_length; |
139 | | fastcgi_pass_header Authorization; |
140 | | fastcgi_intercept_errors off; |
141 | | fastcgi_param REMOTE_ADDR $remote_addr; |
142 | | } |
143 | | access_log /var/log/nginx/localhost.access_log main; |
144 | | error_log /var/log/nginx/localhost.error_log; |
145 | | } |
| 46 | location / { |
| 47 | include fastcgi_params; |
| 48 | fastcgi_pass 127.0.0.1:8080; |
| 49 | } |