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 {
  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?


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;
| improve this answer | |
  • 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: 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

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.