Http 运行uwsgi模式会导致nginx错误

Http 运行uwsgi模式会导致nginx错误,http,nginx,http-headers,uwsgi,Http,Nginx,Http Headers,Uwsgi,如果我正常运行uwsgi(非皇帝模式),nginx工作得很好 如果我在帝皇模式下配置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;

如果我正常运行uwsgi(非皇帝模式),nginx工作得很好

如果我在帝皇模式下配置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
proxy_pass          http://127.0.0.1:27070;
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
[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 
myapplication.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
[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——皇帝/opt/uwsgi——附庸包括/opt/uwsgi/vassals-default.ini——主人——按期限死亡

*** 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文件中有这一行:

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
proxy_pass          http://127.0.0.1:27070;
当nginx.conf文件中存在上面的代理传递行时,错误400错误请求:

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
proxy_pass          http://127.0.0.1:27070;

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

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
proxy_pass          http://127.0.0.1:27070;

此外,在获取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/"

尝试在setting.py中将localhost或您的ip/domain添加到允许的\u主机,如下所示

允许的_HOSTS=['localhost','127.0.0.1','your.server.ip.here']

同时删除以下内容:

听127.0.0.1:27070

uwsgi_通行证127.0.0.1:27070

代用通行证

并使nginx使用unix套接字代替ini文件中的套接字


socket=/tmp/sockets/myapplication.sock

几天来,我一直面临着完全相同的问题……我知道这是一个旧线程,但你还记得如何修复它吗?