HOWTO: Serve virtual host with Nginx
Web September 6th, 2008
As the limited memory budget, and I plan to host multiple website in the VPS, I decided to take a less versatile, but lightweight Apache alternative, the Nginx made by the polar bear.
There is no RPM in the repository I have enlisted, so let’s fallback the old-school way:
yum remove glibc-dummy-centos-4
yum -y install gcc openssl-devel
# Now it is time to build the nginx:
./configure –prefix=/opt/nginx –with-http_ssl_module –with-http_stub_status_module
make && sudo make install
Then get the PHP with FastCGI support, and the lighttpd-fastcgi for the fastcgi loader.
Here is the nginx.conf that server.
worker_processes 2;
pid logs/nginx.pid;
error_log logs/error.log;
events {
worker_connections 2048;
use epoll;
}
http {
include mime.types;
include fastcgi_params;
default_type application/octet-stream;
log_format main ‘$remote_addr - $remote_user [$time_local] $request ‘
‘”$status” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
gzip_static on;
output_buffers 1 32k;
postpone_output 1460;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 75 20;
server {
#REVIEW: how to redirect https? using re?
server_name www.kunxi.org;
rewrite ^(.*) http://kunxi.org$1 permanent;
}
server {
# kunxi’s gallery
listen 80;
server_name gallery.kunxi.org;
root /home/webadmin/$host;
access_log logs/$host.log main;
location / {
root /home/webadmin/$host;
index index.html index.htm index.php;
include zenphoto.rewrite;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
# deny access to .htaccess files.
location ~ /\.ht {
deny all;
}
}
server {
# kunxi’s sites
listen 80;
server_name kunxi.org *.kunxi.org;
root /home/webadmin/$host;
error_page 404 $document_root/404.html;
error_page 500 502 503 504 $document_root/50x.html;
access_log logs/$host.log main;
location / {
root /home/webadmin/$host;
index index.html index.htm index.php;
# the blog dir, aka where index.php is
set $blog_dir ”;
# the wordpress dir where all wp-* stays
set $wordpress_dir ‘/wordpress’;
include wordpress.rewrite;
}
# rewrite the /files/
#
location /files/ {
alias /home/webadmin/static.kunxi.org/;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
# deny access to .htaccess files.
#
location ~ /\.ht {
deny all;
}
}
}
Some highlights of the configuration:
Rewrite www.kunxi.org to kunxi.org, yes, we support no-www!
#REVIEW: how to redirect https? using re?
server_name www.kunxi.org;
rewrite ^(.*) http://kunxi.org$1 permanent;
}
And this wildcards will cover all sub-domains powered by PHP:
root /home/webadmin/$host;
… ….
Home-brewed nginx and fastcgi init scripts to make it works after the reboot:
chkconfig –add fcgi-php
service start nginx
service start fcgi-php
Tips and Traps:
Nginx supports 0 downtime upgrade, so if your nginx.conf is wrong, the server would ignore it and suck up the complain. Make sure stop the nginx service and start nginx during debugging rewrite rules.
fcgi-php seems to have problem to parse localhost, so I use 127.0.0.1 instead.
The rewrite rule for WordPress and ZenPhoto are explained here and here. (TODO).