# AWS+Django+Nginx+Gunicorn

**URL:** <https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404>\
**Category:** Help\
**Created:** [August 5, 2022, 7:12pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404 "2022-08-05T19:12:28Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![snswrld](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/snswrld/32/62928_2.png) [@snswrld](https://community.letsencrypt.org/u/snswrld)\
**Post date:** [August 5, 2022, 7:12pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/1 "2022-08-05T19:12:29Z")

</div>

Please fill out the fields below so we can help you better. Note: you must provide your domain name to get help. Domain names for issued certificates are all made public in Certificate Transparency logs (e.g. [crt.sh | example.com](https://crt.sh/?q=example.com)), so withholding your domain name here does not increase secrecy, but only makes it harder for us to provide help.

My site was working but I let my cert expire thinking that it would renew automatically the way I set it up. It did not so I ran certbot after certbot I get an error that says my site isn't redirecting properly. It was working before. I shut down the server and redeployed the site from scratch in a new location on the server, now I'm back to the beginning. Please help.

My domain is: [cryptonik.io](http://cryptonik.io)

I ran this command: sudo certbot --nginx -d [cryptonik.io](http://cryptonik.io) -  
d [www.cryptonik.io](http://www.cryptonik.io)

It produced this output:  
too late for that  
My web server is (include version): AWS

The operating system my web server runs on is (include version): NAME="Amazon Linux"  
VERSION="2"  
ID="amzn"  
ID\_LIKE="centos rhel fedora"  
VERSION\_ID="2"  
PRETTY\_NAME="Amazon Linux 2"  
ANSI\_COLOR="0;33"  
CPE\_NAME="cpe:2.3:o:amazon:amazon\_linux:2"  
HOME\_URL="[https://amazonlinux.com/](https://amazonlinux.com/)"

My hosting provider, if applicable, is: Amazon

I can login to a root shell on my machine (yes or no, or I don't know): no but I have a shell with sudo capability

I'm using a control panel to manage my site (no, or provide the name and version of the control panel): no

The version of my client is (e.g. output of `certbot --version` or `certbot-auto --version` if you're using Certbot): certbot 1.11.0

My NGINX sites available

```nohighlight
server {
        listen 80;
        server_name cryptonik.io www.cryptonik.io;
        return 307 https://$host$request_uri;
        location = /favicon.ico {access_log off; log_not_found off;}
        location /static/{
                root /var/www;
        }
        location /{
                proxy_set_header Host $http_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;
                proxy_pass http://unix:/var/log/gunicorn/cryptonik.sock;
}

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/cryptonik.io/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cryptonik.io/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.cryptonik.io) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

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

        listen 80;
        server_name cryptonik.io www.cryptonik.io;
    return 404; # managed by Certbot

```

---

<div class="post-metadata">

**Author:** ![rg305](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/rg305/32/91314_2.png) [@rg305](https://community.letsencrypt.org/u/rg305)\
**Post date:** [August 5, 2022, 7:39pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/2 "2022-08-05T19:39:59Z")

</div>

Hi @snswrld, and welcome to the LE community forum 🙂

The first server block shown is listening to both (port 80 and port 443).  
That is extremely difficult to do correctly.  
[it should probably just be listening on port 443; As it has a certificate for that]

> [@snswrld](#):
>
> It produced this output:  
> too late for that

We would need more of that output to work with.  
Can you show the logfile?  
~~[usually: `/etc/letsencrypt/letsencrypt.log`]~~  
[usually: `/var/log/letsencrypt/letsencrypt.log`]

---

<div class="post-metadata">

**Author:** ![Osiris](https://avatars.discourse-cdn.com/v4/letter/o/839c29/32.png) [@Osiris](https://community.letsencrypt.org/u/Osiris)\
**Post date:** [August 5, 2022, 7:43pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/3 "2022-08-05T19:43:35Z")

</div>

> [@rg305](#):
>
> The first server block shown is listening to both (port 80 and port 443).

It's in conflict with the block just beneath it 😉

---

<div class="post-metadata">

**Author:** ![rg305](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/rg305/32/91314_2.png) [@rg305](https://community.letsencrypt.org/u/rg305)\
**Post date:** [August 5, 2022, 7:51pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/4 "2022-08-05T19:51:27Z")

</div>

And the one beneath is the one being used:

```nohighlight
curl -Ii http://www.cryptonik.io/
HTTP/1.1 301 Moved Permanently

```

301 NOT 307:

> [@snswrld](#):
>
> `return 307 https://$host$request_uri;`

---

<div class="post-metadata">

**Author:** ![snswrld](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/snswrld/32/62928_2.png) [@snswrld](https://community.letsencrypt.org/u/snswrld)\
**Post date:** [August 5, 2022, 8:04pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/5 "2022-08-05T20:04:09Z")

</div>

cat: /etc/letsencrypt/letsencrypt.log: No such file or directory

There is no logfile under /etc/letsencrypt

I got rid of listen 80; in the top server block. Should I change the 301 redirects to 307?

---

<div class="post-metadata">

**Author:** ![Osiris](https://avatars.discourse-cdn.com/v4/letter/o/839c29/32.png) [@Osiris](https://community.letsencrypt.org/u/Osiris)\
**Post date:** [August 5, 2022, 8:06pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/6 "2022-08-05T20:06:41Z")

</div>

It's usually `/var/log/letsencrypt/letsencrypt.log`, @rg305 needs some coffee 😉

Also 301 is fine: it means the location has permanently changed and not temporarily. You want permanently, as your HTTPS should be sort of the default.

---

<div class="post-metadata">

**Author:** ![snswrld](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/snswrld/32/62928_2.png) [@snswrld](https://community.letsencrypt.org/u/snswrld)\
**Post date:** [August 5, 2022, 8:08pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/7 "2022-08-05T20:08:36Z")

</div>

> [@Osiris](#):
>
> `/var/log/letsencrypt/letsencrypt.log`

2022-08-05 17:14:59,936:DEBUG:certbot.display.util:Notifying user: Congratu  
lations! You have successfully enabled [https://cryptonik.io](https://cryptonik.io) and  
[https://www.cryptonik.io](https://www.cryptonik.io)  
2022-08-05 17:14:59,936:DEBUG:certbot.\_internal.reporter:Reporting to user:  
If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: [Donate - Let's Encrypt](https://letsencrypt.org/donate)  
Donating to EFF: [Support EFF's Work on Let's Encrypt | Electronic Frontier Foundation](https://eff.org/donate-le)

---

<div class="post-metadata">

**Author:** ![MikeMcQ](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/mikemcq/32/52772_2.png) [@MikeMcQ](https://community.letsencrypt.org/u/MikeMcQ)\
**Post date:** [August 5, 2022, 8:27pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/8 "2022-08-05T20:27:12Z")

</div>

> [@snswrld](#):
>
> ` return 307 https://$host$request_uri;`

You should remove that line from your https block. It is creating an infinite loop of redirects

---

<div class="post-metadata">

**Author:** ![snswrld](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/snswrld/32/62928_2.png) [@snswrld](https://community.letsencrypt.org/u/snswrld)\
**Post date:** [August 5, 2022, 8:33pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/9 "2022-08-05T20:33:36Z")

</div>

OK commented out that line and now I get 502 bad gateway error

---

<div class="post-metadata">

**Author:** ![Osiris](https://avatars.discourse-cdn.com/v4/letter/o/839c29/32.png) [@Osiris](https://community.letsencrypt.org/u/Osiris)\
**Post date:** [August 5, 2022, 8:36pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/10 "2022-08-05T20:36:16Z")

</div>

> [@snswrld](#):
>
> `proxy_pass http://unix:/var/log/gunicorn/cryptonik.sock;`

~~Is this even allowed, combining the `http://` protocol with `unix:`? Doesn't make much sense to me, I don't think you can do "HTTP" using an Unix socket anyway.~~ I stand corrected, [Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass) mentions this as a valid thing 🙄 My bad.

In that case it might throw a 502 for many reasons, perhaps gunicorn isn't running. Please see the nginx and/or gunicorn log files for more info.

Although I think the `/var/log/` tree is not the correct place for a socket.

---

<div class="post-metadata">

**Author:** ![MikeMcQ](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/mikemcq/32/52772_2.png) [@MikeMcQ](https://community.letsencrypt.org/u/MikeMcQ)\
**Post date:** [August 5, 2022, 8:42pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/11 "2022-08-05T20:42:06Z")

</div>

> [@snswrld](#):
>
> ` proxy_pass http://unix:/var/log/gunicorn/cryptonik.sock;`

Nevermind - lots of cross-posts  
~~I wondered about http and unix too but apparently it is ok. The nginx docs say you need a colon after `.sock` but maybe just if a /uri/ is used? Not sure~~

> or as a UNIX-domain socket path specified after the word “unix” and enclosed in colons:
> 
> proxy\_pass [http://unix](http://unix):/tmp/backend.socket:/uri/;

---

<div class="post-metadata">

**Author:** ![snswrld](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/snswrld/32/62928_2.png) [@snswrld](https://community.letsencrypt.org/u/snswrld)\
**Post date:** [August 5, 2022, 8:42pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/12 "2022-08-05T20:42:13Z")

</div>

I didn't think it was right but it's on the django site

> **[How To Deploy Django App with Nginx, Gunicorn, PostgreSQL and Let's Encrypt...](https://djangocentral.com/deploy-django-with-nginx-gunicorn-postgresql-and-lets-encrypt-ssl-on-ubuntu/)**
>
> Django is a high-level full-stack open-source web fram

---

<div class="post-metadata">

**Author:** ![snswrld](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/snswrld/32/62928_2.png) [@snswrld](https://community.letsencrypt.org/u/snswrld)\
**Post date:** [August 5, 2022, 8:46pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/13 "2022-08-05T20:46:53Z")

</div>

![image](https://global.discourse-cdn.com/letsencrypt/original/3X/5/e/5ee91e8eac91c6f8d401c50e206b141efb5bd130.png)

---

<div class="post-metadata">

**Author:** ![Osiris](https://avatars.discourse-cdn.com/v4/letter/o/839c29/32.png) [@Osiris](https://community.letsencrypt.org/u/Osiris)\
**Post date:** [August 5, 2022, 8:47pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/14 "2022-08-05T20:47:44Z")

</div>

I think it's a nginx thingy (and perhaps other specific services too). As I understand it, Unix sockets are generic sockets for inter-process communication on the same host. I don't think the "Unix socket" definition per se defines the _content_ of that communication, so processes like nginx can decide to just use HTTP trough their Unix socket instead of a TCP/IP socket. And to configure such a thing, you get weird values as `http://unix:/...` apparently 😛

Anyway, please refer to the nginx and/or gunicorn log files to see what's going on with your 502 error.

---

<div class="post-metadata">

**Author:** ![MikeMcQ](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/mikemcq/32/52772_2.png) [@MikeMcQ](https://community.letsencrypt.org/u/MikeMcQ)\
**Post date:** [August 5, 2022, 8:48pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/15 "2022-08-05T20:48:44Z")

</div>

You could try gunicorn status

```nohighlight
sudo systemctl status gunicorn

```

Is it running without errors?

---

<div class="post-metadata">

**Author:** ![snswrld](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/snswrld/32/62928_2.png) [@snswrld](https://community.letsencrypt.org/u/snswrld)\
**Post date:** [August 5, 2022, 8:51pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/16 "2022-08-05T20:51:18Z")

</div>

> [@MikeMcQ](#):
>
> `sudo systemctl status gunicorn`

Ok gunicorn failed, so I am going to go track that error down now. I thank you for your help.

---

<div class="post-metadata">

**Author:** ![rg305](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/rg305/32/91314_2.png) [@rg305](https://community.letsencrypt.org/u/rg305)\
**Post date:** [August 5, 2022, 8:57pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/17 "2022-08-05T20:57:42Z")

</div>

> [@Osiris](#):
>
> @rg305 needs some coffee

OMG !  
I'm going back to bed and restarting my entire day - LOL

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/letsencrypt/original/3X/c/a/ca6c06ea1ea201324bba7048c6841ce60236468d.png) [@system](https://community.letsencrypt.org/u/system)\
**Post date:** [September 4, 2022, 8:58pm UTC](https://community.letsencrypt.org/t/aws-django-nginx-gunicorn/182404/18 "2022-09-04T20:58:09Z")

</div>

Closed on September 4, 2022, 8:58pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
