nginx is a web server used by many web sites, you can follow the link for further information. In this post, I'll briefly talk about how to serve two sites from the same port.


Prices, of course. I had two small websites to serve, which can fit into a small setup easily, why open to separate servers for that?. After setting up two separate websites, I saw the exception Address already in use because two applications cannot bind to the same port. For the sake of a clean blog URL, I didn't want to use arbitrary ports like


I assume you have basic knowledge of nginx, have a server already set up, have two different web servers up and running. If you don't have a server and want to serve just static files, that's even easier, just copy the static files to a location on the server.

nginx configuration

The nginx configuration files rests under /etc/nginx directory. The main configuration file is /etc/nginx/nginx.conf (at least there is in Ubuntu). Please check the nginx.conf file has following line as the last line in http block:

http {
    #other config
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;

    #include other sites
    include /etc/nginx/sites-enabled/*;

Because we'll add new configuration files under /etc/nginx/sites-available/ directory and create a symbolic link to them in /etc/nginx/sites-enabled/(best practice, you can enable/disable websites by deleting the link, not the configuration file itself).

First server

Here we go: the first server is actually a web server, so this configuration creates a reverse proxy which proxies requests incoming into 80 to localhost:2222. Of course, it assumes the running server listens to port 2222. The content of /etc/nginx/sites-available/ghost is:

server {
    listen 80;
    server_name; # Replace with your domain

    root /usr/share/nginx/html;
    index index.html index.htm;

    location / {
        proxy_pass http://localhost:2222;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;

Ghost is the platform by blog is based on, hence the name of the file is ghost. Then, I ran following command to create the symbolic link:

sudo ln -s /etc/nginx/sites-available/ghost /etc/nginx/sites-enabled/ghost

The site should be ready - remember, you should reload nginx for changes to show effect. As a result, requests to should be proxied to locahost:2222.

Second server

The second server is a WordPress blog. It uses fastcgi_pass to pass requests to php server. Here is the content of /etc/nginx/sites-available/otherblog:

server {
	listen 80;
	server_name; #Change it!

	root /var/www/otherblog;
	index index.php index.html index.htm;

	location / {
		try_files $uri $uri/ /index.php?q=$uri&$args;

	error_page 404 /404.html;
	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
		root /usr/share/nginx/html;

	location ~ \.php$ {
		try_files $uri =404;
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_index index.php;
		include fastcgi_params;

Similar to previous configuration file, I have created a symbolic link:

sudo ln -s /etc/nginx/sites-available/otherblog /etc/nginx/sites-enabled/otherblog

Voilà! We have two websites served on port 80. Do not forget to reload nginx before refreshing your page.