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/d7.access.log; 
  error_log /var/log/nginx/d7.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/d7/; # 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 drupal

http://example.com/index.php?q=node/3

## Drupal clean URL rewrite rule in nginx. 
location @drupal { ## location com nome
  rewrite ^/(.*)$ /index.php?q=$1 last;
}

if in location

cache Drupal — Boost


location @cache {
  ## If there's a cookie with a user ID then that means that
  ## it's a logged in user. Relay to Drupal.
  if ($http_cookie ~ "DRUPAL_UID") {
    return 405;
  }
  (...)
 }

Linguagem configuração

Achtung! procedimentais

Nginx + Drupal

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;
}

Config zero rewrites

Ficheiros privados rápidos

Nginx + PHP

php-fpm

Usar o Monit

exemplo configuração

### Monitoring php-fpm: the parent process.
check process php-cgi with pidfile /var/run/php-fpm.sock
  group phpcgi # phpcgi group
  start program = "/etc/init.d/php5-fpm start"
  stop program  = "/etc/init.d/php5-fpm stop"
  ## Test the UNIX socket. Restart if down.
  if failed unixsocket /var/run/php-fpm.sock 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/drupal-with-nginx

Nginx bleeding edge

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

E ainda