Skip to content

Instantly share code, notes, and snippets.

@talentlessguy
Created May 17, 2020 15:25
Show Gist options
  • Save talentlessguy/9f3134808246226fa33bba881c5f6e7f to your computer and use it in GitHub Desktop.
Save talentlessguy/9f3134808246226fa33bba881c5f6e7f to your computer and use it in GitHub Desktop.
Next.js + Django + Nginx HTTPS setup

Next.js + Django + HTTPS setup

  1. Install certbot:
sudo apt-get install certbot python3-certbot-nginx
  1. Update your config with this:

/etc/nginx/nginx.conf:

http {
        server {
          listen 80 default_server;
          listen [::]:80 default_server;

          root /var/www/html;

          index index.html index.htm index.nginx-debian.html;

          server_name <example.com> www.<example.com>;

location ~ /.well-known {

        allow all;

        root /usr/share/nginx/html;

}
 location / {
                proxy_pass http://localhost:3000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
  }
           location /api {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        }

                location /media {
                proxy_pass http://localhost:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                }

  location /admin {
                proxy_pass http://localhost:8000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
  }
  location /static {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
  }

}

/etc/nginx/sites-available/default:

server {

  listen 80 default_server;

  listen [::]:80 default_server;



  root /var/www/html;

  index index.html index.htm index.nginx-debian.html;



  server_name <example.com>;



  location / {

    # try_files $uri $uri/ =404;

  }

  

  # for letsencrypt

  location ~ /.well-known {

    allow all;

  }

}
  1. Run certbot
sudo certbot --nginx -d <your domain>
  1. Enjoy https!

Written by @v1rtl right after making it work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment