【问题标题】:Deploying Django Application on Alibaba ECS在阿里云 ECS 上部署 Django 应用
【发布时间】:2019-02-22 07:11:23
【问题描述】:

我正在尝试在阿里巴巴 ECS 上部署一个简单的 todo 应用程序。

它是一个 Django 应用程序,我正在尝试在 gunicorn 和 nginx 的帮助下部署它。

但是,在设置所需文件后,当我尝试访问 http://149.129.136.180/ 时,我收到消息 - “无法访问该站点。149.129.136.180 响应时间过长。”

以下是所需的详细信息:

操作系统:Ubuntu 16.04.4 LTS(GNU/Linux 4.4.0-117-generic x86_64)

root@iZa2asghjgdfsdjfsgukZ:~# cat /etc/systemd/system/gunicorn.service 
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/root/todo
ExecStart=/root/.virtualenvs/todo_venv/bin/gunicorn --workers 3 --bind unix:/root/todo/todo.sock todo.wsgi:application
[Install]
WantedBy=multi-user.target

root@iZa2asghjgdfsdjfsgukZ:~# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


root@iZa2asghjgdfsdjfsgukZ:~# cat /etc/nginx/sites-available/todo 
server {
  listen 80;
  server_name 149.129.136.180;
  location = /favicon.ico { access_log off; log_not_found off; }
  location / {
      include proxy_params;
      proxy_pass http://unix:/root/todo/todo.sock;
  }
}

root@iZa2asghjgdfsdjfsgukZ:~# sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/nginx.service.d
           └─override.conf
   Active: active (running) since Tue 2018-09-18 17:11:07 CST; 10min ago
  Process: 996 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 1012 ExecStartPost=/bin/sleep 0.1 (code=exited, status=0/SUCCESS)
  Process: 1005 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 1000 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 1009 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1009 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ├─1010 nginx: worker process                           
           └─1011 nginx: worker process                           

Sep 18 17:11:07 iZa2deepdarscg4q4vwgukZ systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 18 17:11:07 iZa2deepdarscg4q4vwgukZ systemd[1]: Started A high performance web server and a reverse proxy server.


root@iZa2asghjgdfsdjfsgukZ:~# sudo journalctl -u gunicorn
-- Logs begin at Sun 2018-04-08 21:14:21 CST, end at Tue 2018-09-18 17:22:20 CST. --
Sep 18 17:03:21 iZa2deepdarscg4q4vwgukZ systemd[1]: Started gunicorn daemon.
Sep 18 17:03:22 iZa2deepdarscg4q4vwgukZ gunicorn[675]: [2018-09-18 17:03:22 +0800] [675] [INFO] Starting gunicorn 19.9.0
Sep 18 17:03:22 iZa2deepdarscg4q4vwgukZ gunicorn[675]: [2018-09-18 17:03:22 +0800] [675] [INFO] Listening at: unix:/root/todo/todo.sock (675)
Sep 18 17:03:22 iZa2deepdarscg4q4vwgukZ gunicorn[675]: [2018-09-18 17:03:22 +0800] [675] [INFO] Using worker: sync
Sep 18 17:03:22 iZa2deepdarscg4q4vwgukZ gunicorn[675]: [2018-09-18 17:03:22 +0800] [796] [INFO] Booting worker with pid: 796
Sep 18 17:03:22 iZa2deepdarscg4q4vwgukZ gunicorn[675]: [2018-09-18 17:03:22 +0800] [801] [INFO] Booting worker with pid: 801
Sep 18 17:03:22 iZa2deepdarscg4q4vwgukZ gunicorn[675]: [2018-09-18 17:03:22 +0800] [802] [INFO] Booting worker with pid: 802

但是,我可以在通过以下方式运行应用程序时查看我的应用程序(通过访问提供的 IP):

python manage.py runserver

gunicorn --bind 0.0.0.0:8000 todo.wsgi:application

【问题讨论】:

  • 我认为问题不在于您的代码或配置。检查您的实例的安全组(这就是它们在 AWS 中的名称)并允许从端口 80 或您喜欢的任何其他端口进行连接。

标签: django nginx server gunicorn alibaba-cloud-ecs


【解决方案1】:

我猜测您的问题出在 nginx 配置文件上。我附上了以下配置(尽管未经测试),因此您可能希望编辑您的配置,使其看起来更像这样:

具体来说,不要在开头引用http:// 的unix 套接字。将此移动到 upstream app_server 块内的单独 server 声明中。

然后您可以使用proxy_pass 引用http://app_server

http {

    upstream app_server {
        # fail_timeout=0 means we always retry an upstream even if it failed
        # to return a good HTTP response

        # for UNIX domain socket setups
        server unix:/root/todo/todo.sock fail_timeout=0;

        # for a TCP configuration
        # server 127.0.0.1:9000 fail_timeout=0;
    }

    server {
        # if no Host match, close the connection to prevent host spoofing
        listen 80 default_server;
        return 444;
    }

    server {
        listen 80;
        server_name 149.129.136.180;

        location / {
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app;
        } 

        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            # we don't want nginx trying to do something clever with
            # redirects, we set the Host: header above already.
            proxy_redirect off;
            proxy_pass http://app_server;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-13
    • 2019-02-27
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多