nginx和uWSGI给出“504网关超时”

时间:2021-11-14 21:29:37

I am following the tutorial Setting up Django and your web server with uWSGI and nginx.

我正在学习使用uWSGI和nginx设置Django和web服务器的教程。

uWSGI is up and running

I set up uwsgi to serve my Django project with the following line.

我建立了uwsgi来为我的Django项目提供以下内容。

mydjangoproj $ uwsgi --http 0.0.0.0:8002 --module wsgi --harakiri 5

This works when I go there in a browser, to 42.42.42.42:8002.

当我在浏览器中运行到42.42.42:42:8002时,它就可以工作了。

My nginx setup

nginx is running as a daemon, and visiting it's default site, port 80, works.

nginx作为守护进程运行,访问它的默认站点,端口80,可以工作。

I added this as a site to nginx using the following mydjangoproj_nginx.conf file:

我使用下面的mydjangoproj_nginx向nginx添加了这个站点。配置文件:

server {
    listen      8000;
    server_name 42.42.42.42;
    charset     utf-8;
    client_max_body_size 75M;

    location /static {
        alias /home/myuser/mydjangoproj/static; 
    }

    location / {
        uwsgi_pass 127.0.0.1:8002;
        include     /home/myuser/mydjangoproj/uwsgi_params;
    }
}

I use the unmodified version of uwsgi_params, from the tutorial:

我使用uwsgi_params的未修改版本,来自教程:

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

It does serve the static files perfectly.

它确实完美地服务于静态文件。

Error

If I visit 42.42.42.42:8000 it hangs for a long time, until the nginx timeout I guess, and I get 504 Gateway Time-out.

如果我访问42.42.42.42:8000,它会挂很长时间,直到nginx超时,我猜是504网关超时。

uWSGI writes nothing in the shell. If visiting directly in browser, it does write about receiving a request.

uWSGI不会在shell中写入任何内容。如果直接在浏览器中访问,它会写关于接收请求的内容。

The nginx error log writes, only after the timeout:

nginx错误日志只在超时后才写入:

2014/12/11 05:31:12 [error] 28895#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 66.66.66.66, server: 42.42.42.42, request: "GET / HTTP/1.1", upstream: "uwsgi://42.42.42.42:8002", host: "42.42.42.42:8000"

If I close the uWSGI, which is just run from a shell, I instantly get a 502 Bad Gateway.

如果我关闭uWSGI(从shell中运行),我将立即得到一个502坏网关。

When searching online, people just recommend setting the uWSGI timeout lower than the nginx timeout, that's why I run uWSGI with --harakiri 5.

在网上搜索时,人们只建议将uWSGI超时设置为小于nginx超时,这就是我使用harakiri 5运行uWSGI的原因。

So, what is my problem here?

我的问题是什么?

1 个解决方案

#1


22  

I think you are running uwsgi in http mode --http 0.0.0.0:8002 but you have configured nginx as wsgi proxy change your uwsgi script as:

我认为您正在以http模式运行uwsgi——http 0.0.0.0.0.0:8002,但是您已经将nginx配置为wsgi代理,将您的uwsgi脚本更改为:

 uwsgi --socket :8002 --module wsgi --harakiri 5

Note that if you are running nginx and uwsgi on the same machine is better to use unix sockets

注意,如果在同一台机器上运行nginx和uwsgi,最好使用unix套接字

#1


22  

I think you are running uwsgi in http mode --http 0.0.0.0:8002 but you have configured nginx as wsgi proxy change your uwsgi script as:

我认为您正在以http模式运行uwsgi——http 0.0.0.0.0.0:8002,但是您已经将nginx配置为wsgi代理,将您的uwsgi脚本更改为:

 uwsgi --socket :8002 --module wsgi --harakiri 5

Note that if you are running nginx and uwsgi on the same machine is better to use unix sockets

注意,如果在同一台机器上运行nginx和uwsgi,最好使用unix套接字