首页 > 解决方案 > ERR_CONNECTION_TIMED_OUT when trying to access AWS EC2 hosted Django application

问题描述

I'm stuck with ERR_CONNECTION_TIMED_OUT in browser when trying to access Django application which is deployed to AWS EC2. A week ago I was able to connect but now when I relaunched instance, changed ALLOWED_HOSTS in .env file the browser just throw me this error. The output in the console says everything is ok. To fix this I've tried to create another instance and deploy the app there and also added Elastic IP address but still got the same error. How can I fix this issue?

I'm using Django with Docker, Nginx proxy and UWSGI.

Nginx proxy configuration (default.conf.tpl):

server {
    listen ${LISTEN_PORT};

    location /static {
        alias /vol/static;
    }

    location / {
        uwsgi_pass              ${APP_HOST}:${APP_PORT};
        include                 /etc/nginx/uwsgi_params;
        client_max_body_size    10M;
    }
}

Nginx proxy Dockerfile (Dockerfile):

FROM nginxinc/nginx-unprivileged:1-alpine

COPY ./default.conf.tpl /etc/nginx/default.conf.tpl
COPY ./uwsgi_params /etc/nginx/uwsgi_params
COPY ./run.sh /run.sh

ENV LISTEN_PORT=8000
ENV APP_HOST=app
ENV APP_PORT=9000

USER root

RUN mkdir -p /vol/static && \
    chmod 755 /vol/static && \
    touch /etc/nginx/conf.d/default.conf && \
    chown nginx:nginx /etc/nginx/conf.d/default.conf && \
    chmod +x /run.sh

VOLUME /vol/static

USER nginx

CMD ["/run.sh"]

run.sh script:

#!/bin/sh

set -e

envsubst < /etc/nginx/default.conf.tpl > /etc/nginx/conf.d/default.conf
nginx -g 'daemon off;'

.env:

...
ALLOWED_HOSTS=18.119.35.216
...

(18.119.35.216 is the Elastic IP address)

uwsgi_params:

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

Here's console output:

