Nginx – Running Code server on subdomain with nginx reverse-proxy

nginxreverse-proxyUbuntu

I have a flask web server running on an ubuntu server on port 8000, with an nginx reverse proxy to port 80. I also want to run a code-server from code.com on a subdomain (code.example.com) at the same time. I set up code server to run on port 8443, and I have a reverse proxy configured the way they specify on their github page, but whenever I go to code.example.com, I am redirected to localhost:8443. This is my nginx site config (/etc/nginx/sites-enabled/example.com):

server {
server_name www.example.com example.com;

location / {

include uwsgi_params;
uwsgi_pass unix:/home/ubuntu/server/server.sock;
proxy_pass http://127.0.0.1:8000/;

}


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot




}
server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



listen 80;
server_name www.example.com example.com;
    return 404; # managed by Certbot




}

server {
  listen 80;
  listen [::]:80;
  server_name code.example.com;
    location / {
       proxy_pass http://localhost:8443/;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection upgrade;
       proxy_set_header Accept-Encoding gzip;
    }
 }

I have not found a solution that works, I have even tried this adding before proxy_pass

   proxy_redirect off;
   proxy_set_header Host code.example.com;

To handle any redirects inside code-server, but it should be able to be hosted on a domain name, even a subdomain. I appreciate any help, so please respond if you have an idea! Thank you.

NOTE: Please tell me if this is not the correct site to post this question, and I will move it, I put it here because I saw other reverse proxy nginx questions.

Best Answer

I can't determine why your config isn't working, but I can provide you with a template I've used to do the same thing. Be sure to replace the path to your SSL certificates as needed.

I've used Certbot with this config, and it works well, even with a single certificate for multiple domains.

# ... 
http {
  server {
    listen  80;
    listen  [::]:80;
    server_name example.com;
    return 301 https://example.com$request_uri;
  }

  server {
    listen       443 ssl;
    listen       [::]:443 ssl;
    server_name  example.com;
    # relevant ssl settings here...

    location / {
      proxy_pass  http://127.0.0.1:8000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
    }

  ssl_certificate <path_to_certbot_fullchain.pem> # managed by Certbot
  ssl_certificate_key <path_to_certbot_privkey.pem> # managed by Certbot
  }

  server {
    listen  80;
    listen  [::]:80;
    server_name sub.example.com;
    return 301 https://sub.example.com$request_uri;
  }

  server {
    listen       443 ssl;
    listen       [::]:443 ssl;
    server_name  sub.example.com;
    # relevant ssl settings here...

    location / {
      proxy_pass  http://127.0.0.1:8443;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
    }
  }

  ssl_certificate <path_to_certbot_fullchain.pem> # managed by Certbot
  ssl_certificate_key <path_to_certbot_privkey.pem> # managed by Certbot
}
# ...