flask 的网站部署了一天, nginx 依然无法与 uwsgi 通讯,系统 Fedora 23。希望搭建过 Flask 环境的大牛看看。

2016-01-30 22:40:12 +08:00
 fyyz

nginx 的配置(开了 81 端口,方便调试):

[root@localhost ~]# cat /etc/nginx/conf.d/dev.auth.lan.conf 
server
{
    listen          81;
    server_name     dev.auth.lan;
    root            /var/web/dev.auth.lan;

    location /
    {
        include     uwsgi_params;
        uwsgi_pass  127.0.0.1:8000;
    }
}

uwsgi 的配置:

[root@localhost dev.auth.lan]# cat /etc/uwsgi.ini 
[uwsgi]
uid = uwsgi 
gid = uwsgi
pidfile = /run/uwsgi/uwsgi.pid
emperor = /etc/uwsgi.d
stats = /run/uwsgi/stats.sock
emperor-tyrant = true
cap = setgid,setuid
[root@localhost ~]# cat /etc/uwsgi.d/dev.auth.lan.ini 
[uwsgi]

socket = :8000

chdir = /var/web/dev.auth.lan
wsgi-file = manager.py

master = true
process = 10
vacuum = true

daemonize = /var/log/dev.auth.lan.uwsgi.log

nginx 的运行情况(实心圆圈是绿灯):

[root@localhost ~]# systemctl status nginx -l
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 六 2016-01-30 06:03:28 PST; 25min ago
  Process: 936 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 861 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 848 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 991 (nginx)
   CGroup: /system.slice/nginx.service
           ├─991 nginx: master process /usr/sbin/ngin
           └─993 nginx: worker proces

1 月 30 06:03:28 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
1 月 30 06:03:28 localhost.localdomain nginx[861]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
1 月 30 06:03:28 localhost.localdomain nginx[861]: nginx: configuration file /etc/nginx/nginx.conf test is successful
1 月 30 06:03:28 localhost.localdomain systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
1 月 30 06:03:28 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.

uwsgi 的运行情况(也是绿灯):

[root@localhost ~]# systemctl status uwsgi -l
● uwsgi.service - uWSGI Emperor Service
   Loaded: loaded (/usr/lib/systemd/system/uwsgi.service; enabled; vendor preset: disabled)
   Active: active (running) since 六 2016-01-30 06:32:29 PST; 2s ago
  Process: 2277 ExecStartPre=/bin/chown uwsgi:uwsgi /run/uwsgi (code=exited, status=0/SUCCESS)
  Process: 2274 ExecStartPre=/bin/mkdir -p /run/uwsgi (code=exited, status=0/SUCCESS)
 Main PID: 2281 (uwsgi)
   Status: "The Emperor is governing 1 vassals"
   CGroup: /system.slice/uwsgi.service
           ├─2281 /usr/sbin/uwsgi --ini /etc/uwsgi.ini
           ├─2283 /usr/sbin/uwsgi --ini /etc/uwsgi.ini
           ├─2284 /usr/sbin/uwsgi --ini dev.auth.lan.ini
           └─2285 /usr/sbin/uwsgi --ini dev.auth.lan.ini

1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: your server socket listen backlog is limited to 100 connections
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: your mercy for graceful operations on workers is 60 seconds
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: mapped 145536 bytes (142 KB) for 1 cores
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: *** Operational MODE: single process ***
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: *** no app loaded. going in full dynamic mode ***
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: *** uWSGI is running in multiple interpreter mode ***
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: spawned uWSGI master process (pid: 2284)
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: Sat Jan 30 06:32:29 2016 - [emperor] vassal dev.auth.lan.ini has been spawned
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: spawned uWSGI worker 1 (pid: 2285, cores: 1)
1 月 30 06:32:29 localhost.localdomain uwsgi[2281]: Sat Jan 30 06:32:29 2016 - [emperor] vassal dev.auth.lan.ini is ready to accept requests

端口也确实开了:

[root@localhost ~]# netstat -lpn | grep 8000
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      2284/uwsgi

项目目录下也确实有 manager.py

[root@localhost dev.auth.lan]# pwd
/var/web/dev.auth.lan

[root@localhost dev.auth.lan]# ls -alh
总用量 4.0K
drwxr-xr-x. 3 root root  39 1 月  30 06:36 .
drwxr-xr-x. 3 root root  26 1 月  30 02:32 ..
-rwxr-xr-x. 1 root root 132 1 月  30 06:10 manager.py
drwxr-xr-x. 5 root root  82 1 月  30 05:02 project

[root@localhost dev.auth.lan]# cat manager.py
#!/usr/bin/env python3

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "<span style='color:red'>I am app 1</span>"
[root@localhost dev.auth.lan]#

但是用浏览器访问那台服务器的 81 端口,就是报 502 , nginx 的日志(太长,加了换行进去):

2016/01/30 06:37:59 [crit] 993#0: *25 connect() to 127.0.0.1:8000 failed 
(13: Permission denied) 
while connecting to upstream, client: 192.168.124.1, 
server: dev.auth.lan, request: "GET / HTTP/1.1", 
upstream: "uwsgi://127.0.0.1:8000", host: "192.168.124.237:81"
4669 次点击
所在节点    Python
13 条回复
ethego
2016-01-30 22:45:13 +08:00
*** no app loaded. going in full dynamic mode ***
看到这玩意没有?
ethego
2016-01-30 22:45:44 +08:00
wsgi 容器里没装东西
fyyz
2016-01-30 22:55:39 +08:00
@ethego
非常感谢你的回复。
不过,我不是很清楚,你说的这个 wsgi 容器是什么玩意?依照我上面的配置,我应该如何更改呢?
ethego
2016-01-30 22:59:28 +08:00
wsgi-file = manager.py 换成 module = manager 或者 module = manager:app
fyyz
2016-01-31 00:25:41 +08:00
直接回复似乎不支持 markdown ,我写 APPEND 里了。
@ethego
billlee
2016-01-31 00:33:33 +08:00
看到有 connect() to 127.0.0.1:8000 failed (13: Permission denied)
也许是 SELinux 的问题?
squid157
2016-01-31 08:43:14 +08:00
@billlee
我也这么猜。我因为不会配置,都是直接关闭 SELinux
fyyz
2016-01-31 10:06:55 +08:00
非常感谢 @billlee @squid157 这两位,当然还有楼上的 @ethego 。目前网站已经成功跑起来了。
Ansen
2016-01-31 10:57:30 +08:00
uwsgi 你用 http ,不要用 sockt
sockt 不是这样配置的( nginx )
jings
2016-01-31 13:21:46 +08:00
mark
aabbccli
2016-01-31 15:30:40 +08:00
用 SOCK 文件的话, NGINX 应该是这样

upstream first {
server unix:///var/www/site/site.sock;
}

server {
listen 80;
server_name localhost;


location / {

include /etc/nginx/uwsgi_params;
uwsgi_pass first;

}

}
fyyz
2016-02-01 09:07:34 +08:00
@Ansen 为什么不要用 socket ?
Ansen
2016-02-01 09:10:03 +08:00
@fyyz 你 nginx 配的是 http , uwsgi 用的是 sockt

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/254525

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX