运行uwsgi emperor模式会给出nginx错误

时间:2015-07-16 18:22:25

标签: http nginx http-headers uwsgi

如果我正常运行uwsgi(非皇帝模式),nginx可以正常运行。

如果我在emperor模式下配置uwsgi,我会收到各种我无法解决的错误。

这是我的nginx.conf文件:

worker_processes  1;
worker_rlimit_nofile 8192;

events {
    worker_connections  8192;
    #multi_accept on;      # does not matter if I use multi-accept
    use epoll;             #   or epoll, same errors
}

http {
    large_client_header_buffers  2  1k;       
    client_body_buffer_size     10k;           
    client_header_buffer_size    1k;           
    client_max_body_size         8m;           
    request_pool_size           32k;
    connection_pool_size        256;
    ignore_invalid_headers       on;

        sendfile        on;
    tcp_nopush      on;
    server_names_hash_bucket_size 128;
    keepalive_timeout  90;
    gzip               on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;

    # Configuration for Nginx
    server  {
        # Running port
        listen 80;
        listen 127.0.0.1:27070;              
        server_name 127.0.0.1 myserver_name.com;
        charset utf-8;
        access_log  /log/nginx_access.log;
        error_log  /log/nginx_error.log;   

        # Proxying connections to application servers
        location /myapplication/ {
             include             uwsgi_params;
             uwsgi_buffering     off;
             uwsgi_param         SCRIPT_NAME /myapplication;
             uwsgi_modifier1     30;
             uwsgi_pass          127.0.0.1:27070;  
             proxy_pass          http://127.0.0.1:27070;
             proxy_redirect      off;
             proxy_set_header    Host $host;
             proxy_set_header    X-Real-IP $remote_addr;
             proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header    X-Forwarded-host $server_name;
        }   # end mysite
   }   # end server
}   # end http

vassals-default.ini文件:

[uwsgi]
log-date = true
emperor=/etc/uwsgi
emperor-use-clone = fs,net,ipc,pid,uts
emperor-tyrant = true
vassals-include = /opt/uwsgi/vassals-default.ini
master = true
cap = setgid, setuid
vassal-set = processes=8
vassal-set = enable-metrics=1
vassal_name = %n
socket = /tmp/sockets/%n.sock
chmod-socket = 770
logto = /opt/uwsgi/%n.log

myapplication.ini文件:

[uwsgi]
domain = myserver_name.com
app-name = myapplication
uid = myapplication
gid = myapplication
socket=/tmp/sockets/myapplication.sock
chdir = /opt/myapplication/myapp
virtualenv = /opt/myapplication/webapp-20150203/
module = WSGI:app
callable = app
processes = 2
buffer-size=32768
http-buffer-size=32768
daemonize = /opt/uwsgi/myapplication.log
pidfile = /opt/uwsgi/myapplication.pid
logto = /opt/uwsgi/uwsgi.log
debug = true
disable-logging = false
thunder-lock = true
enable-threads = true
harakiri = 30 

套接字文件:

[root@npi uwsgi]# ls -l /tmp/sockets
srwsrws--- 1 sadlesum sadlesum 0 Jul 16 13:48 myapplication.sock
srwxrwx--- 1 root     root     0 Jul 16 13:48 vassals-default.sock

日志文件看起来不错:

myapplication.log:

