Django pod serwerem nginx

Nginx to szybki serwer HTTP i Reverse Proxy. Obsługuje m.in. protokół FastCGI i może być stosowany do obsługi Django. Paru deweloperów Ruby on Rails poleca to rozwiązanie nad Lighttpd.
Mamy dwa możliwe rozwiązania - nginx jako reverse proxy dla kilku działających serwerów z działającą aplikacją django lub też nginx obsługujący django z pomocą FastCGI.

Instalacja Nginx

- sprawdź repozytoria dystrybucji, jeżeli nie ma pakietu to skompiluj standardowo ze źródeł (sprawdź ./configure --help dla dodatkowych opcji)
- po instalacji konfiguracja powinna znajdować się w /etc/nginx/ a serwer uruchomić można poleceniem:
/etc/coś/nginx start


Nginx i Django poprzez FastCGI

Wykorzystaj poniższą konfigurację i wstaw ją do nginx.conf:
user  apache apache;

worker_processes  2;

error_log /var/log/nginx/error_log info;

events {
	worker_connections  1024;
	use epoll;
}

http {
	include		/etc/nginx/mime.types;
	default_type	application/octet-stream;

	log_format main
		'$remote_addr - $remote_user [$time_local] '
        	'"$request" $status $bytes_sent '
		'"$http_referer" "$http_user_agent" '
		'"$gzip_ratio"';

	client_header_timeout	10m;
	client_body_timeout	10m;
	send_timeout		10m;

	connection_pool_size		256;
	client_header_buffer_size	1k;
	large_client_header_buffers	4 2k;
	request_pool_size		4k;

	gzip on;
	gzip_min_length	1100;
	gzip_buffers	4 8k;
	gzip_types	text/plain;

	output_buffers	1 32k;
	postpone_output	1460;

	sendfile	on;
	tcp_nopush	on;
	tcp_nodelay	on;

	keepalive_timeout	75 20;

	ignore_invalid_headers	on;
	index index.html;

	server {
		listen 80;
		server_name localhost;
		# site_media - folder in uri for static files
		location /site_media  {
			root /path/to/media/folder;
			}
		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) {
		access_log   off; # po co mi logi obrazków :)
		expires      30d; 
		}
		location / {
			# host and port to fastcgi server
			fastcgi_pass 127.0.0.1:8080;
			fastcgi_param PATH_INFO $fastcgi_script_name;
			fastcgi_param REQUEST_METHOD $request_method;
			fastcgi_param QUERY_STRING $query_string;
			fastcgi_param CONTENT_TYPE $content_type;
			fastcgi_param CONTENT_LENGTH $content_length;
			fastcgi_pass_header Authorization;
			fastcgi_intercept_errors off;
			}
		access_log	/var/log/nginx/localhost.access_log main;
		error_log	/var/log/nginx/localhost.error_log;
		}
	}
}
Jedyne co musisz zmodyfikować to:
server_name localhost;
# site_media - katalog z plikami statycznymi, w django widziany w tym przykładzie jako /site_media - 
location /site_media  {
	root /path/to/media/folder;
	}
- Wystartuj django w "trybie" FastCGI:
python manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings
Jeżeli chcesz dodać coś do PYTHONPATH:
python manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings --pythonpath=/ścieżka/gdzie/daleko/
- Uruchom nginx, pod http://localhost/ powinieneś widzieć swoją stronę

Reverse proxy dla Django+SCGI+Cherokee

Edytuj cherokee.conf i zmień:
Port 80
- Na:
Port 8000
- Uruchom wszystko (djang-scgi.py, cherokee), sprawdź czy http://localhost:8000/ działa
- Edytuj nginx.conf i wklej:
user  apache apache;

worker_processes  2;

error_log /var/log/nginx/error_log info;

events {
	worker_connections  1024;
	use epoll;
}

http {
	include		/etc/nginx/mime.types;
	default_type	application/octet-stream;

	log_format main
		'$remote_addr - $remote_user [$time_local] '
        	'"$request" $status $bytes_sent '
		'"$http_referer" "$http_user_agent" '
		'"$gzip_ratio"';

	client_header_timeout	10m;
	client_body_timeout	10m;
	send_timeout		10m;

	connection_pool_size		256;
	client_header_buffer_size	1k;
	large_client_header_buffers	4 2k;
	request_pool_size		4k;

	gzip on;
	gzip_min_length	1100;
	gzip_buffers	4 8k;
	gzip_types	text/plain;

	output_buffers	1 32k;
	postpone_output	1460;

	sendfile	on;
	tcp_nopush	on;
	tcp_nodelay	on;

	keepalive_timeout	75 20;

	ignore_invalid_headers	on;
	index index.html;

 upstream django {
        server 127.0.0.1:8000;
    }


	server {
		listen 80;
		server_name localhost;
		location / {
			proxy_pass  http://django;
			proxy_redirect  default;
			}
		access_log	/var/log/nginx/localhost.access_log main;
		error_log	/var/log/nginx/localhost.error_log;
		}
	}
}
Mamy tu dwa ważne elementy:
 upstream django {
        server 127.0.0.1:8000;
    }
 upstream ETYKIETA {
        server IP:PORT;
        server IP:PORT;
        server IP:PORT;
    }
Upstream definiuje grupę serwerów, do których będzie przekierowywał ruch, dzieląc rządania pośród dostępne. W naszym przypadku mamy jeden działający proces cherokee na porcie 8000. Oprócz tego mamy:
location / {
	proxy_pass  http://django;
	}
location / {
	proxy_pass  http://ETYKIETA;
	}
Określa poprzez etykietę na jakie serwery przekierowywać ruch.
- Uruchom nginx i odwiedź http://localhost - wszystko powinno śmigać.
RkBlog

Django, 14 July 2008

Comment article
Comment article RkBlog main page Search RSS Contact