title: Nginx: How to reverse proxy 2 specific ports to another specific ports with the same server_name?
body: <p>I'm trying to use Nginx reverse proxy port 80 to port 5000 and port 8080 to port 8081.
  I've configured ssl so GET <code>http://DOMAIN:80</code> with redirect to port 443 - this works fine.</p>
  
  <p>I'v configured another server block with subdomain <code>server_name api.DOMAIN</code> listen to port 8080. The reverse proxy suppose to pass requests to <code>localhost:8081</code> but when I run 
  <code>curl http://api.DOMAIN:8080/</code> I get <code>Operation timed out</code>.
  It seems that Nginx does not actually listen to port 8080 although when running </p>
  
  <pre><code>lsof -i :8080
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>nginx   20836     root   10u  IPv4 3939217      0t0  TCP *:http-alt (LISTEN)
  nginx   20840 www-data   10u  IPv4 3939217      0t0  TCP *:http-alt (LISTEN)
  </code></pre>
  
  <p>The configuration I use:</p>
  
  <pre><code>server {
  
          server_name DOMAIN;
  
          location / {
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header X-Forwarded-Proto $scheme;
               proxy_set_header X-Forwarded-Port $server_port;
  
               proxy_pass http://localhost:5000/;
          }
  
      listen 443 ssl; 
      ssl_certificate PATH_TO_PEM_FILE/PEM.pem; 
      ssl_certificate_key PATH_TO_PRIVATE_KEY/privkey.pem;
      include /etc/letsencrypt/options-ssl-nginx.conf; 
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
  
  }
  
   server {
  
          server_name api.DOMAIN;
          listen 8080;
  
          location / {
                  proxy_pass http://localhost:8081/;
          }
  }
  
  server {
      if ($host = DOMAIN) {
          return 301 https://$host$request_uri;
      }
  
  
  
          server_name askalgotrader.com;
      listen 80;
      return 404; # managed by Certbot
  
  
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>