简体   繁体   English

django-gunicorn-nginx:502坏网关

[英]django-gunicorn-nginx: 502 bad gateway

I'm trying to ship my web application to the server and this is my first time configuring the server. 我正在尝试将我的Web应用程序发送到服务器,这是我第一次配置服务器。 I'm using django-gunicorn-nginx setup by following this tutorial http://ijcdigital.com/blog/django-gunicorn-and-nginx-setup/ First everything was perfect and I got the django welcome page . 我正在使用django-gunicorn-nginx设置,遵循本教程http://ijcdigital.com/blog/django-gunicorn-and-nginx-setup/首先,一切都很完美,我得到了django欢迎页面 Then I loaded the apps in the django project and setup the static root and Now I'm getting 502 bad gateway You can check out in http://qlimp.com 然后我在django项目中加载应用程序并设置静态根目录,现在我得到502坏网关你可以在http://qlimp.com查看

Everything upto the gunicorn and supervisor setup is the same as shown in that tutorial. gunicorn和supervisor设置的所有内容与该教程中显示的相同。 But I modified some nginx conf. 但我修改了一些nginx conf。 Here it is: 这里是:

upstream app_server_djangoapp {
    server localhost:8001 fail_timeout=0;
}

server {
    listen 80;
    server_name qlimp.com;

    access_log  /var/log/nginx/guni-access.log;
    error_log  /var/log/nginx/guni-error.log info;

    keepalive_timeout 5;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://app_server_djangoapp;
            break;
        }
    }
    location /files/ {
        autoindex on;
        root /home/nirmal/qlimp/qlimp/files/;
    }
}

Here is my Media url: 这是我的媒体网址:

MEDIA_URL = '/files/'

Files is the folder where I'm having all the static files. 文件是我拥有所有静态文件的文件夹。 How can I get my project to work in the server? 如何让我的项目在服务器中工作? Could anyone guide me? 谁能指导我?

UPDATE UPDATE

Errors.log https://gist.github.com/2768425 Errors.log https://gist.github.com/2768425

Thanks! 谢谢!

First. 第一。 Don't use if in an nginx conf. 不要在nginx conf中使用if It's bad. 这不好。 Like really, really horrible. 就像真的一样,非常可怕。 Use the following instead: 请改用以下内容:

location / {
    try_files $uri @proxy;
}

location @proxy {
    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_djangoapp;
}

See: http://wiki.nginx.org/IfIsEvil and http://wiki.nginx.org/Pitfalls 请参阅: http//wiki.nginx.org/IfIsEvilhttp://wiki.nginx.org/Pitfalls

Now, as far as debugging goes. 现在,就调试而言。 Your gunicorn workers are booting because there's some fatal error. 你的gunicorn工作人员正在启动,因为有一些致命的错误。 Try shutting down gunicorn. 尝试关闭gunicorn。 If you're using supervisor: 如果您正在使用主管:

sudo supervisorctl stop [gunicorn process name]

Then, from your project root run: 然后,从您的项目根运行:

python manage.py run_gunicorn -c path/to/gunicorn.conf

Note any startup errors or if it actually boots, test your site in the browser. 请注意任何启动错误或实际启动时,请在浏览器中测试您的站点。 If you're still not getting any meaningful info try just running the standard runserver 如果您仍然没有获得任何有意义的信息,请尝试运行标准的runserver

python manage.py runserver

Again, note any errors and if it loads fine, test your site in the browser. 再次注意任何错误,如果加载正常,请在浏览器中测试您的网站。 I suggest testing on localhost:8000 like you would in development. 我建议像在开发中那样在localhost:8000上进行测试。 One of these should give you something to work with. 其中一个应该给你一些工作。

UPDATE UPDATE

The error you're getting says it can't connect to "ind=127.0.0.1". 你得到的错误说它无法连接到“ind = 127.0.0.1”。 Then, looking at the command you're running, gunicorn_django -bind=127.0.0.1:8001 , it's easy to see the problem. 然后,查看您正在运行的命令gunicorn_django -bind=127.0.0.1:8001 ,很容易看到问题所在。 You can specify the IP and port to bind to with either -b or --bind . 您可以使用-b--bind指定要绑定的IP和端口。 Since you only used one - it's interpreting the IP as ind=127.0.0.1 , which is obviously not correct. 由于您只使用了一个-它将IP解释为ind=127.0.0.1 ,这显然是不正确的。 You need to use: 你需要使用:

gunicorn_django --bind=127.0.0.1:8001

Or 要么

gunicorn_django -b 127.0.0.1:8001

You need to understand directives properly. 您需要正确理解指令。 "server_name" directive holds the IP address and "proxy_pass" will connect to port where your server is hosted. “server_name”指令保存IP地址,“proxy_pass”将连接到托管服务器的端口。 In your case: 在你的情况下:

server_name 127.0.0.1;
proxy_pass http://127.0.0.1:8001;

There is no reason for this not to work but still if it does not then try "python manage.py runserver" command to make sure that your site runs with no error because in case site is not able to present data to wsgi.py that likely to show same error. 这没有理由不工作但仍然如果它没有尝试“python manage.py runserver”命令以确保您的站点运行没有错误,因为如果站点无法向wsgi.py提供数据可能会显示相同的错误。

Increase the keepalive_timeout. 增加keepalive_timeout。

server {
        listen 5000 default deferred;
        client_max_body_size 4G;
        keepalive_timeout 5;
        server_name _;
        location / {
            proxy_read_timeout 800;
            proxy_pass  http://localhost:9000;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            break;
        }
}

Try using a worker-class like gevent. 尝试使用像gevent这样的工人级 I had some trouble using gevent in python 3.7, better to use python 3.6. 我在python 3.7中使用gevent时遇到了一些麻烦,最好使用python 3.6。

Django, Python 3.6 example: Django,Python 3.6示例:

pip install gevent
gunicorn my_app.wsgi --workers 2 --worker-class=gevent --bind 0.0.0.0:80 --timeout=90 --graceful-timeout=10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM