Multiple Custom Domains on an NGINX Web Server

By August 21, 2013 HowTo No Comments
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

In this tutorial we’ll show you how to host a PHP website and run multiple custom domains on an NGINX web server in minutes. You can greatly benefit from the features of having multiple domain names. This helps with SEO and is a great benefit for your website security and anonymity. Cost spending is also decreased because you only pay for one service for two or more of your domains.

Do you have multiple custom domain names and want one of the domains to point to the main page of your PHP website, but the other domains to point to a subsection of your site without modifying the URL in the address bar? No problem! With Jelastic PaaS you can achieve all these actions right in the cloud in just a few minutes!

1. Sign up and log into your Jelastic account.

2. Click the Create environment button and select the PHP tab in order to create a new environment for your PHP application. After that choose NGINX web server and specify the resource usage limits for it (reserved and dynamic values). Configure your environment topology by picking other software stacks, which are needed for your PHP application proper functionality. Then enter your PHP environment name and click on the Create button.

Host PHP Website on NGINX Web Server

In  a few seconds your environment with NGINX web server will be successfully created.

Host PHP Website Environment

3. You have to set your domain names in your DNS, resolving to your environment IP address, for further binding to the created environment. You can do this in two ways: by adding a CNAME record or by setting A Records. Find more information here.

4. After you have your own domain names, go back to the Jelastic dashboard and click the Settings button for your environment. Navigate to the Custom domains tab and bind your domain names.

Custom Domains Binding

Note: We didn’t buy real domain names, we’ve just put entries into a hosts’ file for local testing and this works only for the machine with these host entries.

5. Upload your zip packages to the Deployment manager and deploy them to different contexts.

NGINX Web Server Contexts

6. Once your applications are deployed to the specified contexts click Config for NGINX and navigate to the nginx.conf file, which is located in the conf directory. Specify your settings in the server block by following the next example:

server {
        listen       80;
        server_name  domain.org;

	include /etc/nginx/aliases.conf;
        location / {
            root   /var/www/webroot/mycontext;
            index  index.html index.htm index.php;

    	    location ~ \.php$ {
    	    	location ~ /\. { deny all; access_log off; log_not_found off; }
	    	include /etc/nginx/fastcgi_params;
	        fastcgi_pass 127.0.0.1:9000;
        	fastcgi_param SCRIPT_FILENAME /var/www/webroot/mycontext$fastcgi_script_name;
	        fastcgi_param PATH_INFO $fastcgi_script_name;
        	fastcgi_param DOCUMENT_ROOT /var/www/webroot/mycontext;
    	    }

        }
        index  index.php index.html index.htm;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

	location ~ /\. { deny all; access_log off; log_not_found off; }

	location ~ \.php$ {
		location ~ /\. { deny all; access_log off; log_not_found off; }
        	include /etc/nginx/fastcgi_params;
	        fastcgi_pass 127.0.0.1:9000;
        	fastcgi_param SCRIPT_FILENAME /var/www/webroot$fastcgi_script_name;
	        fastcgi_param PATH_INFO $fastcgi_script_name;
        	fastcgi_param DOCUMENT_ROOT /var/www/webroot;
        }

    }

server {
        listen       80;
        server_name  domain.com;

	include /etc/nginx/aliases.conf;
        location / {
            root   /var/www/webroot/ROOT;
            index  index.html index.htm index.php;

    	    location ~ \.php$ {
    	    	location ~ /\. { deny all; access_log off; log_not_found off; }
	    	include /etc/nginx/fastcgi_params;
	        fastcgi_pass 127.0.0.1:9000;
        	fastcgi_param SCRIPT_FILENAME /var/www/webroot/ROOT$fastcgi_script_name;
	        fastcgi_param PATH_INFO $fastcgi_script_name;
        	fastcgi_param DOCUMENT_ROOT /var/www/webroot/ROOT;
    	    }

        }
        index  index.php index.html index.htm;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

	location ~ /\. { deny all; access_log off; log_not_found off;

	location ~ \.php$ {
		location ~ /\. { deny all; access_log off; log_not_found off; }
        	include /etc/nginx/fastcgi_params;
	        fastcgi_pass 127.0.0.1:9000;
        	fastcgi_param SCRIPT_FILENAME /var/www/webroot$fastcgi_script_name;
	        fastcgi_param PATH_INFO $fastcgi_script_name;
        	fastcgi_param DOCUMENT_ROOT /var/www/webroot;
        }

    }

NGINX Web Server Configuration

7.  Save the changes, restart the NGINX server and check the results.

Multiple Custom Domains on NGINX Web Server

Now that was easy and fast! Registration of multiple custom domains is a very easy and inexpensive task. Now, binding and running on an NGINX web server becomes much easier too. Benefit from all of the advantages of having multiple domain names, with Jelastic.

Related articles:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

Leave a Reply

Subscribe to get the latest updates