RkBlog

Hardware, programming and astronomy tutorials and reviews.

Pylons pod serwerem nginx

Konfiguracja serwera nginx dla frameworka Pylons umożliwiająca produkcyjne hostowanie aplikacji Pylonsowych za pomocą tego serwera

Edytuj plik *ini (development.ini zazwyczaj) i zmień blok [server:main] na:
[server:main]
use = egg:PasteScript#flup_fcgi_thread
host = 0.0.0.0
port = 8080
- Jeżeli nie masz zainstalowane flupa to zainstaluj:
easy_install flup
- Edytuj /etc/nginx/nginx.conf do postaci:
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;
		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_param  SERVER_ADDR        $server_addr;
			fastcgi_param  SERVER_PORT        $server_port;
			fastcgi_param  SERVER_NAME        $server_name;
			fastcgi_param  SERVER_PROTOCOL    $server_protocol;
			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;
		}
	}
}
Gdzie ważnym elementem jest:
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_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
fastcgi_pass 127.0.0.1:8080; - oznacza że Paste/FastCGI działa na porcie 8080, host: localhost.
-Uruchom Paste i nginx:
paster serve --reload development.ini 
/etc/something/nginx start
- http://localhost/ powinno pokazać twój projekt w Pylons

Reverse Proxy i Pylons

Dla reverse proxy potrzeba kilku działających normalnie serwerów. Wykorzystamy dwa serwery Paste:
- Edytuj development.ini i skopiuj blok [server:main] i [app:main] jako [server:main2] i [app:main2]. Zmień port jednemu z nich. Przykład:
[server:main]
use = egg:Paste#http
host = 0.0.0.0
port = 8080

[app:main]
dsn = sqlite://@localhost/termdb
use = egg:searchengine
myghty_data_dir = %(here)s/data/templates
cache_data_dir = %(here)s/data/cache
session_data_dir = %(here)s/data/sessions
session_key = searchengine
session_secret = somesecret

[server:main2]
use = egg:Paste#http
host = 0.0.0.0
port = 8081

[app:main2]
dsn = sqlite://@localhost/termdb
use = egg:searchengine
myghty_data_dir = %(here)s/data/templates
cache_data_dir = %(here)s/data/cache
session_data_dir = %(here)s/data/sessions
session_key = searchengine
session_secret = somesecret
Edytuj /etc/nginx/nginx.conf do postaci:
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 pylons {
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
    }

	server {
		listen 80;
		server_name localhost;
		location / {
			proxy_pass  http://pylons;
			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 pylons {
        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://pylons;
	}
location / {
	proxy_pass  http://ETYKIETA;
	}
- Uruchom serwery Paste:
paster serve --reload development.ini
paster serve --reload development.ini --server-name=main2
- Uruchom nginx i odwiedź http://localhost/ - twoja strona w Pylons powinna śmigać.
RkBlog

Pylons, 14 July 2008,

Comment article