How to redirect port 80 and 8080 to 443 using nginx for a Jenkins server

I am try to redirect anything going to port 80 and 8080 to 443 (https) using nginx. This is for a Jenkins server. I am using ubuntu. This is the nginx config I have at the moment:

server {
  listen 80;
  server_name jenkins.mydomain.com;

  location / {
    proxy_pass          http://localhost:8080;
    proxy_set_header    Host      $host;
    proxy_redirect      http://localhost:8080 https://jenkins.mydomain.com;
  }

  return 301 https://jenkins.mydomain.com$request_uri;
}

server {
  listen [::]:443 ssl ipv6only=on;
  listen 443 ssl;
  server_name jenkins.mydomain.com;

  ssl on;
  ssl_certificate /path/to/wildcard.mydomain.com.crt;
  ssl_certificate_key /path/to/wildcard.mydomain.com.key;

  location / {
    add_header Cache-Control private;
    expires epoch;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Host $host;
    proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    proxy_ssl_server_name on;

    include /etc/nginx/proxy_params;
    proxy_pass          http://localhost:8080;
    proxy_read_timeout  90s;
    proxy_redirect      http://localhost:8080 https://jenkins.mydomain.com;
  }
}

As you can see I tried adding the proxy related headers to the port 80 server block but that is not working. When I go to http://jenkins.mydomain.com or http://jenkins.mydomain.com:8080 it does not redirect to https://jenkins.mydomain.com. How do I redirect anything going to port 80 and 8080 to 443?


I am using this config, maybe from any documentation page and it is working for years without any problem. It should be probably better but for my internal usage is it enough.

upstream jenkins {
  server 127.0.0.1:8080 fail_timeout=0;
}

server { 
        listen 80; 
        listen [::]:80;
        server_name XXXX fail_timeout=0;
        return 301 https://XXXXX$request_uri; 
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    ssl on; 
    ssl_certificate /etc/nginx/ssl/XXXX.cert.pem;
    ssl_certificate_key /etc/nginx/ssl/XXXX.key.pem;

    server_name XXXXX;

    location / { 

      proxy_set_header        Host $host:$server_port;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;
      proxy_pass http://jenkins;
      proxy_redirect http:// https://;
      add_header Pragma "no-cache";
    }   
}