Home > Blockchain >  django application only working with port 8000
django application only working with port 8000

Time:11-16

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.

after updating config file-

server {
listen 80;
server_name mydomain.info;

client_body_buffer_size 10k;

location = /favicon.ico { access_log off; log_not_found off; }
location /staticfiles/ {
    root /home/ubuntu/dev/myproject;
}
location /media/  {
    root /home/ubuntu/dev/myproject;
}

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/aman-personal/aman- 
    personal.sock;
    proxy_read_timeout 120;
}

}

CodePudding user response:

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.

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04.

Also check your aws security groups.

  • Related