Thu Jul 16 13:48:25 2015 - *** starting uWSGI sub-Emperor ***
Thu Jul 16 13:48:25 2015 - announcing my loyalty to the Emperor...
Thu Jul 16 13:48:25 2015 - uwsgi socket 0 bound to UNIX address /tmp/sockets/myapplication.sock fd 3
Thu Jul 16 13:48:25 2015 - uwsgi socket 1 bound to UNIX address /tmp/sockets/vassals-default.sock fd 4
Thu Jul 16 13:48:25 2015 - Python version: 2.7.6 (default, Jan 11 2014, 21:49:01)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
Thu Jul 16 13:48:25 2015 - Set PythonHome to /opt/myapplication/webapp-20150203/
Thu Jul 16 13:48:25 2015 - Python main interpreter initialized at 0x10f7470
Thu Jul 16 13:48:25 2015 - python threads support enabled
Thu Jul 16 13:48:25 2015 - your server socket listen backlog is limited to 100 connections
Thu Jul 16 13:48:25 2015 - your mercy for graceful operations on workers is 60 seconds
Thu Jul 16 13:48:25 2015 - mapped 304320 bytes (297 KB) for 2 cores
Thu Jul 16 13:48:25 2015 - *** Operational MODE: preforking ***
Thu Jul 16 13:48:29 2015 - WSGI app 0 (mountpoint='') ready in 4 seconds on interpreter 0x10f7470 pid: 26688 (default app)
Thu Jul 16 13:48:29 2015 - *** uWSGI is running in multiple interpreter mode ***
Thu Jul 16 13:48:29 2015 - spawned uWSGI master process (pid: 26688)
Thu Jul 16 13:48:29 2015 - spawned uWSGI worker 1 (pid: 26703, cores: 1)
Thu Jul 16 13:48:29 2015 - spawned uWSGI worker 2 (pid: 26704, cores: 1)

以皇帝模式启动uwsgi:

uwsgi --emperor / opt / uwsgi --vassals-include /opt/uwsgi/vassals-default.ini --master --die-on-term

*** starting uWSGI Emperor ***
Python version: 2.7.6 (default, Jan 11 2014, 21:49:01)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
*** has_emperor mode detected (fd: 6) ***
[uWSGI] getting INI configuration from saddlesum.ini
[uWSGI] getting INI configuration from /opt/uwsgi/vassals-default.ini
*** has_emperor mode detected (fd: 7) ***
[uWSGI] getting INI configuration from vassals-default.ini
[uWSGI] getting INI configuration from /opt/uwsgi/vassals-default.ini
Thu Jul 16 13:48:25 2015 - [emperor] vassal myapplication.ini is now loyal
Thu Jul 16 13:48:25 2015 - [emperor] vassal vassals-default.ini is now loyal
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xb16870
your mercy for graceful operations on workers is 60 seconds
*** Operational MODE: no-workers ***
spawned uWSGI master process (pid: 26686)
Thu Jul 16 13:48:25 2015 - [emperor] vassal vassals-default.ini has been spawned
Thu Jul 16 13:48:25 2015 - [emperor] vassal vassals-default.ini is ready to accept requests
Thu Jul 16 13:48:29 2015 - [emperor] vassal myapplication.ini has been spawned
Thu Jul 16 13:48:29 2015 - [emperor] vassal myapplication.ini is ready to accept requests

当我尝试在firefox(或Chrome)中访问我的webapp时,出现以下错误:

如果我在我的nginx.conf文件中有这一行:

proxy_pass          http://127.0.0.1:27070;

在nginx.conf文件中出现上面的proxy_pass行时出现错误400错误请求:

Error 400 bad request

在nginx.conf文件中注释掉上面的proxy_pass行时出现错误502错误的网关:

502 bad gateway result

此外,当遇到502错误网关错误时,我也会在我的nginx_error.log文件中看到这一点:

2015/07/16 14:10:18 [error] 28289#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /myapplication/ HTTP/1.1", upstream: "uwsgi://127.0.0.1:27070", host: "myserver_name.com", referrer: "http://myserver_name.com/"

1 个答案:

答案 0 :(得分:0)

尝试将localhost或ip / domain添加到setting.py中的ALLOWED_HOSTS,如下所示

ALLOWED_HOSTS = ['localhost','127.0.0.1','your.server.ip.here']

也删除这些:

听127.0.0.1:27070;

uwsgi_pass 127.0.0.1:27070;

proxy_pass http://127.0.0.1:27070;

并使nginx使用unix socket代替ini文件

插座= / TMP /插座/ myapplication.sock