0

Lets assume I have a domain called example.com along with the following 'n' number of subdomains all of them with 'A' record to same IP address(Assume Server A).

a.example.com,b.example.com,c.example.com ... z.example.com

There is another subdomain 'email.example.com' with CNAME set to 'mailgun.org'.

Now I have Server A with the following nginx configuration wherein I want to forward all HTTP requests to HTTPS.

server {
    listen 80;
    return 301 https://$host$request_uri;
}

And set of code blocks to handle individual sub domain requests to 443.

server {
    listen 443 ssl;
    server_name  a.example.com;
    //SSL Details
    location / {
            root /var/www/a/;
    }

Similarly for rest of the sub domains except email.example.com which points to mailgun.org.

Now, http://email.example.com they will be redirected to http://mailgun.com. (At DNS level)

http://example.com they will be redirected to https://example.com. (At Server level).

http://a.example.com they will be redirected to https://a.example.com. (At server level)

Problem: After visiting to http://example.com, when someone tries to visit http://email.example.com they are being redirected to https://email.example.com by browser. Since I have no control over mailgun.org, I can handle https requests for email.example.com

Is there anyway to set nginx config where in every request to HTTP will be redirected to HTTPS except email.example.com?

8
  • What you describe sounds rather improbable - the issue with http:// email.example.com appears to be occurring in the browser or in DNS - hence nginx configuration has nothing to do with it. I suggest your first course of action is to try to identify empirically what DNS / the browser / nginx are actually doing.
    – symcbean
    Jan 24, 2017 at 9:48
  • Firstly, there is no redirection "at DNS level". Secondly, what SSL details do you use? Is there an HSTS header? Jan 24, 2017 at 9:57
  • @symcbean Exactly., How do I tell the browser not to redirect http:// email.example.com to https? When I visit example.com its redirecting to example.com, but after that browsers are redirecting even email.example.com to https.
    – Karthik RP
    Jan 25, 2017 at 6:37
  • @RichardSmith When I meant "at DNS level" I was talking about CNAME record. I'm using rapidSSL wildcard SSL. I've set "Strict-Transport-Security: max-age=31536000; includeSubDomains" in nginx.
    – Karthik RP
    Jan 25, 2017 at 6:41
  • 1
    There is your problem. Your HSTS header instructs the browser to use https://email.example.com. If you must use http on your subdomain, you will need to remove includeSubDomains and wait 31536000 seconds ;-) Jan 25, 2017 at 8:41

1 Answer 1

1

Ok, I found the problem.

I changed my nginx configuration for example.com to not to include subdomains.

ie., Changed the following line

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

as below.,

add_header Strict-Transport-Security "max-age=31536000;" always;

So that browsers now don't add header to sub domains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.