Nginx 1.4 Virtual hosting

Published: Thursday, Aug 22, 2013 Last modified: Monday, May 27, 2024

Aiming here to replicate [[Apache’s VirtualDocumentRoot|04025]]

This is mandatory otherwise nginx won’t start. Pretty bizaare:

events {
	worker_connections  1024;

This is the Archlinux default:

http {
	include       mime.types;
	default_type  application/octet-stream;
	sendfile      on;

server {
	charset utf-8;
	listen 80;
	autoindex on;
	include        fastcgi.conf;

Here we map the requested domain to /srv/www/$vhost

	server_name ~^(?<vhost>.*)$;
	root /srv/www/$vhost;
	index  index.html index.php index.cgi index.txt;
	access_log /var/log/nginx/$vhost.access.log;

To get PHP/CGI working, it’s you need this with php-fpm.service & fcgiwrap.service going beforehand.

	location ~ \.php$ {
		try_files      $uri =404;
		fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;

	location ~ \.cgi$ {
		fastcgi_pass   unix:/run/fcgiwrap.sock;

Optimisations to consider