Sunday, February 5, 2017

How to redirect root domain to www using Openshift v2 and nginx?

I am not able redirect my example.com request to www.example.com and they are now both different websites, pointing to the same application.



Background:



I have installed php 7 with nginx on Openshift ver. 2 using this. I have added 2 aliases for the application deployed, as follows:




  1. Application URL: myapp.rhcloud.com;

    Domain Name: www.example.com

  2. Application URL: myapp.rhcloud.com;
    Domain Name: example.com



I have also added the following DNS detailes in CNAME:




  1. Name: www.example.com; Value: myapp.rhcloud.com

  2. Name: @; Value: www.example.com




My nginx config file (erb) is as follows:



server {
root <%= ENV['OPENSHIFT_REPO_DIR'] %>/www;
listen <%= ENV['OPENSHIFT_PHP_IP'] %>:<%= ENV['OPENSHIFT_PHP_PORT'] %>;
server_name <%= ENV['OPENSHIFT_APP_DNS'] %>;
index index.php index.html index.htm <%= ENV['NGINX_EXTRA_INDEX'] %>;
set_real_ip_from <%= ENV['OPENSHIFT_PHP_IP'] %>;

real_ip_header X-Forwarded-For;


which becomes:



server {
root /path/to/my/app-root/folder/;
listen 127.11.2.1:8080;
server_name myapp.rhcloud.com;
index index.php index.html index.htm ;

set_real_ip_from 127.11.2.1;
real_ip_header X-Forwarded-For;


I have tried to add another server block in my nginx config file like the following:



server {
server_name example.com;
return 301 https://www.example.com$request_uri;
}



However I am not sure what should I use in place of example.com.

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...