0

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.

thelastray
  • 51
  • 2

2 Answers2

0

I don't use Openshift but it seems to me that there is something wrong in your DNS settings where you've created a CNAME for your root domain:

Name: @; Value: www.example.com

According to DNS specifications you cannot create CNAME record for your root domain. Even if your DNS server allow you to do that, it will not work.

Check out this question to see why is that not allowed.

madz
  • 123
  • 1
  • 6
  • I was not sure about the CNAME for root domain, as well. However, I just gave it a try. But, as you can I can not use A name, also, since the IP address is not static (`<%= ENV['OPENSHIFT_PHP_IP'] %>:<%= ENV['OPENSHIFT_PHP_PORT'] %>`) – thelastray Jul 21 '17 at 06:49
  • In that case, you need a third party with some advanced DNS server to let you CNAME your root domain. I myself am part of a team to create frfra.com projct that does that for free, But it is not completed yet, You can use cloudflare.com – madz Jul 21 '17 at 16:45
0

As far as DNS goes, as pointed out by another answer, you cannot do @ CNAME www, i.e., cannot point from the root to a subdomain. However, doing the opposite, www CNAME @, i.e., pointing from a subdomain back to the root, should be entirely acceptable, and does not have to be symmetric to the way the web-side of the domain is represented (e.g., web-site-wise, you could still point from root to a subdomain, even though you're doing the exact opposite on the DNS side).

As far as the web-site way is concerned, it would seem that it would make sense to have both server definitions in the same file, where you simply manually prefix www. right in front of <%= ENV['OPENSHIFT_APP_DNS'] %> for one of the servers, e.g., server_name www.<%= ENV['OPENSHIFT_APP_DNS'] %>;.

Alternatively, although it wouldn't be the most efficient solution, you could also do a conditional redirect with an if statement in a single server context (e.g., one with server_name .example.com;):

if ($host != "www.example.com") {
    rewrite ^   http://www.example.com$request_uri? redirect;
}

Alternatively, if you would rather prefer pretty but less efficient code, you could also use the regular expressions:

if ($host !~ "^www\.") {
    rewrite ^   http://www.$host$request_uri?   redirect;
}
cnst
  • 12,948
  • 7
  • 51
  • 75
  • I **can not** _simply_ add a 'www.' prefix at '<%= ENV['OPENSHIFT_APP_DNS'] %>', since it points to the sub-domain of 'rhcliud.com'; whereas I need to redirect 'example.com' to 'www.example.com' which are mapped through aliases at Openshift console. The same applies for you second option, too. – thelastray Jul 24 '17 at 05:25
  • @thelastray, maybe it shouldn't point to a subdomain, then? I'm not too sure that I follow your need to use OpenShift templates, especially if they apparently don't let you handle aliases correctly? Why not simply hardcode the values within the nginx file instead of creating a plethora of templates? – cnst Jul 24 '17 at 05:44
  • `@cnst`I am afraid. I am not a web server guy. Could you please explain how can I hardcode the values in nginx config. – thelastray Jul 24 '17 at 11:54
  • @thelastray It sounds like you may not be aware that you can have multiple server declarations in a single file. Basically, just define a whole separate (and hardcoded) `server` right next to your original one, in the same file, without using any of your template variables, if none have the useful data in them. – cnst Jul 24 '17 at 22:38
  • `@cnst` Yes, I have tried adding a separate server block in my config as above in my original question. But this gives **503 service temporarily unavailable** – thelastray Jul 25 '17 at 06:20
  • @thelastray, who gives you that error? That's not generally an nginx error. – cnst Jul 25 '17 at 06:40
  • `@cnst' When I am trying to access my website, with that extra server block in my config file. Actually that error is from Openshift Apache server because of wrong configuration, I believe. – thelastray Jul 25 '17 at 06:50
  • nginx lets you have as many servers as you require in a single file; indeed, the problem seems unrelated to nginx – cnst Jul 25 '17 at 06:54
  • No, this not a problem of nginx, in particular. Rather it is perhaps more related to openshift configuration and domain forwarding things. – thelastray Jul 25 '17 at 07:00