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
allow_retry と Arel.sql / allow_retry and Arel.sql
euglena1215
1
150
DuckDB-Wasmを使って ブラウザ上でRDBMSを動かす
hacusk
1
140
AWS環境のリソース調査を Claude Code で効率化 / aws investigate with cc devio2025
masahirokawahara
2
1.1k
クラウドセキュリティを支える技術と運用の最前線 / Cutting-edge Technologies and Operations Supporting Cloud Security
yuj1osm
2
260
バッチ処理で悩むバックエンドエンジニアに捧げるAWS Glue入門
diggymo
3
110
役割は変わっても、変わらないもの 〜スクラムマスターからEMへの転身で学んだ信頼構築の本質〜 / How to build trust
shinop
0
160
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
30k
攻撃と防御で実践するプロダクトセキュリティ演習~導入パート~
recruitengineers
PRO
4
1.8k
Function Body Macros で、SwiftUI の View に Accessibility Identifier を自動付与する/Function Body Macros: Autogenerate accessibility identifiers for SwiftUI Views
miichan
2
160
RSCの時代にReactとフレームワークの境界を探る
uhyo
9
2.5k
Snowflakeの生成AI機能を活用したデータ分析アプリの作成 〜Cortex AnalystとCortex Searchの活用とStreamlitアプリでの利用〜
nayuts
0
160
AI時代にPdMとPMMはどう連携すべきか / PdM–PMM-collaboration-in-AI-era
rakus_dev
0
260
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
840
Intergalactic Javascript Robots from Outer Space
tanoku
272
27k
Balancing Empowerment & Direction
lara
3
610
How to Think Like a Performance Engineer
csswizardry
26
1.9k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.6k
Scaling GitHub
holman
463
140k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Agile that works and the tools we love
rasmusluckow
330
21k
For a Future-Friendly Web
brad_frost
179
9.9k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.5k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.9k
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/