title: NGINX: How do I remove a port when performing a reverse proxy?
body: <p>I have an Nginx reverse proxy set up which is being used as an SSL offload for several servers such as confluence. I've got it successfully working for taking <a href="http://confluence" rel="nofollow noreferrer">http://confluence</a> and <a href="https://confluence" rel="nofollow noreferrer">https://confluence</a> but when I try to redirect <a href="http://confluence:8090" rel="nofollow noreferrer">http://confluence:8090</a>, it tries to go to <a href="https://confluence:8090" rel="nofollow noreferrer">https://confluence:8090</a> and fails.</p>
  
  <p>How can I remove the port from the URL?</p>
  
  <p>The config below is a bit trimmed but maybe helpful? Is the $server_port bit in the headers causing the problem?</p>
  
  <pre><code>server {
      listen      8090;
      server_name confluence;
  
      return 301 https://confluence$request_uri;
  }
  
  server {
      listen      443 ssl http2;
      server_name confluence;
      location / {
          proxy_http_version 1.1;
          proxy_pass http://confbackend:8091
          proxy_set_header X-Forwarded-Host $http_host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header Host $server_name:$server_port;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header Upgrade $http_upgrade; #WebSocket Support
          proxy_set_header Connection $connection_upgrade; #WebSocket Support
     }
  }
  </code></pre>
  
  <p>Seems like a lot of answers here involve <a href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect" rel="nofollow noreferrer">http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect</a> but I find no solace in that confusing mess.</p>
  
  <p>I also would have thought you'd have a single server but I was trying the advice from <a href="https://serverfault.com/questions/815797/nginx-rewrite-to-new-protocol-and-port">https://serverfault.com/questions/815797/nginx-rewrite-to-new-protocol-and-port</a></p>
  
  <p>I tried messing with the <code>port_in_redirect off;</code> option but maybe I was using it wrong?</p>
  
  <hr>
  
  <p><strong>EDIT 1:</strong> Add conf files</p>
  
  <p>The files below are modifications from the <a href="https://github.com/jfrog/artifactory-docker-examples/tree/master/docker-compose/artifactory" rel="nofollow noreferrer">Artifactory nginx setup</a>. I used their setup initially and added additional conf files (in ./conf.d/) for other RP endpoints.</p>
  
  <p><strong>Confluence.conf</strong></p>
  
  <pre><code>server {
    listen 8090 ssl http2;
    server_name confluence.domain.com confluence;
    ## return 301 https://confluence.domain.com$request_uri;
    proxy_redirect https://confluence.domain.com:8090 https://confluence.domain.com;
  }
  
  server {
  
    ## add ssl entries when https has been set in config
    ssl_certificate  /data/rpssl/confluence.pem;
    ssl_certificate_key  /data/rpssl/confluence_unencrypted.key;
  
    ## server configuration
    listen 443 ssl http2;
    server_name confluence.domain.com confluence;
  
    add_header Strict-Transport-Security max-age=31536000;
  
    if ($http_x_forwarded_proto = '') {
      set $http_x_forwarded_proto  $scheme;
    }
    ## Application specific logs
    access_log /var/log/nginx/confluence-access.log timing;
    error_log /var/log/nginx/confluence-error.log;
    client_max_body_size 0;
  
    proxy_read_timeout    1200;
    proxy_connect_timeout 240;
  
    location / {
      proxy_http_version  1.1;
      proxy_pass          http://backendconfluence.domain.com:8091;
  
      proxy_set_header    X-Forwarded-Host  $http_host;
      proxy_set_header    X-Forwarded-Proto $scheme;
      proxy_set_header    Host              $server_name:$server_port;
      proxy_set_header    X-Forwarded-For   $remote_addr;
      proxy_set_header    Upgrade           $http_upgrade; # WebSocket Support
      proxy_set_header    Connection        $connection_upgrade; # WebSocket support
    }
  }
  </code></pre>
  
  <p><strong>nginx.conf</strong></p>
  
  <pre><code># Main Nginx configuration file
  worker_processes  4;
  
  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;
  
  worker_rlimit_nofile  4096;
  
  events {
    worker_connections  2048;
  }
  
  
  http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
  
    variables_hash_max_size 1024;
    variables_hash_bucket_size 64;
    server_names_hash_max_size 4096;
    server_names_hash_bucket_size 128;
    types_hash_max_size 2048;
    types_hash_bucket_size 64;
    proxy_read_timeout 2400s;
    client_header_timeout 2400s;
    client_body_timeout 2400s;
    proxy_connect_timeout 75s;
    proxy_send_timeout 2400s;
    proxy_buffer_size 32k;
    proxy_buffers 40 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 250m;
    proxy_http_version 1.1;
    client_body_buffer_size 128k;
  
    map $http_upgrade $connection_upgrade { #WebSocket support
      default upgrade;
      '' '';
    }
  
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
  
    log_format timing 'ip = $remote_addr '
    'user = \"$remote_user\" '
    'local_time = \"$time_local\" '
    'host = $host '
    'request = \"$request\" '
    'status = $status '
    'bytes = $body_bytes_sent '
    'upstream = \"$upstream_addr\" '
    'upstream_time = $upstream_response_time '
    'request_time = $request_time '
    'referer = \"$http_referer\" '
    'UA = \"$http_user_agent\"';
  
    access_log  /var/log/nginx/access.log  timing;
  
    sendfile        on;
    #tcp_nopush     on;
  
    keepalive_timeout  65;
  
    #gzip  on;
  
    include /etc/nginx/conf.d/*.conf;
  }
  </code></pre>