Issue
I am working on an project and I have hosted it on EC2 to see if everything is working fine or not. It's only a single page application for now with no endpoint.
Problem is it only works if I use my_domain:8000
or my_ip:8000
.
Here is the config file I have written:
server {
listen 8000;
server_name mydomain.info;
# to avoid any error while fetching fevicon
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/dev/myproject;
}
location / {
include proxy_params;
# communicate via socket file created by Gunicorn
proxy_pass http://unix:/home/ubuntu/dev/myproject.sock;
}
}
I enabled it using:
sudo ln -s /etc/nginx/sites-available/config-file /etc/nginx/sites-enabled/
After doing that, I restarted nginx
using:
sudo systemctl restart nginx
Then started server using:
python3 manage.py runserver 0.0.0.0:8000
It's my first time doing this and I think I have done something wrong with config file or while enabling it but I can't figure it out.
Solution
I hope you are using nginx and gunicorn
- setup gunicorn
- configure and run your nginx to listen port 80 instead of 8000
Then nginx will proxy pass the django requests to the gunicorn.
When you run python3 manage.py runserver 0.0.0.0:8000, django development server is starting, (which is not suitable for production), instead use gunicorn, Create a gunicron unit file and enable and start it.
/etc/nginx/config-file
server {
listen 80;
server_name your_ip_address;
client_body_buffer_size 10k;
.......
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/dev/myproject;
}
location /media/ {
root /home/ubuntu/dev/content;
}
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:/home/ubuntu/dev/myproject.sock;
proxy_read_timeout 120;
}
}
create a gunicorn service file /etc/systemd/system/my_gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=nginx
WorkingDirectory=/root/project/myproject
ExecStart=/path to env/bin/gunicorn --workers 4 --bind unix:/home/ubuntu/dev/myproject.sock <myproject>.wsgi:application --access-logfile /var/log/gunicorn/access.log --error-logfile /var/log/gunicorn/error.log
Restart=on-failure
[Install]
WantedBy=multi-user.target
After start the gunicorn service and nignx, Follow the link for complete details to configure nignx-gunicorn-django.
Also check your aws security groups.
Answered By - Jisson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.