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
NGINX for Fun & Profit
Search
Martin Loy
May 31, 2014
Technology
1
190
NGINX for Fun & Profit
slides from my talk #phpDay 2014 on Buenos Aires
Martin Loy
May 31, 2014
Tweet
Share
More Decks by Martin Loy
See All by Martin Loy
Intro to Nginx
tombar
2
200
Other Decks in Technology
See All in Technology
米国国防総省のDevSecOpsライフサイクルをAWSのセキュリティサービスとOSSで実現
syoshie
2
1.2k
5min GuardDuty Extended Threat Detection EKS
takakuni
0
160
生成AI活用の組織格差を解消する 〜ビジネス職のCursor導入が開発効率に与えた好循環〜 / Closing the Organizational Gap in AI Adoption
upamune
5
3.9k
急成長を支える基盤作り〜地道な改善からコツコツと〜 #cre_meetup
stefafafan
0
130
CI/CD/IaC 久々に0から環境を作ったらこうなりました
kaz29
1
190
登壇ネタの見つけ方 / How to find talk topics
pinkumohikan
5
530
MySQL5.6から8.4へ 戦いの記録
kyoshidaxx
1
260
"サービスチーム" での技術選定 / Making Technology Decisions for the Service Team
kaminashi
1
170
AWS テクニカルサポートとエンドカスタマーの中間地点から見えるより良いサポートの活用方法
kazzpapa3
2
560
Oracle Audit Vault and Database Firewall 20 概要
oracle4engineer
PRO
3
1.7k
Amazon ECS & AWS Fargate 運用アーキテクチャ2025 / Amazon ECS and AWS Fargate Ops Architecture 2025
iselegant
17
5.7k
生成AIで小説を書くためにプロンプトの制約や原則について学ぶ / prompt-engineering-for-ai-fiction
nwiizo
4
2.4k
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
RailsConf 2023
tenderlove
30
1.1k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
5.9k
Embracing the Ebb and Flow
colly
86
4.7k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
124
52k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
35
2.4k
How STYLIGHT went responsive
nonsquared
100
5.6k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Transcript
for Fun & Profit #phpDay, @MartinLoy
$ whoami Martin Loy BOFH @ Batanga Media GuruHub CoFounder
CoOrganizer tech.meetup.uy meetups (devops|php|mysql) martinloy tombar martinloy
$ ls -l /nx/topics/* - nginx intro - config &&
syntax; - apache [ cache | load balancing ] - nginx + php - nginx modules - scenarios - tips
Rise up!
$ apropos nginx - Small, powerful, scalable web|proxy server. -
Russian engineering. - BSD license. - Market Share 14.3% | 38% top 1000 websites - It's pronounced “engine X” - Also an e-mail (imap|pop3) proxy.
$ diff apache nginx Nginx php-fpm 5.3.3+ Configuration syntax &
Rewrite Rules Recompile to add* Apache mod_php, php-fpm .htaccess Modular (a2enmod)
$ man nginx.conf 3 main contexts http | server |
location variables and methods are lowercases and only support a-z and underscore set $var foo; set_header X-Example bar; http://wiki.nginx.org/Http{X}Module#{method}
//config && syntax
@ http://wiki.nginx.org/HttpCoreModule#location server { server_name *.test.com www.test.com; listen 127.0.0.1:8000; access_log
/var/log/nginx/access.log; root /var/www/test; location / { <PHP> } }
Locations
@ http://wiki.nginx.org/HttpCoreModule#location location = /admin { ... } /admin location
/ { ... } /index.html /example/test.html /match.any location /docs { ... } /docs/example.html location ^~ /images { ... } /images/avatar.gif location ~* (jpg|gif|png) { ... } /docs/1.gif
location ~* (jpg|png|gif) { rewrite ^ http://disney.com? permanent; } location
/ { if ($geoip_city_country_code = "AR") { rewrite ^/$ http://example.org/ar redirect; } try_files $uri $uri/ /index.php?$args; } $ locations > mod_rewrite
$ Proxy Module location /paso-todo-para-apache { include /etc/nginx/proxy_params proxy_set_header Host
$host; proxy_pass http://myBackend/app; proxy_redirect /app /; proxy_read_timeout 30s; } @ http://wiki.nginx.org/HttpProxyModule
location /cache-2hs-los-pedidos { proxy_pass http://myBackend; proxy_cache_valid 200 301 302 2h;
proxy_cache_valid any 1m; proxy_cache_bypass $nocache_cookie; proxy_cache_key "$host$uri$args"; proxy_ignore_headers Cache-Control; } proxy_cache_use_stale error | timeout | updating | http_500 | http_502 | http_503 | http_404 Caching with nginx
$ Upstream Module upstream myBackendFarm { server 10.0.1.1 weight=2 down;
server 10.0.1.2 weight=10 keepalive=5; server unix:/path/to/socket server 10.0.1.4 max_fails=3 fail_timeout=5s; server 10.0.1.5 backup; [ fair | hash | sticky | + ] } @ http://wiki.nginx.org/HttpUpstreamModule
NGINX + PHP
$ apropos php-fpm Alternative PHP FastCGI implementation $ yum|apt-get install
php-fpm - use listen tcp instead of socket - slow.log - App pool
$ nginx + php location ~ \.php$ { include /etc/nginx/fastcgi_params
fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_pass phpBackend; } @ http://wiki.nginx.org/PHPFcgiExample
so far, so good?
@ http://wiki.nginx.org/3rdPartyModules
# Cache Backends redis, memcache, mongo, purge # Security mod_security,
naxsi, http pam # Media image filter, mp4, thumb extractor # Misc php, form parser, rdd, download $ ls -l /nx/3rdPartyMods/* # Optimizers pagespeed, css, js min # Monitoring statsd, sflow, syslog # Real FUN ! lua, perl, python, javascript, ruby # more awesomeness openresty, upload, push/comet
NGINX use cases
# per user cache proxy_cache_key "$user_cookie$host$uri"; # per country cache
proxy_cache_key "$geoip_city_country_code$host$uri"; proxy_cache_use_stale error | timeout | updating | http_500 | http_502 | http_503 | http_404 # remote caching proxy_pass https://api.remote.provider.com; $ ls -l /nx/scenarios/cache*
log_format multidomain-multiproxy-geoip $host - { combined apache log format }
- Cache: $upstream_cache_status $upstream_status $upstream_response_time $upstream_addr - Geo: $geoip_city_country_code $geoip_region $ ls -l /nx/scenarios/logging
@ http://wiki.nginx.org/HttpLogModule#log_format log_format json_log '{' '"host": "$host", ' '"remote_addr": "$remote_addr",
' '"remote_user": "$remote_user", ' '"postdata": {' '"request_body": "$request_body", ' '"request_completion":"$request_completion"' ' },' '"cookie": {' '"userid": "$cookie_userid", ' ' } ' '}';
@ nginx fastcgi_param request_uuid $request_id; @ php-fpm /etc/php5/fpm/pool/default access.format =
… %{request_uuid}o ... @ php echo $ _SERVER["request_uuid"]; @ mysql -- uuid: {$request_uuid} $ ls -l /nx/scenarios/tracking
A/B testing
Abusing A/B testing
HTTP black boxing
@ ELK stack | http://www.elasticsearch.org/overview/
Moar questions?
We are recruiting >
[email protected]
tech.meetup.uy [ 15 november 2014, mvd UY ]
contact me @martinloy | github.com/tombar
@martinloy github.com/tombar