0

I have two servers A and B configured using nginx, each with domain a.domain.com and b.domain.com. The problem with B is that its port 80 is blocked, so I thought I'd reroute all requests, including both port 80 and port 443 traffic for server A to server B, rewriting it to HTTPS in the process. Here's my attempt at it.

A

server {
  listen 80;
  server_name a.domain.com
  location / {
    return 301 https://b.domain.com$request_uri;
  }
}

server {
  listen 443;
  server_name a.domain.com
  ssl on;
  ssl_certificate       /* not shown */
  ssl_certificate_key   /* not shown */
  location / {
    return 301 https://b.domain.com$request_uri;
  }
}

B

server {
  listen 443;
  server_name b.domain.com
  ssl on;
  ssl_certificate       /* not shown */
  ssl_certificate_key   /* not shown */
  location / {
    proxy_redirect http://127.0.0.1:8080 https://b.domain.com;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass_request_headers      on;
    proxy_pass http://127.0.0.1:8080;        # this is to tomcat
    proxy_set_header X-Real-IP $remote_addr;
  }
}

The problem is that it doesn't work. Directly accessing something like https://b.domain.com/folder works, http://a.domain.com and https://a.domain.com jumps to https://b.domain.com, but neither http://a.domain.com/folder or https://a.domain.com/folder redirects as intended (to https://b.domain.com/folder). In the process, as shown in my browser, the HTTPS scheme is dropped; but because server B has no port 80 access the request times out. Something I think is noteworthy is that a.domain.com is configured to use a wildcard certificate while b.domain.com is configured to use a certificate dedicated to the subdomain.

Help?

8
  • 1
    The configuration you've posted looks correct. So something else is going on. Consider showing the actual configuration, including the real domain name(s). Why we ask for this Apr 28 '18 at 0:47
  • Hi Michael, thank you for your response. I wasn't aware of the convention; but all a.domain.com in the question would be p.cgs.news and b.domain.com would be pi.cgs.news. I've temporarily removed all irrelevant entries in the configuration files on the two servers, and restarted them. The problem persists. Apr 28 '18 at 1:11
  • 1
    I can't reproduce this. I load http://p.cgs.news/folder and get redirected properly to https://pi.cgs.news/folder/ and see the page containing only the word "Working". Apr 28 '18 at 1:33
  • That's very weird. Thank you for your time, I'll try to figure it out on my own. Apr 28 '18 at 2:12
  • 2
    Was the redirection ever different/wrong in the past? Your browser(s) may have cached it -- and that can differ by URL. Apr 28 '18 at 7:42
0

You forgot the ssl offloading:

server {
   listen 443 ssl;
   server_name b.domain.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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