Networking
NGinx: Custom error handling
by devarni on 18 Mai 2010, under Howto
I will give you a idea about the easy implementation of error page handling with Nginx.
1. Setting error handler
Error handler can be set with the error_page directive. More about this directive you can find here.
Do this in your server block but better use a generic file and include this in every virtual host server block. I use for this a file “vhost” with all such settings.
The handler or html files are placed in a separate folder outside the webs. You can use a folder inside your Nginx settings (I use here for example a “html” folder).
The location block is needed to pass the correct root and file to php fastcgi. In this example I use a file “fastcgi” with this mostly used lines:
fastcgi_pass unix:/tmp/fastcgi.socket; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
Here the part in the server block (or in your generic vhost file for including):
# error handler, codes can also be combined
error_page 401 /error.php?c=401;
error_page 403 /error.php?c=403;
error_page 404 /error.php?c=404;
error_page 500 /error.php?c=500;
error_page 502 503 504 /error.php?c=50x;
# serve error.php from /etc/nginx/html folder
location =/error.php {
root /etc/nginx/html;
include conf/fastcgi;
}
2. PHP file
The error.php file will be called with the URL encoded error-code, so its easy to grab this code in the PHP file from the superglobal $_GET array.
To map this error code to a message string, use a associative array and a function to return the string.
This is the part in the error.php file:
"Autorisierung erforderlich! - Authorization Required",
"403" => "Zugriff ist nicht erlaubt! - Access forbidden",
"404" => "Seite wurde nicht gefunden - Page not found",
"500" => "Interner Serverfehler - Internal Server Error",
"50x" => "Seite ist momentan nicht verfügbar. Bitte Anfrage später wiederholen.
-
The page is temporarily unavailable. Try again later.");
// get the error string
function errorString($error) {
global $_err;
foreach ($_err as $key => $val) {
if ($key == $error)
return $val;
}
return "Unknown Error";
}
isset($_GET['c']) or die("Unknown Error");
$error = $_GET['c'];
$str = errorString($error);
?>
Error <?=$error?>: <?=$str?>
You can format the error page as you like. But remember: the web root url will be the actual virtual host! Easiest way: use inline CSS. If this is not your preferred way, place the css file in a virtual host (eg. your default virtual host)
Nginx: phpmyadmin configuration
by devarni on 13 Mai 2010, under Howto
This configuration is used to access phpmyadmin as a “subfolder” of the domain like:
http://mysite.com/phpmyadmin
The location will be append to the root! For a similar behaviour like the Apache configuration, you could use the “alias” directive
My phpmyadmin installation is under /usr/share/phpmyadmin, thats why I use /usr/share as the root.
server{
...
location /phpmyadmin{
root /usr/share;
index index.php;
}
location ~ \.php$ {
set $php_root $document_root;
if ($request_uri ~* /phpmyadmin) {
set $php_root /usr/share;
}
fastcgi_pass unix:/tmp/fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Server Benchmark: Apache, Nginx, Cherokee, Lighttp
by devarni on 13 Mai 2010, under Networking
Quick Benchmark of Apache, Nginx, Cherokee, Lighttpd
A short benchmark of this 4 server with dynamic and static content.
All server provides the same functionality like URL rewriting, password protected folders/files etc. but all have their own way to do that
For shared hosting where the user has no access to the server configuration, Apache with the .htaccess file support is a good choice.
I used the benchmark tool from Apache ab -n 50000 -c 20
php-cgi goes over a Unix socket
Dynamic Content (PHP)
| Server | Requests (sec) | Transferrate (KB/s) |
| Apache 2.2.14 | 2125 | 11684 |
| Nginx 0.7.65 | 1734 1861 |
9436 (php-cgi) 10115 (php-fpm) |
| Cherokee 1.0.0 | 2119 2103 |
11562 (php-cgi) 11454 (php-fpm) |
Static content (14785 Bytes HTML file)
| Server | Requests (sec) | Transferrate (KB/s) |
| Apache 2.2.14 | 6768 | 99593 |
| Lighttpd 2.4.26 | 15782 | 213866 |
| Cherokee 1.0.0 | 7602 | 111285 |
| Nginx 0.7.65 | 10912 | 159828 |
More “real live” test with PHP and eAccelerator enabled
Cherokee/NGinx/Lighttp are using the same php-fpm socket
| Server | Requests/s | Ø |
| Apache 2.2.14 | 2713|2744|2766 | 2741 |
| Nginx 0.7.65 | 2416|2433|2418 | 2422 |
| Cherokee 1.0.0 | 2702|2638|2694 | 2678 |
| Lighttp 1.4.26 | 2492|2363|2540 | 2465 |
Results
There is a small difference between fast-cgi and php-fpm (alternative implementation of fast-cgi).
You should prefer php-fpm (will be included in PHP 5.4 or you must build it manually from source).
Apache and Cherokee are both excellent for dynamic content.
For static content Lighttp is the fastest server.
There is not _the_ best server but a good combination: Lighttpd or Nginx as a proxy for serving static content, Apache or Cherokee for the dynamic content. Or a another proxy server like Varnish for static content and caching and Apache/Cherokee for serving dynamic content
For a PHP site with only 1 server running: Apache.
For a smaller memory footprint and if .htaccess is not needed: Cherokee (also the only server with a very good graphical admin interface)
English
Deutsch