Serve multiple websites in local development with nginx

0
3049
nginx HTTP Web Server
nginx HTTP Web Server

To enable serving multiple websites in local development with nginx, you will need to config to assign each website with a specific port.

Assuming, we have two websites to serve locally via nginx, admin and public. We will need to create two config files under /etc/nginx/sites-available,  named them sequentially as local-admin.conf, local-public.conf.

Their configuration would be like this:


// local-admin.conf
server {
    listen 9001;
    server_name 127.0.0.1;
    root /var/www/admin_html;

    location / {
        try_files $uri $uri/ =404;
    }
}


// local-public.conf
server {
    listen 9002;
    server_name 127.0.0.1;
    root /var/www/public_html;

    location / {
        try_files $uri $uri/ =404;
    }
}

As you can see above, I assign port 9001 to admin web and 9002 to public web.

After restarting nginx server, you can access them on browser via http://127.0.0.1:9001 and http://127.0.0.1:9002.

That’s all you have to do ~