nginx+uwsgi完美配置文件,解决“upstream prematurely closed connection”报错

有运维或运维开发方面的需求,可以联系博主QQ 452336092或Email:admin#centos.bz(收费)

这段时间在折腾django,一开始用单一的uwsgi控制web访问,虽然说没有什么大问题,但是很多东西没法配置,比如超时时间,uwsgi虽然有个“harakiri”配置项,但并没有什么作用。

所以终究还是需要接口nginx来做前端代理,但是在代理的出现了一个问题,前端一直没有响应,nginx错误日志(/var/log/nginx/error.log)中出现如下报错

2017/12/21 10:36:48 [error] 9056#0: *18 upstream prematurely closed connection while reading response header from upstream, client: 180.160.72.9, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://127.0.0.1:8000", host: "127.0.0.1:8002"

nginx中access.log返回:

180.160.72.9 - - [21/Dec/2017:10:25:28 +0800] "GET / HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
180.160.72.9 - - [21/Dec/2017:10:25:53 +0800] "GET / HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
180.160.72.9 - - [21/Dec/2017:10:26:55 +0800] "GET / HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
180.160.72.9 - - [21/Dec/2017:10:27:55 +0800] "GET / HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"

找了很多资料后,从v2ex某个评论中看到了某个点,就是代理的地址需要将端口换成sock进程文件,

最终完美的配置文件

新建一个conf配置文件,同时可解决大部分超时问题,内容如下:

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 256k;         #以下四个参数已加大,如果设置太小也会出现timeout 504
fastcgi_buffers 16 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;


server {
        listen  8002;
        server_name localhost;

        location / {
            include     uwsgi_params;
            uwsgi_pass   unix:/home/api/vuln_cn/log/uwsgi.sock;    #这里是关键,指向uwsgi.sock文件,不能用127.0.0.1:8000,否则会出现文中的报错
            uwsgi_param UWSGI_CHDIR  /home/api/vuln_cn;     #这里与uwsgi.ini中配置类似,指向django项目目录
            uwsgi_param UWSGI_SCRIPT vuln_cn.wsgi;     #同上,指向项目名称
            access_log /home/api/vuln_cn/log/access.log;     #定义访问日志
            uwsgi_read_timeout 1800;
            uwsgi_send_timeout 300;
            proxy_read_timeout 300;
            }
    }

以上,备忘

原文出处:vuln -> http://www.vuln.cn/8794

打赏

如果此文对你有所帮助,请随意打赏鼓励作者^_^