Nginx

notas de um subterrâneo

História

Software livre

Licença BSD

Redistribution and use in source and
binary forms, with or without
modification, are permitted provided 
that the following conditions (...)
THIS SOFTWARE IS PROVIDED BY AUTHOR
AND CONTRIBUTORS ``AS IS'' (...)
FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.

Casos de sucesso

exemplos

Aplicação

Arquitectura I

Arquitectura II

Funcionalidades

Arquitectura III

Arquitectura IV

Exemplo

./configure --conf-path=/etc/nginx/nginx.conf 
            --error-log-path=/var/log/nginx/error.log 
            --http-client-body-temp-path=/var/lib/nginx/body 
            --http-fastcgi-temp-path=/var/lib/nginx/fastcgi 
            --http-log-path=/var/log/nginx/access.log 
            --http-proxy-temp-path=/var/lib/nginx/proxy 
            --lock-path=/var/lock/nginx.lock 
            --pid-path=/var/run/nginx.pid 
            --with-debug 
            --with-http_flv_module 
            --with-http_geoip_module 
            --with-http_gzip_static_module 
            --with-http_realip_module 
            --with-http_stub_status_module 
            --with-http_ssl_module 
            --with-http_sub_module 
            --with-ipv6 
            --with-http_secure_link_module 
            --add-module=$(CURDIR)/modules/nginx-upstream-fair 
            --add-module=$(CURDIR)/modules/nginx-upload-progress

Upgrade sem downtime

Módulos principais

Outros módulos

E ainda

contextos hierárquicos

global

afecta todos os contextos


## Definido no módulo Main.
user www-data; # utilizador processos filho

worker_processes  1; # número de processos filho

error_log  /var/log/nginx/error.log; # log de erros

pid        /var/run/nginx.pid;  # ficheiro PID

worker_rlimit_nofile 8192; # número máximo de descriptores

http

Afecta uma instância de servidor HTTP

http {
  ## MIME types.
  include /etc/nginx/mime.types;

  ## FastCGI.             
  include /etc/nginx/fastcgi.conf;

  ## MIME type pré-definido.
  default_type  application/octet-stream;

  ## Ficheiro de log.
  access_log /var/log/nginx/access.log;
  (...)
}

server

server {
  listen [::]:80; # IPv6 port 80
  limit_conn arbeit 10; # nº máximo conexões simultâneas/cliente

  ## Logs.
  access_log /var/log/nginx/wp.access.log; 
  error_log /var/log/nginx/wp.error.log;

  ## Include the blacklist.conf file.
  include sites-available/blacklist.conf;

  ## Disable all methods besides HEAD, GET and POST.
  if ($request_method !~ ^(GET|HEAD|POST)$) {
    return 444;
  }

  root  /var/www/sites/wp/; # raíz do site
  index index.php index.html; # indíce
  (...)
}

Hosts virtuais

server { ## host 1
  listen          80; 
  server_name     www.domain1.com;
  access_log      logs/domain1.access.log main;
 
  index index.html;
  root  /var/www/domain1.com/htdocs;
}
server { ## host 2
  listen          80; 
  server_name     www.domain2.com;
  access_log      logs/domain2.access.log main;

  index index.html;
  root  /var/www/domain2.com/htdocs;
}

Locations estáticas

mapeadas no filesystem


## Support for favicon. Return a 204 (No Content) 
## if the favicon doesn't exist.
location = /favicon.ico {
  try_files /favicon.ico =204;
}

Locations dinâmicas

exemplo wordpress

http://example.com/2011/01/28/lorem

## Tenta fs primeiro sempre (stat syscall) 
location @nocache { ## location com nome
   try_files $uri $uri/ /index.php?q=$uri&$args;
}

if in location

WP Super Cache — WP Super Cache


## If there's a cookie then bust the cache.
if ($http_cookie ~ wordpress_logged_in) {
    return 405;
}

Nginx + WordPress.com

Ficheiros estáticos

servidos directamente


## All static files will be served directly.
location ~* ^.+\.(?:css|js|jpg|jpeg|gif|ico|png|html|xml)$ {
   access_log        off;
   expires           30d;
}

Nginx + PHP

PHP FastCGI

Usar o Monit

exemplo configuração


### Monitoring php-cgi: the parent process.
check process php-cgi with pidfile /var/run/php-cgi.pid
  group phpcgi # phpcgi group
  start program = "/etc/init.d/php-cgi start"
  stop program  = "/etc/init.d/php-cgi stop"
  ## Test the UNIX socket. Restart if down.
  if failed unixsocket /tmp/php-cgi/php-cgi.socket then restart
  ## If the restarts attempts fail then alert.   
  if 3 restarts within 5 cycles then timeout
    alert root@localhost only on {timeout}

Upload progress

Há muito mais

Já agora...

A minha configuração exemplo: github.com/perusio/wordpress-nginx

Nginx bleeding edge

O meu repositório Debian com a versão mais recente do Nginx.

E ainda