django 应用程序只能使用端口 8000

django application only working with port 8000

我正在做一个项目,我把它托管在 ec2 上,看看是否一切正常。 目前它只是一个没有端点的单页应用程序。

问题是它只有在我使用 my_domain:8000 或 my_ip:8000

时才有效

这是我写的配置文件。

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;
}

}

我启用它使用:

sudo ln -s /etc/nginx/sites-available/config-file /etc/nginx/sites-enabled/

之后我使用-

重新启动了nginx
sudo systemctl restart nginx

然后使用-

启动服务器
python3 manage.py runserver 0.0.0.0:8000

这是我第一次这样做,我认为我在配置文件或启用配置文件时做错了,但我无法弄清楚。

更新配置文件后-

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;
}

}

希望您使用的是 nginx 和 gunicorn

  • 设置 gunicorn
  • 配置并运行你的 nginx 监听端口 80 而不是 8000

然后nginx会代理将django请求传递给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;
    }
}

创建 gunicorn 服务文件 /etc/systemd/system/my_gunicorn.服务

[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

启动gunicorn服务和nignx后, 按照 link 了解配置 nignx-gunicorn-django 的完整详细信息。

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

同时检查您的 aws 安全组。