Przykładowe konfiguracje Nginx
14 July 2008
Comments
[toc]Load Balancing - Rozkład ruchu na kilka serwerów
Używamy modułu upstream by zdefiniować kilka serwerów serwujących naszą stronę. Następnie dla bloku "serwer" przekazujemy połączenia tym serwerom za pomocąproxy_pass http://NAZWA_LISTY_UPSTREAM
http {
upstream myproject {
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen: 80;
server_name: www.domain.com;
location / {
proxy_pass http://myproject;
}
}
}VirtualHost
Dla różnych katalogów można przypisać domenę za pomocą server_name. Konfiguracja dla poszczególnych serwerów zawiera się w blokach "server". Można też dołączać (include) pliki konfiguracyjne dla poszczególnych virtual hostów. Oto przykład dwóch virtualhostów serwujących pliki statyczne:http {
server {
listen 80;
server_name www.domain1.com;
access_log logs/domain1.access.log main;
location / {
index index.html;
root /var/www/domain1.com/htdocs;
}
}
server {
listen 80;
server_name www.domain2.com;
access_log logs/domain2.access.log main;
location / {
index index.html;
root /var/www/domain2.com/htdocs;
}
}
}Serwer dla statycznych plików
Oto pełna konfiguracja dla prostego serwera serwującego statyczną treść:user www-data www-data;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server {
listen 80;
server_name moja-domena.com www.inna-domena.com;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
}PHP poprzez FastCGI
Nginx nie uruchamia sam procesów FastCGI i trzeba to zrobić innym programem. By dodać obsługę PHP należy do "location" naszej konfiguracji dodać:location ~ \.php$ {
fastcgi_pass 127.0.0.1:12345;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}Pełna konfiguracja:
user www-data www-data;
worker_processes 2;
error_log logs/error.log debug;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server {
listen 80;
server_name moja-domena.com www.server-name.com;
access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:12345;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}
}
}DokuWiki pod Nginx
Oto przykład z wiki skryptu wraz z regułami rewrite:
server {
listen 80;
server_name _ *;
port_in_redirect off;
optimize_server_names off;
access_log /var/log/nginx/localhost.access.log;
rewrite ^(/dokuwiki/)_media/(.*) $1lib/exe/fetch.php?media=$2 last;
rewrite ^(/dokuwiki/)_detail/(.*) $1lib/exe/detail.php?media=$2 last;
rewrite ^(/dokuwiki/)_export/([^/]+)/(.*) $1doku.php?do=export_$2&id=$3 last;
location / {
root /var/www;
index index.html index.htm index.php;
}
location /dokuwiki/ {
if (!-f $request_filename) {
rewrite ^(/dokuwiki/)(.*)?(.*) $1doku.php?id=$2&$3 last;
rewrite ^(/dokuwiki/)$ $1doku.php last;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:8888;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}PHP + Drupal + Nginx
Nginx, Fastcgi, PHP, rewrite config for Drupal - Wraz ze skryptem tworzącym procesy FastCGI PHPRunning Drupal with Clean URL on Nginx or Lighttpd
Frameworki Django i Pylons
PylonsDjango
PHP + Ruby on Rails + Django
Inne Przykłady
Using Tracd with Nginx in Cluster ModeNginx, my new favorite front end for mongrel cluster
Nginx jako Load Balancer dla kilku Mongreli (RoR)
nginx: the front end solution for rails deployment?
RkBlog
Comment article