Nginx i Varnish – konfiguracja pod magento

5 min czytania

Nginx i Varnish - konfiguracja pod magentoNginx, Varnish – obecnie to najczęściej spotykana para programów znacznie przyspieszającą prace magento.
W tym artykule opiszę konfigurację jaką najczęściej stosujemy w firmie smartmage.pl.
Oczywiście programy muszą być zainstalowane w systmie. Do zainstalowania Nginxa można użyć poleceń:

1apt-get install nginx

Do zainstalowania Varnisha musimy dodać dodatkowe repozytoria:

1curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add -
2echo "deb http://repo.varnish-cache.org/debian/ wheezy varnish-3.0" >> /etc/apt/sources.list
3apt-get update
4apt-get install varnish

Konfiguracja programów: Varnish na porcie 80, Nginx na porcie 8080.

Konfiguracja nginxa na porcie 8080:
Plik nginx.conf:

1user www-data;
2worker_processes 4; # Liczbę ustawiamy taką samą jak liczba procesorów
3 
4pid /var/run/nginx.pid;
5 
6events {
7 worker_connections 1024;
8}
9 
10http {
11 ##
12 # Zabezpieczenie przed atakami DDOS
13 ##
14 
15 limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=5r/s;
16 limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
17 
18 ##
19 # Basic Settings
20 ##
21 
22 sendfile on;
23 tcp_nopush on;
24 tcp_nodelay on;
25 keepalive_timeout 65;
26 types_hash_max_size 2048;
27 server_tokens off;
28 client_max_body_size 20m;
29 client_body_buffer_size 128k;
30 
31 include /etc/nginx/mime.types;
32 default_type application/octet-stream;
33 
34 ##
35 # Logging Settings / Zmiana logowania ip. W standardowej konfiguracje bedzie tylko logowania ip z Varnisha
36 ##
37 
38 log_format varnish_log '$http_x_forwarded_for - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent "$http_referer" ' '"$http_user_agent"' ;
39 access_log /var/log/nginx/access.log;
40 error_log /var/log/nginx/error.log;
41 
42 ##
43 # Gzip Settings
44 ##
45 
46 gzip on;
47 gzip_min_length 1100;
48 gzip_buffers 4 32k;
49 gzip_disable "msie6";
50 
51 gzip_vary on;
52 gzip_proxied any;
53 gzip_comp_level 9;
54 gzip_http_version 1.1;
55 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript applicacion/x-font-ttf;
56 
57 ##
58 # nginx-naxsi config
59 ##
60 # Uncomment it if you installed nginx-naxsi
61 ##
62 
63 #include /etc/nginx/naxsi_core.rules;
64 
65 ##
66 # Virtual Host Configs
67 ##
68 
69 include /etc/nginx/conf.d/*.conf;
70 include /etc/nginx/sites-enabled/*;
71}

Przykladowy plik vhosta:

1server {
2 listen 10.0.0.10:8080; # Przykładowy ip
3 
4 server_name www.domena.pl; ## Domain is here twice so server_name_in_redirect will favour the www
5 root /var/www/domena/web;
6 
7 location / {
8 index index.php index.html; ## Allow a static html file to be shown first
9 try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
10 expires 30d; ## Assume all files are cachable
11 }
12 
13 ## These locations downloadable would be hidden by .htaccess normally
14 location ^~ /app/ { deny all; }
15 location ^~ /includes/ { deny all; }
16 location ^~ /lib/ { deny all; }
17 location ^~ /media/downloadable/ { deny all; }
18 location ^~ /pkginfo/ { deny all; }
19 location ^~ /report/config.xml { deny all; }
20 location ^~ /var/ { deny all; }
21 
22 location /var/export/ { ## Allow admins only to view export folder
23 auth_basic "Restricted"; ## Message shown in login window
24 auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
25 autoindex on;
26 }
27 
28 location /. { ## Disable .htaccess and other hidden files
29 return 404;
30 }
31 
32 location @handler { ## Magento uses a common front handler
33 rewrite / /index.php;
34 }
35 
36 location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
37 rewrite ^(.*.php)/ $1 last;
38 }
39 
40 location ~ .php$ { ## Execute PHP scripts
41 if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
42 
43 expires off; ## Do not cache dynamic content
44 fastcgi_read_timeout 1200s;
45 fastcgi_send_timeout 1200s;
46 fastcgi_connect_timeout 1200s;
47 fastcgi_pass unix:/var/run/php5-fpm.sock;
48 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
49 fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
50 fastcgi_param MAGE_RUN_TYPE store;
51 fastcgi_param PHP_ADMIN_VALUE sendmail_path = /usr/sbin/sendmail -t -i -fadres@email.pl;
52 include fastcgi_params; ## See /etc/nginx/fastcgi_params
53 
54 # Ograniczenie polaczen DDOS
55 limit_req zone=req_limit_per_ip burst=10 nodelay;
56 limit_conn conn_limit_per_ip 10;
57 }
58 
59 access_log /var/log/nginx/access-domena.pl.log varnish_log;
60 error_log /var/log/nginx/error-domena.pl.log;
61}

Konfiguracja Varnisha na porcie 80:

Konfiguracja pliku varnish w katalogu /etc/default:

1START=yes
2 
3# Maximum number of open files (for ulimit -n)
4NFILES=131072
5 
6# Maximum locked memory size (for ulimit -l)
7# Used for locking the shared memory log in memory. If you increase log size,
8# you need to increase this number as well
9MEMLOCK=82000
10 
11DAEMON_OPTS="-a 10.0.0.10:80 \
12 -T localhost:6082 \
13 -f /etc/varnish/default.vcl \
14 -S /etc/varnish/secret \
15 -s malloc,1024m;

Konfiguracja pliku default.vcl w katalogu /etc/varnish:

1# default backend definition. Set this to point to your content server.
2backend default {
3 .host = 10.0.0.10;
4 .port = 8080;
5}
6 
7# admin backend with longer timeout values. Set this to the same IP & port as your default server.
8backend admin {
9 .host = 10.0.0.10;
10 .port = 8080;
11 .first_byte_timeout = 18000s;
12 .between_bytes_timeout = 18000s;
13}
14 
15# add your Magento server IP to allow purges from the backend
16acl purge {
17 10.0.0.10;
18 localhost;
19 127.0.0.1;
20}
21 
22import std;
23 
24sub vcl_recv {
25if (req.restarts == 0) {
26 if (req.http.x-forwarded-for) {
27 set req.http.X-Forwarded-For =
28 req.http.X-Forwarded-For + ", " + client.ip;
29 } else {
30 set req.http.X-Forwarded-For = client.ip;
31 }
32 }
33if (req.request != "GET" &&
34req.request != "HEAD" &&
35 req.request != "PUT" &&
36 req.request != "POST" &&
37 req.request != "TRACE" &&
38 req.request != "OPTIONS" &&
39 req.request != "DELETE" &&
40 req.request != "PURGE") {
41 /* Non-RFC2616 or CONNECT which is weird. */
42 return (pipe);
43 }
44 
45# purge request
46 if (req.request == "PURGE") {
47 if (!client.ip ~ purge) {
48 error 405 "Not allowed.";
49 }
50 ban("obj.http.X-Purge-Host ~ " + req.http.X-Purge-Host + " && obj.http.X-Purge-URL ~ " + req.http.X-Purge-Regex + " && obj.http.Content-Type ~ " + req.http.X-Purge-Content-Type);
51 error 200 "Purged.";
52 }
53 
54# switch to admin backend configuration
55 if (req.http.cookie ~ "adminhtml=") {
56 set req.backend = admin;
57 }
58 
59# we only deal with GET and HEAD by default
60 if (req.request != "GET" && req.request != "HEAD") {
61 return (pass);
62 }
63 
64# normalize url in case of leading HTTP scheme and domain
65 set req.url = regsub(req.url, "^http[s]?://[^/]+", "");
66 
67# collect all cookies
68 std.collect(req.http.Cookie);
69 
70# static files are always cacheable. remove SSL flag and cookie
71 if (req.url ~ "^/(media|js|skin)/.*\.(png|jpg|jpeg|gif|css|js|swf|ico)$") {
72 unset req.http.Https;
73 unset req.http.Cookie;
74 }
75 
76# not cacheable by default
77 if (req.http.Authorization || req.http.Https) {
78 return (pass);
79 }
80 
81# do not cache any page from index files
82 if (req.url ~ "^/(index)") {
83 return (pass);
84 }
85 
86# as soon as we have a NO_CACHE cookie pass request
87 if (req.http.cookie ~ "NO_CACHE=") {
88 return (pass);
89 }
90 
91# remove Google gclid parameters
92 set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA"
93 set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar"
94 set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"
95 
96return (lookup);
97}
98 
99sub vcl_hash {
100 hash_data(req.url);
101 if (req.http.host) {
102 hash_data(req.http.host);
103 } else {
104 hash_data(server.ip);
105 }
106 
107if (req.http.cookie ~ "PAGECACHE_ENV=") {
108 set req.http.pageCacheEnv = regsub(
109 req.http.cookie,
110 "(.*)PAGECACHE_ENV=([^;]*)(.*)",
111 "\2"
112 );
113 hash_data(req.http.pageCacheEnv);
114 remove req.http.pageCacheEnv;
115 }
116 
117if (!(req.url ~ "^/(media|js|skin)/.*\.(png|jpg|jpeg|gif|css|js|swf|ico)$")) {
118 call design_exception;
119 }
120 return (hash);
121}
122 
123sub vcl_fetch {
124 if (beresp.status == 500) {
125 set beresp.saintmode = 10s;
126 return (restart);
127 }
128 set beresp.grace = 5m;
129 
130# enable ESI feature if needed
131 if (beresp.http.X-Cache-DoEsi == "1") {
132 set beresp.do_esi = true;
133 }
134 
135# add ban-lurker tags to object
136 set beresp.http.X-Purge-URL = req.url;
137 set beresp.http.X-Purge-Host = req.http.host;
138 
139if (beresp.status == 200 || beresp.status == 301 || beresp.status == 404) {
140 if (beresp.http.Content-Type ~ "text/html" || beresp.http.Content-Type ~ "text/xml") {
141 if ((beresp.http.Set-Cookie ~ "NO_CACHE=") || (beresp.ttl < 1s)) { set beresp.ttl = 0s; return (hit_for_pass); } # marker for vcl_deliver to reset Age: set beresp.http.magicmarker = "1"; # Don't cache cookies unset beresp.http.set-cookie; } else { # set default TTL value for static content set beresp.ttl = 4h; } return (deliver); } return (hit_for_pass); } sub vcl_deliver { # debug info if (resp.http.X-Cache-Debug) { if (obj.hits > 0) {
142 set resp.http.X-Cache = "HIT";
143 set resp.http.X-Cache-Hits = obj.hits;
144 } else {
145 set resp.http.X-Cache = "MISS";
146 }
147 set resp.http.X-Cache-Expires = resp.http.Expires;
148 } else {
149 #remove Varnish/proxy header
150 remove resp.http.X-Powered-By;
151 remove resp.http.X-Varnish;
152 remove resp.http.Via;
153 remove resp.http.Age;
154 remove resp.http.X-Purge-URL;
155 remove resp.http.X-Purge-Host;
156 }
157 
158if (resp.http.magicmarker) {
159 # Remove the magic marker
160 unset resp.http.magicmarker;
161 
162set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
163 set resp.http.Pragma = "no-cache";
164 set resp.http.Expires = "Mon, 31 Mar 2008 10:00:00 GMT";
165 set resp.http.Age = "0";
166 }
167}

Na koniec restartujemy Nginx-a oraz Varnish-a. System jest gotowy do pracy.

Menu