nginx - 将 Nginx 服务器配置为 python flask 应用程序

标签 nginx flask uwsgi

我是配置服务器的新手。我想配置我的 Amazon-EC2 实例。我是按照这个文档配置的。 http://www.soundrenalin.com/about

但是当我点击 url 时,遇到了 502 Bad Gateway 错误。 我的项目位于这个路径:/home/ubuntu/dsn/app
/home/ubuntu/dsn文件夹树是:

app/
    app.py
    static/
    templates/
    themes/
bin/
build/
include/
lib/
local/
run.py


这是我的 nginx 配置(/etc/nginx/sites-available/default):

server {
        listen   80; 

        root /home/ubuntu/dsn/app
        index index.html index.htm;

        server_name localhost;
        location / { try_files $uri @app; }
        location @app {
             include uwsgi_params;
             uwsgi_pass unix:/tmp/uwsgi.sock;
        }
}

这是我的 uwsgi.ini 文件:

[uwsgi]
     chdir = /home/ubuntu/dsn/
     uid = www-data
     gid = www-data
     chmod-socket = 666
     socket = /tmp/uwsgi.sock
     module = run
     virtualenv = /home/ubuntu/dsn/   

另一件事是:
当我运行命令 tail -f/var/log/nginx/error.log 结果是:

2013/06/09 15:58:11 [error] 5321#0: *1 connect() to unix:/tmp/uwsgi.sock failed (111: Connection refused) while connecting to upstream, client: <myip>, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "54.218.14.213"

我该如何解决这个问题?谢谢。

最佳答案

可能是以下情况之一:

  1. 你的 UWSGI socket 不在/tmp/uwsgi.sock
  2. uwsgi 或 www-data 用户没有在您的/tmp 目录中创建 uwsgi.sock 的权限。

如果你在 Ubuntu 12.04 上安装了 Uwsgi,配置文件应该位于:

/usr/share/uwsgi/conf/default.ini

这是该文件中的默认套接字配置:

# bind to UNIX socket at /run/uwsgi/<confnamespace>/<confname>/socket
socket = /run/uwsgi/%(deb-confnamespace)/%(deb-confname)/socket

您应该创建一个与我的类似的特定于应用程序的配置文件。我可以确认它在虚拟环境中工作。

sudo vim /etc/uwsgi/apps-available/your-app.ini

your-app.ini 的内容:

[uwsgi]
base = /home/nick/your-app.com

#location of the flask application file
file = /home/nick/your-app.com/main.py

#uwsgi varible only, does not relate to your flask application
callable = app

#uwsgi plugins
plugins = http,python

#to create within a virtual environment
home = %(base)/venv

pythonpath = %(base)
socket = /tmp/%n.sock
logto = /var/log/uwsgi/%n.log
workers = 3

要启用,您需要符号链接(symbolic link)并重新启动 nginx。

#make sure that any user to write to the /tmp directory
sudo chmod 777 /tmp
sudo ln -s /etc/uwsgi/apps-available/your-app.ini /etc/uwsgi/apps-enabled/your-app.ini
sudo service uwsgi restart

关于nginx - 将 Nginx 服务器配置为 python flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17011577/

相关文章:

nginx - Nginx.conf可以访问环境变量吗?

javascript - 将 Flask 中的变量显示为 javascript 代码时出错

uWSGI: "you have enabled harakiri without post buffering"的修复是什么?

在写入缓冲区之前计算 C 字符串的大小

PhpStorm 和 Remote XDebug 不工作

mysql - Node.js 服务器在某些请求后不返回响应

postgresql - nextval(seq_name) 未从数据库获取正确的值

python - 如何在 Flask (Python) 中更新具有选择选项的表单

flask - 不使用 sudo 启动 uWSGI 服务器

python - 如何构建支持 SSL 的 uWSGI 以使用 websocket 握手 API 功能?