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
AWSに詳しくない人でも始められるコスト最適化ガイド
yuhta28
2
420
Max out Local LLM in Challenging Environments
sashimimochi
2
210
今年のRubyKaigiはProfiler Year🤘
osyoyu
0
510
リテール金融(キャッシュレス・ネット銀行・ネット証券)の競争環境と経済圏
8maki
0
1.8k
IaCジェネレーターとBedrockで詳細設計書を生成してみた
tsukasa_ishimaru
4
940
コードファーストの考え方。 Amplify Gen2から学ぶAWS次世代のWeb開発体験
yoshiitaka
2
530
M5と自作基板をくっつけてみた〜M5 Japan Tour 2024 Spring 福冈 (Fukuoka|福岡)〜
keropiyo
1
240
GrafanaMeetup_AmazonManagedGrafanaのアクセス制御機能とマルチテナント環境下でのアクセス制御について
daitak
0
460
Azureの基本的な権限管理の勉強会
yhana
1
2.2k
本当のガバクラ基礎
toru_kubota
0
170
LLM開発・活用の舞台裏@2024.04.25
yushin_n
3
1.3k
自らを知り外と繋がる、日経のエンジニア採用とDevRel活動/devreljp92
nishiuma
2
180
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
261
12k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
7k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
80
44k
Raft: Consensus for Rubyists
vanstee
133
6.3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
21
1.6k
Infographics Made Easy
chrislema
238
18k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
Build The Right Thing And Hit Your Dates
maggiecrowley
25
2k
Clear Off the Table
cherdarchuk
85
310k
Scaling GitHub
holman
457
140k
Teambox: Starting and Learning
jrom
128
8.4k
Stop Working from a Prison Cell
hatefulcrawdad
267
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/