In the first part of the “Self-made webserver” series I’ve described how you can compile Nginx on Ubuntu. Now let’s have a look how we can optimize our virtual host configuration by adding PHP-FPM Support and some other settings.
First of all the configuration file:
server { # Redirect from example.com to www.example.com listen *:80; server_name example.com; rewrite ^/(.*) http://www.example.com/$1 permanent;}
server{ server_name www.example.com; listen *:80; root /var/www/www.example.com/public;
# logging access_log /var/www/www.example.com/log/access.log; error_log /var/www/www.example.com/log/error.log;
# deny access to hidden files location ~ /\. { access_log off; log_not_found off; deny all; }
# Maximum expiration for static files location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { expires max; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; }
if (-f $request_filename) { break; }
# removes trailing slashes (prevents duplicate content issues) if (!-d $request_filename) { rewrite ^/(.*)/$ /$1 permanent; }
# Index files location / { index index.html index.php; autoindex off; }
# If file doesn't exists rewrite to index.php, # necessary for most php based frameworks/applications if (!-e $request_filename) { rewrite ^.*$ /index.php last; }
# Pass PHP scripts to PHP-FPM location ~* \.php$ { try_files $uri /index.php; fastcgi_index index.php; fastcgi_pass php5-fpm; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; }}Configuration in detail
As you can see I have two server sections for the same domain. The purpose is to redirect addresses without www prefix. There are groups of people that say it’s outdated, but I prefer to do so because of the cookie sending behavior of the browser. If you use only example.com without prefix, and you set a cookie there, the browser will send the cookie information to all request of *.example.com. So static.example.com/my.jpg would also receive cookie headers, which generates more overhead traffic.
Another point is that most application recognize www.example.com as link, whereas example.com isn’t. Ok, I must confess this is a small issue, but anyway it’s up to you how you configure it.
Deny and redirect
Hidden files are supposed to be hidden also from the user, therefore I’ve placed a rule to deny access to hidden files:
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
For static files I’ve added an expiration setting which is set to maximum. Also Cache-Controll headers are added automatically:
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
Files that don’t exist are going to be redirected to index.php. This setting is required by most applications to realize pretty URIs.
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
The last section worth mentioning is the PHP-FPM configuration. PHP files are passed to fast-cgi, in this case to PHP-FPM. If you followed the series, you will notice that we didn’t installed PHP-FPM yet. This is the next step I will describe in the next part.



















