As part of the deployment of a Django website via an Application Loag Balancer + Auto Scaling Group, I'm running into a problem that I've found the origin of, but I can't see how to solve it.
I've configured my instance so that gunicorn launches at startup, and nginx listens on port 80 (redirection of django port 8000 to 80) on the address of the Application Load Balancer.
When my instances are in the auto-scaling-group -> They ping "Unhealthy" and are cut off in a loop.
In order to test at instance level, I SSH directly to the instance via the private IP (the architecture is in a VPC, the ASG launch template is done without public IP). I check nginx and gunicorn, everything seems to work. When I try to check the return via :
curl http://10.0.4.84:80
or
curl http://10.0.4.84:8000
(10.0.4.84 being the private IP address), I get an error on port 80 :
curl: (7) Failed to connect to 10.0.4.84 port 80 after 0 ms: Couldn't connect to server
And a timeout on port 8000.
On the other hand, here's what I don't understand: if I assign a public IP address to the instance and remove it immediately, I can then do
curl http://10.0.4.84:80
or
curl http://10.0.4.84:8000
And it works perfectly. If I create an instance outside the ASG, do this manipulation and place it in the target group of the Application Load Balancer, it appears "Healthy" and the ALB DNS works perfectly!
Do you know how to solve this problem? I'm attaching the nginx and gunicorn configurations, I don't know if that's where it's coming from.
nginx.conf :
user nginx;worker_processes auto;error_log /var/log/nginx/error.log notice;pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;events { worker_connections 1024;}http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80; server_name application_load_balancer_dns_is_here; location / { proxy_pass http://127.0.0.1:8000; 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; } }}
gunicorn.service :
[Unit]Description=gunicorn daemon for datasourceAfter=network.target[Service]User=ec2-userGroup=ec2-userWorkingDirectory=/home/ec2-user/datasource/srcEnvironment="SECRET_NAME_1=my_first_secret"Environment="SECRET_NAME_2=my_second_secret"Environment="SECRET_REGION_NAME=aws_region"ExecStart=/usr/local/bin/gunicorn datasource.wsgi:application --access-logfile - --workers 2 --threads 4 -b 0.0.0.0:8000 --chdir /home/ec2-user/datasource/src[Install]WantedBy=multi-user.target