【问题标题】:gunicorn, nginx, and using port 80 for running a django web applicationgunicorn,nginx,并使用端口 80 运行 django web 应用程序
【发布时间】:2015-01-07 15:00:24
【问题描述】:

我在 Web 服务器上安装了 django、nginx 和 gunicorn。

Nginx 监听 80 端口 Gunicorn 在 8000 端口上运行 django 项目

这很好用。如果我去 www.mysite.com:8000/myapp/ django 应用程序就可以正常运行。但是如果我想让用户去 www.mysite.com/myapp/ 查看 django 应用程序呢?我不认为摆脱 Nginx 是答案,我希望我错过了一些我可以应用的配置调整来完成这项工作。

感谢任何建议。

【问题讨论】:

    标签: django nginx port gunicorn


    【解决方案1】:

    你可以使用如下配置,这样你就可以在80端口正常访问你的网站了:

    这是你的 nginx 配置文件,sudo vim /etc/nginx/sites-available/django

    upstream app_server {
            server 127.0.0.1:9000 fail_timeout=0;
        }
        server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;
            root /usr/share/nginx/html;
            index index.html index.htm;
            client_max_body_size 250M;
            server_name _;
            keepalive_timeout 15;
    
    # Your Django project's media files - amend as required
            location /media  {
                alias /home/xxx/yourdjangoproject/media;
            }
            # your Django project's static files - amend as required
            location /static {
                alias /home/xxx/yourdjangoproject/static;
            }
            location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_pass http://app_server;
            }
        }
    

    并将 gunicorn 配置为

    description "Gunicorn daemon for Django project"
    start on (local-filesystems and net-device-up IFACE=eth0)
    stop on runlevel [!12345]
    # If the process quits unexpectadly trigger a respawn
    respawn
    setuid yourdjangousernameonlinux
    setgid yourdjangousernameonlinux
    chdir /home/xxx/yourdjangoproject
    
    exec gunicorn \
        --name=yourdjangoproject \
        --pythonpath=yourdjangoproject \
        --bind=0.0.0.0:9000 \
        --config /etc/gunicorn.d/gunicorn.py \
        yourdjangoproject.wsgi:application
    

    【讨论】:

      【解决方案2】:

      不,摆脱 nginx 绝对不是答案。答案是按照very nice documentation配置nginx作为gunicorn的反向代理。

      【讨论】:

      • 非常感谢。我一直在关注其他教程,我完全跳过了 gunicorn 自己的教程。效果很好。
      猜你喜欢
      • 1970-01-01
      • 2019-05-27
      • 2015-05-17
      • 2015-11-24
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多