Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Einführung in Nginx
Search
Dominik Siebel
September 27, 2012
Technology
0
240
Einführung in Nginx
Dominik Siebel
September 27, 2012
Tweet
Share
More Decks by Dominik Siebel
See All by Dominik Siebel
Einführung in Lucene Solr (PHPUG D/DU/KR)
dsiebel
0
99
Reguläre Ausdrucke (PCRE)
dsiebel
0
59
Other Decks in Technology
See All in Technology
回り回って効いてくる副次的効果としての技術広報/techpr
nishiuma
2
210
Google Cloudを組織(企業)で運用する時のベストプラクティス × 健康の環境分離戦略 #まるクラ勉強会
yasumuusan
0
180
Trade-offs all the way down
_aitor
1
100
生成AI活用推進の為にやったこと/やらなかったこと
ktc_wada
0
210
OPENLOGI Company Profile for engineer
hr01
1
2.2k
From here to resilience - a travel guide
ufried
1
160
ハードウェアを動かすTypeScriptの世界
9wick
3
1.2k
#phpconkagawa レガシーコードにもオブザーバビリティを 〜少しずつ始めるサービス監視〜
yamato_sorariku
0
560
テストコードを書きながらCompose Multiplatformを乗りこなす
subroh0508
0
150
複雑なビジネスルールに挑む:正確性と効率性を両立するfp-tsのチーム活用術 / Strike a balance between correctness and efficiency with fp-ts
kakehashi
5
3.7k
拓展QA日常工作的邊界
line_developers_tw
PRO
0
610
My road to OSEE Part1
yunolay
0
120
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
325
20k
Build The Right Thing And Hit Your Dates
maggiecrowley
25
2k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
117
18k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
It's Worth the Effort
3n
180
27k
Six Lessons from altMBA
skipperchong
22
3k
Building Adaptive Systems
keathley
32
1.9k
The Pragmatic Product Professional
lauravandoore
26
5.9k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
123
39k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
358
22k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
21
2k
Transcript
Einführung in Nginx PHP Usergroup D / DU / KR
Dominik Siebel
From Russia with love
Wer von Euch setzt Nginx ein?
Nginx ➔ Entw. ~ 2002 Igor Sysoev ➔ C10k Problem
➔ Events statt Threads ➔ Version 1.2.0 ➔ BSD Lizenz
Nginx @ ... Wordpress.com GitHub Pinterest Golem Wikimedia Imageshack Soundcloud
... → Stand August 2012: 11,53% aller Webserver (1) Netcraft Studie
Web Server Reverse Proxy Mail Proxy Cache Load Balancer Und
mehr...
Woes & Awesomes
Pro ➔ Non-Blocking / Event basiert ➔ Mehr gleichzeitige Verbindungen
➔ Weniger Last ➔ Geringer Speicherverbrauch ➔ auch unter hoher Last
Pro ➔ Perl Interpreter (+ PCRE support) ➔ On-The-Fly Updates
➔ “Einfache” Konfiguration ➔ Aktive und hilfsbereite Community
Contra ➔ Keine .htacces ➔ Module nicht dynamisch ladbar ➔
Lückenhafte Dokumentation
Module
Module ➔ Core ➔ HTTP ➔ HTTP Erweiterungen http://wiki.nginx.org/Modules
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf ➔ Allgemeine Server Configuration ➔ Includes
user www-data pid /var/run/nginx.pid http { access_log /var/log/nginx/access_log error_log /var/log/nginx/error_log
}
http { gzip on; gzip_disable “msie6”; }
http { include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
/etc/nginx/sites-available/*
server ➔ “Virtual Host”
server { listen 80; server_name www.example.com root /var/www/example.com }
server { listen 80; server_name www.example.com root /var/www/example.com }
server { listen 80; server_name www.example.com server_name *.example.com server_name ~^www\d+\.example\.com
server_name - root /var/www/example.com }
location ➔ Am häufigsten verwendeter Block
location / { try_files $uri $uri/ /index.html; }
location /nginx/is/awesome { return 202; }
location ~ /\. { deny all; } Hide “dotfiles”
Kontexte
Kontexte Http ➔ Allgemeine Container/HTTP Modul Konfiguration Server ➔ “Virtual
Host” Location ➔ URL Handling Upstream ➔ Backend Konfiguration (z.B. Für Load Balancing)
Variablen
Variablen $scheme $host $request_method $request_uri
Variablen $arg_PARAM ➔ Direkter Zugriff auf URL Parameter
Variablen $http_HEADER ➔ Direkter Zugriff auf HTTP Request Header
Variablen $request_time ➔ Zeitspanne zwischen write() und read()
Variablen $upstream_response_time ➔ Antwortzeit des Backends
Variablen http://wiki.nginx.org/HttpCoreModule#Variables
Direktiven
Direktiven add_header add_header Cache-Control debug; ➔ Response Header
Direktiven proxy_set_header ➔ Header an Backend Server
Direktiven expires location ~ ^.+\.(jpg|jpeg|js|png) { expires 2h; } ➔
Browser Cache nach 2 Stunden invalidieren
Direktiven sub_filter ➔ Rechtschreibfehler Quickfix ;)
Direktiven error_pages error_page 400 /errors/404.html error_page 500 501 502 503
504 /errors/whoopsie.html ➔ Failwhales
Direktiven empty_gif location = /empty { empty_gif; }
Direktiven if set map rewrite ➔ Machtige Kontrollwerkzeuge
Direktiven http://wiki.nginx.org/HttpCoreModule#Directives
map
map $request_method $not_allowed_method { default 1; GET 0; HEAD 0;
POST 0; } if ($not_allowed_method) { return 405; }
rewrite
if ($args ~* format=json) { rewrite ^/$uri/?(.*)$ /$uri.json$1 break; }
if ($args ~* format=xml) { rewrite ^/$uri/?(.*)$ /$uri.xml$1 break; }
rewrite last → Stop und nochmal break → Weiter nach
rewrites redirect → 302 permanent → 301
rewrite - Domains server { server_name www.example.com; rewrite ^/(.*)$ example.com/$1
premanent; } server { server_name www.example.com; rewrite ^ example.com/$request_uri premanent; }
rewrite - Domains server { server_name www.example.com; rewrite ^/(.*)$ example.com/$1
premanent; } server { server_name www.example.com; rewrite ^ example.com/$request_uri premanent; }
rewrite - Domains server { server_name www.example.com return 301 $scheme://example.com$request_uri
}
Load Balancing
Round Robin
upstream web_workers { server www1.example.com; server www2.example.com; server www3.example.com; }
IP basiert
upstream web_workers { ip_hash; server www1.example.com; server www2.example.com; server www3.example.com;
}
Gewichtet
upstream web_workers { server www1.example.com weight=2 max_fails=2 fail_timeout=15; server www2.example.com
weight=4 max_fails=3; server www3.example.com weight=4 max_fails=4 fail_timeout=20; }
upstream web_workers { server www1.example.com weight=2 max_fails=2 fail_timeout=15; server www2.example.com
weight=4 max_fails=3; server www3.example.com weight=4 max_fails=4 fail_timeout=20; } ip_hash + weight in Nginx >= 1.3.1
Nginx & PHP
upstream php { server unix:/var/run/php/www1.sock; }
location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include
fastcgi_params; fastcgi_index index.php fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name fastcgi_param SERVER_NAME $host fastcgi_pass php; } Don't trust the tutorials!
Benchmark ➔ Apache Benchmark (ab)
ab -n NREQ -c NCONC [-k] http://www.example.com/FILENAME
Benchmark ➔ Server & “Client” auf einem Server ➔ Request
logging deaktiviert ➔ Mit und ohne keepalive ➔ Durchschnitt aus 5 Testläufen
Benchmark ➔ HelloWorld.php – PHP, 13 bytes ➔ HelloWorld.txt –
statisch, 13 bytes ➔ 100KB.txt – statisch, 100 kb ➔ 1MB.txt – statisch, 1MB ➔ Index.php – “typischer” Frontcontroller
HelloWorld.php
HelloWorld.txt
100KB.txt
1MB.txt
Index.php
Apache PHP vs TXT
Nginx PHP vs TXT
Sollte ich zu Nginx wechseln? Überwiegend statischer Content → auf
jeden Fall Embedded / VPS → Ja Einfach so → vielleicht PHP Performanceoptimierung → Nein!
None
Danke! Fragen?
Quellen ➔ Wikipedia ➔ http://wiki.nginx.org ➔ https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi- and-nginx-dont-trust-the-tutorials-check-your-configuration/ ➔ https://speakerdeck.com/u/helgi/p/cranking-nginx-to-11-
dpc-2012 ➔ https://github.com/perusio/drupal-with-nginx ➔ http://blog.a2o.si/2009/06/24/apache-mod_php-compared- to-nginx-php-fpm/