nginx & php fpm - the webserver you might actually like - php usergroup berlin

36
NGINX THE WEB SERVER YOU MIGHT ACTUALLY LIKE

Upload: edorian

Post on 25-Dec-2014

15.144 views

Category:

Technology


2 download

DESCRIPTION

Slides from the talk given at the Berlin PHP Usergroup 2012.11.06

TRANSCRIPT

Page 1: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

NGINXTHE WEB SERVER YOU MIGHT ACTUALLY LIKE

Page 2: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

ABOUT MESoftware EngineerPHP since 10 yearsCICleanCodeDevOpsTDDShippingBullet points

Page 3: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

INSTEAD OF ME

Page 4: Nginx & php fpm - the webserver you might actually like - php usergroup berlin
Page 5: Nginx & php fpm - the webserver you might actually like - php usergroup berlin
Page 6: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

LET'S GO

Page 7: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

WHY ANOTHER WEBSERVER?

Page 8: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

WHY NOT LIGHTTPD?

Page 9: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

THE BASICSIntroMultiple Servers / DomainsStatic contentCachingSSLError pagesRewritesAuthLoad BalancingProxyPHP!Fancy PHP!

Page 10: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

INTROsudo apt-get install nginx

/etc/nginx/nginx.conf/etc/nginx/conf.d/*.conf

Page 11: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

NGINX CONF BASICS/etc/nginx/nginx.conf

user nginx;worker_processes 4;worker_cpu_affinity 0001 0010 0100 1000;

error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;

events { worker_connections 1024;}

Page 12: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

NGINX CONF BASICS/etc/nginx/nginx.conf

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

access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65;

include /etc/nginx/conf.d/*.conf;}

Page 13: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

SERVERS/etc/nginx/conf.d/mySite.conf/etc/nginx/sites-enabled/wallbash

server { server_name wallbash.com wallbash.de; listen 80; root /var/www/myApp/html/ // ...}

server { server_name _; listen 80; root /var/www/myOtherApp/html/}

Page 14: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

STATIC CONTENT/etc/nginx/conf.d/anyConfig.conf

server { // ...

location / { }}

Page 15: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

FANCY STATIC CONTENTInside Server Blocks

location ~ ̂\/(js|img|css|downloads)\/ {}

location ~ \.(js|css|png|gif|jpg|pdf)$ {}

Page 16: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

CACHINGlocation ~ ̂\/(js|img|css)\/ { expires 14d;}

Page 17: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

DENY ACCESS TO ALL .DOT-FILESInside Server Blocks

location ~ /\. { access_log off; log_not_found off; deny all;}

Page 18: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

SSL

Or just

server { server_name _; listen 443; ssl on;}

server { listen 443 default_server ssl;}

Page 19: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

SSL - CONFIGssl_certificate wildcard.crt;ssl_certificate_key wildcard.key;

ssl_session_timeout 5m;ssl_session_cache shared:SSL:10m;

ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;ssl_prefer_server_ciphers on;ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM;ssl_ecdh_curve secp521r1;

Page 20: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

ERROR PAGES

STARTUP BONUS:

error_page 500 501 502 503 504 /500.html;

location /500.html { internal;}

server { server_name *nextBigThing.io;

location /500 { return 500; }}

Page 21: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

REWRITES

HTTPS ALL THE THINGS

OLDSCHOOL

server { server_name _; listen 80; rewrite ̂ https://$host$request_uri permanent;}

rewrite ̂/users/(.+)$ /show?user=$1? last;

Page 22: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

AUTHlocation / { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/conf.d/myApp.htpasswd;}

Page 23: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

LOAD BALANCINGupstream web_workers { server www1.example.com; server www2.example.com; server www3.example.com;}

Page 24: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

LOAD BALANCING LEGACYupstream web_workers { ip_hash; server www1.example.com; server www2.example.com; server www3.example.com;}

Page 25: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

PROXYlocation / { proxy_pass http://localhost:8080; proxy_set_header X-Real-IP $remote_addr; proxy_cache zone;

//Default: proxy_cache_key $scheme$proxy_host$uri$is_args$args;}

Page 26: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

PHP!

Page 27: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

PHP-FPM!?!FastCGI Process Manager

Page 28: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

PROCESS MANAGEMENT FOR THE MASSESThink: "supervisord"; But without caring

sudo apt-get install php5-fpm

/etc/php5/fpm/php-fpm.conf

// Don't restart the webserver, restart php :)sudo service php5-fpm restart

Page 29: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

FPM-CONFIG[myApp]listen = 9000;listen.allowed_clients = 127.0.0.1

user = phpgroup = php

request_terminate_timeout = 10

request_slowlog_timeout = 1slowlog = /var/log/php-fpm/myApp-slow.log

Page 30: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

FPM-CONFIG - PROCESS MANAGEMENTpm = dynamicpm.max_children = 50pm.start_servers = 5pm.min_spare_servers = 5pm.max_spare_servers = 35

Page 31: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

NGINX + PHPlocation / { fastcgi_pass 127.0.01.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/index.php; include fastcgi_params;}

Page 32: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

APPLICATION SERVERS!location / { fastcgi_pass anontherServer:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/index.php; include fastcgi_params;}

Page 33: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

SCALING!location / { fastcgi_pass workers; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/index.php; include fastcgi_params;}

upstream workers { server App1:9000; server App2:9000; server 192.168.10.3:9000;}

Page 35: Nginx & php fpm - the webserver you might actually like - php usergroup berlin

THANK YOU

Page 36: Nginx & php fpm - the webserver you might actually like - php usergroup berlin