Home

Add multiple sites to Nginx

Website

First one is PHP site config to the WordPress site.

cd /etc/nginx/sites-available
sudo nano dev.myphpsite.com

Add next lines in the file.

server {
        listen 80;
        listen [::]:80;

        root /var/www/myphpsite;
        index index.php;

        server_name dev.myphpsite.com;

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # sock file can be found in /etc/php/7.0/fpm/pool.d/www.conf
                include /etc/nginx/fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $request_filename;
        }

        location ~ /\.ht {
                deny all;
        }
}

Link config file to site enabled folder

sudo ln -s /etc/nginx/sites-available/dev.myphpsite.com /etc/nginx/sites-enabled
service nginx restart

Web app

Next is NodeJS app config.

cd /etc/nginx/sites-available
sudo nano dev.mynodeapp.com

Add next lines in the file. proxy_pass port haw to be you NodeJS app port.

server {
        listen 80;
        listen [::]:80;

        server_name dev.mynodeapp.com;

        location / {
                proxy_pass "http://localhost:3000";
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

}

Link config file to site enabled folder

sudo ln -s /etc/nginx/sites-available/dev.mynodeapp.com /etc/nginx/sites-enabled
service nginx restart

Disable site

An unlink command you can disable sites that we just created.

cd /etc/nginx/sites-enabled/
unlink dev.myphpsite.com
unlink dev.mynodeapp.com
service nginx restart

Leave a Reply

Your email address will not be published.