[ec2-user@ip-172-31-10-215 deployapp]$ docker-compose -f docker-compose-deploy.yml up
Starting deployapp_db_1 ... done
Recreating deployapp_app_1 ... done
Recreating deployapp_proxy_1 ... done
Attaching to deployapp_db_1, deployapp_app_1, deployapp_proxy_1
app_1    | Waiting for database...
app_1    | Database available!
db_1     |
db_1     | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1     |
db_1     | 2021-06-28 19:45:52.274 UTC [1] LOG:  starting PostgreSQL 13.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20210424) 10.3.1 20210424, 64-bit
db_1     | 2021-06-28 19:45:52.274 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1     | 2021-06-28 19:45:52.274 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1     | 2021-06-28 19:45:52.285 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1     | 2021-06-28 19:45:52.289 UTC [21] LOG:  database system was shut down at 2021-06-28 19:42:26 UTC
db_1     | 2021-06-28 19:45:52.320 UTC [1] LOG:  database system is ready to accept connections
proxy_1  | 2021/06/28 19:45:54 [notice] 8#8: using the "epoll" event method
proxy_1  | 2021/06/28 19:45:54 [notice] 8#8: nginx/1.21.0
proxy_1  | 2021/06/28 19:45:54 [notice] 8#8: built by gcc 10.2.1 20201203 (Alpine 10.2.1_pre1)
proxy_1  | 2021/06/28 19:45:54 [notice] 8#8: OS: Linux 4.14.232-177.418.amzn2.x86_64
proxy_1  | 2021/06/28 19:45:54 [notice] 8#8: getrlimit(RLIMIT_NOFILE): 1024:4096
proxy_1  | 2021/06/28 19:45:54 [notice] 8#8: start worker processes
proxy_1  | 2021/06/28 19:45:54 [notice] 8#8: start worker process 9
app_1    | 
app_1    | 0 static files copied to '/vol/web/static', 128 unmodified.
app_1    | Operations to perform:
app_1    |   Apply all migrations: admin, auth, contenttypes, core, sessions
app_1    | Running migrations:
app_1    |   No migrations to apply.
app_1    | *** Starting uWSGI 2.0.19.1 (64bit) on [Mon Jun 28 19:45:55 2021] ***
app_1    | compiled with version: 10.2.1 20201203 on 28 June 2021 06:29:30
app_1    | os: Linux-4.14.232-177.418.amzn2.x86_64 #1 SMP Tue Jun 15 20:57:50 UTC 2021
app_1    | nodename: c51648f9e62f
app_1    | machine: x86_64
app_1    | clock source: unix
app_1    | detected number of CPU cores: 1
app_1    | current working directory: /app
app_1    | detected binary path: /py/bin/uwsgi
app_1    | !!! no internal routing support, rebuild with pcre support !!!
app_1    | your memory page size is 4096 bytes
app_1    | detected max file descriptor number: 1024
app_1    | lock engine: pthread robust mutexes
app_1    | thunder lock: disabled (you can enable it with --thunder-lock)
app_1    | uwsgi socket 0 bound to TCP address :9000 fd 3
app_1    | Python version: 3.9.5 (default, May  4 2021, 18:33:52)  [GCC 10.2.1 20201203]
app_1    | Python main interpreter initialized at 0x7fb9743e0170
app_1    | python threads support enabled
app_1    | your server socket listen backlog is limited to 100 connections
app_1    | your mercy for graceful operations on workers is 60 seconds
app_1    | mapped 364600 bytes (356 KB) for 4 cores
app_1    | *** Operational MODE: preforking ***
app_1    | WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fb9743e0170 pid: 10 (default app)
app_1    | *** uWSGI is running in multiple interpreter mode ***
app_1    | spawned uWSGI master process (pid: 10)
app_1    | spawned uWSGI worker 1 (pid: 11, cores: 1)
app_1    | spawned uWSGI worker 2 (pid: 12, cores: 1)
app_1    | spawned uWSGI worker 3 (pid: 13, cores: 1)
app_1    | spawned uWSGI worker 4 (pid: 14, cores: 1)
proxy_1  | 136.169.169.223 - - [28/Jun/2021:19:46:08 +0000] "\x16\x03\x01\x02\x00\x01\x00\x01\xFC\x03\x03\xBF\x9C\xCD<;l\xE0w?S\xFD$\x9E9\x10n\x8F\xAB\x0B9\x96\x81P\xED\xDC|\xBE\xA4`\x22gu \xFF\x99\xAE\x15\xB0\xB2zq\x80\x16\x81mVJ\xE09]a\x16\xF0p>\x11 K\xB9\xEA\xCAd\x81\xD3\xD5\x00 \x1A\x1A\x13\x01\x13\x02\x13\x03\xC0+\xC0/\xC0,\xC00\xCC\xA9\xCC\xA8\xC0\x13\xC0\x14\x00\x9C\x00\x9D\x00/\x005\x01\x00\x01\x93\xCA\xCA\x00\x00\x00\x17\x00\x00\xFF\x01\x00\x01\x00\x00" 400 157 "-" "-" "-"
proxy_1  | 136.169.169.223 - - [28/Jun/2021:19:46:09 +0000] "\x16\x03\x01\x02\x00\x01\x00\x01\xFC\x03\x03\xFC\x0C\xFFs\xF5\xC1h\xCB\x19\xB9\x1Cq\xD7\xF8\xCC\xD2V1\x22\xFC\xD7^AB\xC1\xFA\xAF\xFF~f\xAEP \x12\xB8\xCF\xB0\x8B\x22\x81\xF4\x12\x82z\xC7y\xBC\xCD\x1A\x89\x83\xF0&\x9F\xEB\x98\xCA\x09\xDD\x89\xEE\x86\x01z)\x00\x22\x9A\x9A\x13\x01\x13\x02\x13\x03\xC0+\xC0/\xC0,\xC00\xCC\xA9\xCC\xA8\xC0\x13\xC0\x14\x00\x9C\x00\x9D\x00/\x005\x00" 400 157 "-" "-" "-"

If you need more information I'll provide.

标签: djangoamazon-web-servicesdockernginxamazon-ec2

解决方案


推荐阅读