Answers: <p>Oops, turns out I just have to remove the line 
  <code>listen [::]:443 ssl ipv6only=on;</code>
  and it works.</p>
  
  <p>it's leftover from running NGINX certbot, I thought that was important</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-01-10T17:38:15.730Z
Answers: You can mark this as answered.
Answers: Levi
Answers: CC BY-SA 4.0
Answers: 2019-01-10T03:49:55.253Z
Answers: 2019-01-10T03:49:55.253Z
Answers: otong
Body: <p>I have two application served in a single server <code>app1.domain.com</code> and <code>app2.domain.com</code>. </p>
  
  <p>I want to proxy pass the request from <code>app1</code> to serve static file and <code>app2</code> to <code>proxy_pass</code> <code>localhost:8000</code></p>
  
  <p>I already have both SSL certificate using letsencrypt, how can I serve both apps using a single server?</p>
  
  <p>I thought I can use different <code>server_name</code>. I read from <a href="https://whatididtodowhatidid.wordpress.com/2014/03/14/subdomains-for-ports-on-same-ubuntu-server-with-nginx-reverse-proxy/" rel="nofollow noreferrer">here</a>: the article use multiple NGINX <code>server</code> with the same port 80. But when I try for the port 443 the test fails "duplicate listen options for..". Here is my current config</p>
  
  <pre><code>server {
      server_name app1.domain.com;
  
      listen [::]:443 ssl ipv6only=on; 
      listen 443 ssl; 
      ssl_certificate /etc/letsencrypt/live/app1.domain.com/fullchain.pem; 
      ssl_certificate_key /etc/letsencrypt/live/app1.domain.com/privkey.pem; 
  
      index index.html;
      root /usr/share/nginx/html/;
  
      location ~* \.(css|js|png|jpg|jpeg)$ {
          try_files $uri $uri/ =404;
          gzip_static on;
      }
  
      location / {
          try_files $uri /index.html =404;
          gzip_static on;
      }
  }
  
  server {
      server_name app2.domain.com;
  
      listen [::]:443 ssl ipv6only=on; 
      listen 443 ssl; 
      ssl_certificate /etc/letsencrypt/live/app2.domain.com/fullchain.pem; 
      ssl_certificate_key /etc/letsencrypt/live/app2.domain.com/privkey.pem; 
  
      index index.html;
      root /usr/share/nginx/html/;
  
      location / {
          proxy_set_header  Host $host;
          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;
  
          # Fix the “It appears that your reverse proxy set up is broken" error.
          proxy_pass    http://localhost:8000/;
          proxy_read_timeout  90;
      }
  }
  
  server {
      if ($host ~ "app1.domain.com|app2.domain.com") {
          return 301 https://$host$request_uri;
      } 
  
      listen 80 default_server;
      listen [::]:80 default_server;
  
      server_name domain.id;
      return 404; 
  }
  </code></pre>
  
  <p>And I thought maybe I could use <code>if ($host = domain)</code> inside the <code>location</code> but it fails too</p>
  
Tags: <nginx>
Title: Multiple Application in single SSL 443 server NGINX
UserDisplayName: otong
ThreadText: # Multiple Application in single SSL 443 server NGINX
  
  (Asked by: username_0 on 2019-01-10)
  
  I have two application served in a single server `app1.domain.com` and `app2.domain.com`.
  
  I want to proxy pass the request from `app1` to serve static file and `app2` to `proxy_pass` `localhost:8000`
  
  I already have both SSL certificate using letsencrypt, how can I serve both apps using a single server?
  
  I thought I can use different `server_name`. I read from [here](https://whatididtodowhatidid.wordpress.com/2014/03/14/subdomains-for-ports-on-same-ubuntu-server-with-nginx-reverse-proxy/): the article use multiple NGINX `server` with the same port 80. But when I try for the port 443 the test fails "duplicate listen options for..". Here is my current config
  
  ```
  server {
      server_name app1.domain.com;
  
      listen [::]:443 ssl ipv6only=on; 
      listen 443 ssl; 
      ssl_certificate /etc/letsencrypt/live/app1.domain.com/fullchain.pem; 
      ssl_certificate_key /etc/letsencrypt/live/app1.domain.com/privkey.pem; 
  
      index index.html;
      root /usr/share/nginx/html/;
  
      location ~* \.(css|js|png|jpg|jpeg)$ {
          try_files $uri $uri/ =404;
          gzip_static on;
      }
  
      location / {
          try_files $uri /index.html =404;
          gzip_static on;
      }
  }
  
  server {
      server_name app2.domain.com;
  
      listen [::]:443 ssl ipv6only=on; 
      listen 443 ssl; 
      ssl_certificate /etc/letsencrypt/live/app2.domain.com/fullchain.pem; 
      ssl_certificate_key /etc/letsencrypt/live/app2.domain.com/privkey.pem; 
  
      index index.html;
      root /usr/share/nginx/html/;
  
      location / {
          proxy_set_header  Host $host;
          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;
  
          # Fix the “It appears that your reverse proxy set up is broken" error.
          proxy_pass    http://localhost:8000/;
          proxy_read_timeout  90;
      }
  }
  
  server {
      if ($host ~ "app1.domain.com|app2.domain.com") {
          return 301 https://$host$request_uri;
      } 
  
      listen 80 default_server;
      listen [::]:80 default_server;
  
      server_name domain.id;
      return 404; 
  }
  
  ```
  
  And I thought maybe I could use `if ($host = domain)` inside the `location` but it fails too
  
  **Tags:** nginx
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-01-10. Score: 1 | Accepted answer
  
  Oops, turns out I just have to remove the line
  `listen [::]:443 ssl ipv6only=on;`
  and it works.
  
  it's leftover from running NGINX certbot, I thought that was important
  
  > **Comments:**
  > **username_1**: You can mark this as answered.  
  
  ---
  
  