How should I set Multiple Custom Domains on an Nginx Web Server

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 can 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 Cloudjiffy PaaS you can achieve all these actions right in the cloud in just a few minutes!

1. Sign up and log into your Cloudjiffy 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 the 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's proper functionality. Then enter your PHP environment name and click on the Create button.

  


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


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. 

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


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.


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 nginx directory. Specify your settings in the server block by following the next example:

server {
listen 80;
listen [::]:80;
server_name domain.com;

include /etc/nginx/aliases.conf;

if ($http_x_remote_port = '' ) {
set $http_x_remote_port $remote_port;
}

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 unix:/var/run/php-fpm.socket;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param PATH_INFO $fastcgi_script_name;
}

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


location ~ ^/\.well-known { root /var/www/webroot/ROOT; allow all;}
location ~ /\.well-known { root /var/www/webroot; allow all;}
location ~ /\. { deny all; access_log off; log_not_found off; }
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location ~ \.php$ {
location ~ /\. { deny all; access_log off; log_not_found off; }
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.socket;
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;
listen [::]:80;
server_name domain.org;

include /etc/nginx/aliases.conf;

if ($http_x_remote_port = '' ) {
set $http_x_remote_port $remote_port;
}

location / {
root /var/www/webroot/ROOT-789;
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 unix:/var/run/php-fpm.socket;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param PATH_INFO $fastcgi_script_name;
}

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


location ~ ^/\.well-known { root /var/www/webroot/ROOT-789; allow all;}
location ~ /\.well-known { root /var/www/webroot; allow all;}
location ~ /\. { deny all; access_log off; log_not_found off; }
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
location ~ \.php$ {
location ~ /\. { deny all; access_log off; log_not_found off; }
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.socket;
fastcgi_param SCRIPT_FILENAME /var/www/webroot$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/webroot;
}

}


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


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 4420