0

I am trying to rewrite requests to http://www.example.com:88 to https://www.example.com:4430

At the moment, I am stuck with nonstandard ports. Here is my configuration:

upstream pool {
  server 127.0.0.1:5001;
  server 127.0.0.1:5002;
}

server {
  listen 88;
  listen 4430 default ssl;

  if ($ssl_protocol = "") {
    rewrite ^ https://$server_name:4430/$uri permanent;

    # this also fails with https://www.example.com/:4430
    #rewrite ^/(.*)$ https://$host:4430/$1 permanent;
    #rewrite ^ https://www.example.com:4430/ permanent;
  }

  location / {
    proxy_pass http://pool;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

This results in http://www.example.com:88/path being rewritten to https://www.example.com/path:4430

What's the syntax to get the port after the domain name?

Terry G Lorber
  • 565
  • 2
  • 7
  • 12

1 Answers1

3

I suggest use separate server for http redirection and another one for ssl:

server {
  listen 88;
  return 302 https://$host:4430$request_uri;
}
server {
  listen 4430 ssl default_server;
  ...
Alexey Ten
  • 7,922
  • 31
  • 35
Fedor Dikarev
  • 706
  • 4
  • 10
  • The :4430 is still being pushed to the end of the address. – Terry G Lorber Nov 18 '16 at 19:00
  • I've tried `return 302 https://$host:4430$uri;` on my own server and I got: `curl -v -s --resolve example.com:88:195.54.209.14 http://example.com:88/some_url 2>&1 | grep Location` `Location: https://example.com:4430/some_url`. Is it what you want or you get something else? – Fedor Dikarev Nov 18 '16 at 19:14
  • ^ Yes, that's what I want. I was able to switch to default ports and it worked. I'll try again. Thanks. – Terry G Lorber Nov 18 '16 at 19:55