Nginx

502 壞網關 nginx。uwsgi,燒瓶

  • April 1, 2015

好的,所以我用 python 開發了一個 web 應用程序,它與內部伺服器一起工作:

if __name__ == '__main__':
   app.app.run(debug = True)

完全沒有問題。我現在正在嘗試使用執行在 arch linux 上的 uwsgi 啟動 nginx 伺服器,這裡是配置文件:

我的應用程序的 config.py:

CSRF_ENABLED = True
SECRET_KEY = "you'll-never-know"
PROPAGATE_EXCEPTIONS = True

import os
basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')

/etc/nginx/nginx.conf:

user http;
worker_processes  2;

events {
   #worker_connections  1024;
   worker_connections 2048;
}

http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  70;

   gzip  on;
   gzip_min_length  1100;
   gzip_buffers  4 32k;
   gzip_types    text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
   gzip_vary on;
   gzip_comp_level 6;

   include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-enabled/test.conf

upstream test-server {
   server unix:/run/uwsgi/test.sock;
   #server 127.0.0.1:3031
}

server {
   listen 80;
   server_name "";
   return 444;
}

server {
   server_name 192.xxx.x.xxx;
   listen 80;

   root /srv/http/test-dir/src/app;

   location /static {
       alias /srv/http/test-dir/src/app/static;
   }

   location / {
       include /etc/nginx/uwsgi_params;
       uwsgi_pass test-server;
   }

#    rewrite ^ https://$server_name$request_uri? permanent;
}

/etc/uwsgi/emperor.ini

[uwsgi]
emperor = /etc/uwsgi/vassals
#master = true
#plugins = python2
uid = http
gid = http

/etc/uwsgi/vassals

[uwsgi]
chdir = /srv/http/test-dir/src
wsgi-file = run.py
callable = app
processes = 4
threads = 2
offload-threads = 2
stats =  127.0.0.1:9191
max-requests = 5000
enable-threads = true
#master = true
vacuum = true
#socket = 127.0.0.1:3031
socket = /run/uwsgi/test.sock
chmod-socket = 664
harakiri = 60
logto = /var/log/uwsgi/test.log

/var/log/uwsgi/test.log

*** Starting uWSGI 2.0.9 (64bit) on [Wed Mar 25 17:17:53 2015] ***
compiled with version: 4.9.2 20150204 (prerelease) on 28 February 2015 13:34:10
os: Linux-3.19.2-1-ARCH #1 SMP PREEMPT Wed Mar 18 16:21:02 CET 2015
nodename: MyServer
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /etc/uwsgi/vassals
detected binary path: /usr/bin/uwsgi
chdir() to /srv/http/test-dir/src
your processes number limit is 13370
your memory page size is 4096 bytes
*** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers *** 
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /run/uwsgi/test.sock fd 3
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 415360 bytes (405 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 1349)
spawned uWSGI worker 1 (pid: 1350, cores: 2)
spawned uWSGI worker 2 (pid: 1351, cores: 2)
spawned 2 offload threads for uWSGI worker 1
spawned uWSGI worker 3 (pid: 1355, cores: 2)
spawned uWSGI worker 4 (pid: 1356, cores: 2)
*** Stats server enabled on 127.0.0.1:9191 fd: 16 ***
spawned 2 offload threads for uWSGI worker 3
spawned 2 offload threads for uWSGI worker 4
spawned 2 offload threads for uWSGI worker 2
-- unavailable modifier requested: 0 --
announcing my loyalty to the Emperor...
-- unavailable modifier requested: 0 --
announcing my loyalty to the Emperor...

如前所述,當我連接到伺服器內部地址時,我生成了 502 錯誤,這是來自 nginx 錯誤日誌:

2015/03/25 17:21:07 [error] 1340#0: *3 upstream prematurely closed connection while reading response header from upstream, client: [me], server: 192.xxx.x.xxx, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/run/uwsgi/test.sock:", host: "192.xxx.x.xxx"

http 擁有套接字,所以: ls -ld /run/uwsgi/test.sock

srw-rw-r-- 1 http http 0 Mar 25 17:17 /run/uwsgi/test.sock

我想我已經安裝了 uwsgi 端的所有東西:sudo pacman -Ss uwsgi

community/mod_proxy_uwsgi 2.0.9-3
   Apache uWSGI proxy module
community/uwsgi 2.0.9-3 [installed]
   A fast, self-healing and developer/sysadmin-friendly application container server coded in pure C
community/uwsgi-plugin-cgi 2.0.9-3
   CGI plugin
community/uwsgi-plugin-jvm 2.0.9-3
   Plugin for Jvm support
