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

时间:2013-06-09 16:15:08

标签: nginx flask uwsgi

我是配置服务器的新手。我想配置我的Amazon-EC2实例。我根据这个文件配置了它。 http://www.soundrenalin.com/about

但是,当我点击网址时,遇到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"

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

2 个答案:

答案 0 :(得分:1)

它可能是以下之一:

  1. 您的UWSGI套接字不在/tmp/uwsgi.sock
  2. uwsgi或www-data用户无权在/ tmp目录中创建uwsgi.sock。
  3. 如果您在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
    

    你的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
    

    要启用,您需要符号链接并重新启动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
    

答案 1 :(得分:-2)

来自https://stackoverflow.com/a/33587478/260127

您可以使用ubutu的upstart和uwsgi的--emperor选项轻松执行python uwsgi应用程序。