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
250
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
110
Reguläre Ausdrucke (PCRE)
dsiebel
0
64
Other Decks in Technology
See All in Technology
SEQUENCE object comparison - db tech showcase 2025 LT2
nori_shinoda
0
150
整頓のジレンマとの戦い〜Tidy First?で振り返る事業とキャリアの歩み〜/Fighting the tidiness dilemma〜Business and Career Milestones Reflected on in Tidy First?〜
bitkey
3
17k
〜『世界中の家族のこころのインフラ』を目指して”次の10年”へ〜 SREが導いたグローバルサービスの信頼性向上戦略とその舞台裏 / Towards the Next Decade: Enhancing Global Service Reliability
kohbis
2
300
Lakebaseを使ったAIエージェントを実装してみる
kameitomohiro
0
140
SmartNewsにおける 1000+ノード規模 K8s基盤 でのコスト最適化 – Spot・Gravitonの大規模導入への挑戦
vsanna2
0
140
Yahoo!しごとカタログ 新しい境地を創るエンジニア募集!
lycorptech_jp
PRO
0
130
タイミーのデータモデリング事例と今後のチャレンジ
ttccddtoki
6
2.4k
オーティファイ会社紹介資料 / Autify Company Deck
autifyhq
10
130k
ビズリーチが挑む メトリクスを活用した技術的負債の解消 / dev-productivity-con2025
visional_engineering_and_design
3
7.9k
Delta airlines Customer®️ USA Contact Numbers: Complete 2025 Support Guide
deltahelp
0
810
敢えて生成AIを使わないマネジメント業務
kzkmaeda
2
460
MobileActOsaka_250704.pdf
akaitadaaki
0
150
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Balancing Empowerment & Direction
lara
1
430
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Six Lessons from altMBA
skipperchong
28
3.9k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
Git: the NoSQL Database
bkeepers
PRO
430
65k
A better future with KSS
kneath
238
17k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Side Projects
sachag
455
42k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
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/