community/uwsgi-plugin-lua51 2.0.9-3
   Plugin for Lua support
community/uwsgi-plugin-mono 2.0.9-3
   Plugin for mono support
community/uwsgi-plugin-php 2.0.9-3
   Plugin for PHP support
community/uwsgi-plugin-psgi 2.0.9-3
   Perl psgi plugin
community/uwsgi-plugin-pypy 2.0.9-3
   Plugin for PyPy support
community/uwsgi-plugin-python 2.0.9-3
   Plugin for Python support
community/uwsgi-plugin-python2 2.0.9-3 [installed]
   Plugin for Python2 support
community/uwsgi-plugin-rack 2.0.9-3
   Ruby rack plugin
community/uwsgi-plugin-webdav 2.0.9-3
   Plugin for webdav support

plugins = python2啟用了

$$ yes this is python2.7 $$在主要的 Emperor.ini 中,但它什麼也沒做,要麼是不需要,要麼是在錯誤的位置。我在這里和每個包站點都閱讀了其他一些東西,雖然不太確定我缺少什麼,但它確實是一些東西。謝謝。 2015 年 3 月 26 日 編輯

我刪除了我的 uwsgi 發行版並通過 pip 安裝它,似乎表現更好。

我開始只是使用命令行配置: sudo uwsgi –http 192.xxx.x.xxx:80 –wsgi-file run.py -callable app –enable-threads –chdir /srv/http/test-dir/ src –processes 4 –threads 2 –offload-threads 2 –vacuum –harakiri 60

*** Starting uWSGI 2.0.10 (64bit) on [Thu Mar 26 21:16:33 2015] ***
compiled with version: 4.9.2 20150304 (prerelease) on 26 March 2015 20:52:38
os: Linux-3.19.2-1-ARCH #1 SMP PREEMPT Wed Mar 18 16:21:02 CET 2015
nodename: MyServer
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /srv/http/test-dir/src
detected binary path: /usr/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /srv/http/test-dir/src
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 13370
your memory page size is 4096 bytes
*** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers ***
detected max file descriptor number: 16384
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on 192.168.1.104:80 fd 4
spawned uWSGI http 1 (pid: 4530)
uwsgi socket 0 bound to TCP address 127.0.0.1:34517 (port auto-assigned) fd 3
Python version: 2.7.9 (default, Dec 11 2014, 04:42:00)  [GCC 4.9.2]
Python main interpreter initialized at 0x120aac0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 332288 bytes (324 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x120aac0 pid: 4529                                                                                                                                                              (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (pid: 4529, cores: 2)
spawned uWSGI worker 2 (pid: 4533, cores: 2)
spawned 2 offload threads for uWSGI worker 2
spawned uWSGI worker 3 (pid: 4536, cores: 2)
spawned uWSGI worker 4 (pid: 4537, cores: 2)
spawned 2 offload threads for uWSGI worker 1
spawned 2 offload threads for uWSGI worker 4
spawned 2 offload threads for uWSGI worker 3

我收到了這個錯誤:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) 在一個執行緒中創建的 SQLite 對像只能在同一個執行緒中使用。該對像是線上程 id 140554959951616 中創建的,這是執行緒 id 140555111335808

刪除 –enable-threads 什麼也不做。使用者將主要在網頁互動時將對象載入和解除安裝到燒瓶會話對像上,這可能會導致此錯誤,不會一直發生,我該怎麼做才能解決這個問題?

我認為有兩件事有助於PROPAGATE_EXCEPTIONS = Trueconfig.py中解決這個問題,我從我的 vassal ini 文件中刪除了執行緒,最後的 uwsgi 文件看起來像這樣:

/etc/uwsgi/emperor.ini:

[uwsgi]
emperor = /etc/uwsgi/vassals
master = true
plugins = python2
uid = http
gid = http

/etc/uwsgi/vassals/test.ini:

[uwsgi]
chdir = /srv/http/test_dir/src
wsgi-file = run.py
callable = app
processes = 4
stats =  127.0.0.1:9191
max-requests = 5000
enable-threads = true
vacuum = true
thunder-lock = true
socket = /run/uwsgi/test-sock.sock
chmod-socket = 664
harakiri = 60
logto = /var/log/uwsgi/test.log

不確定PROPAGATE_EXCEPTIONS = True但刪除了 test.ini 中的執行緒選項並確保在 Emperor.ini 中有一個主選項似乎已經解決了 sql 被扔到不同的踏板上的問題,或者至少它抱怨它並導致崩潰選址,要麼。

引用自:https://serverfault.com/questions/678327