配置Nginx服务器到python烧瓶应用程序 [英] Configuring Nginx server to python flask application

查看:301
本文介绍了配置Nginx服务器到python烧瓶应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  app / 
app.py
static /
模板/
主题/
bin /
建立/
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;
位置/ {try_files $ uri @app; }
location @app {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi.sock;




$ b $ p $这里是我的 uwsgi .ini file:

  [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连接()到unix:/tmp/uwsgi.sock失败(111:连接被拒绝)while连接到上游,客户端:< myip>,服务器:localhost,请求:GET / HTTP / 1.1,上游:uwsgi:// unix:/tmp/uwsgi.sock:,主机:54.218.14.213 

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

解决方案

可能是以下其中一种情况:


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

    $ b $ p如果你在Ubuntu 12.04上安装了Uwsgi,那么这个配置文件应该位于:


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

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

     #绑定到UNIX套接字/ run / uwsgi / < confnamespace> /< confname> / socket 
    socket = / run / uwsgi /%(deb-confnamespace)/%(deb-confname)/ socket
    pre>

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

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

    your-app.ini的内容:

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

    #烧瓶应用程序文件的位置
    file = /home/nick/your-app.com/main.py

    #uwsgi varible only,不涉及你的烧瓶应用程序
    callable = app

    #uwsgi插件
    plugins = http,python

    #在虚拟环境中创建
    home =%(base)/ venv

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

    code>#确保任何用户写入/ tmp目录
    sudo chmod 777 / tmp
    sudo ln -s /etc/uwsgi/apps-available/your-app.ini / etc / uwsgi / apps-enabled / your-app.ini
    sudo服务uwsgi重启


    i am new to configuring server. And i want to configure my Amazon-EC2 instance. I configured it according to this document. http://www.soundrenalin.com/about

    However when i click the url, 502 Bad Gateway error is being encountered. My project is located at this path: /home/ubuntu/dsn/app.
    And the /home/ubuntu/dsn folder tree is:

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


    Here is my nginx config (/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;
            }
    }
    

    And here is my uwsgi.ini file:

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

    And the other thing is:
    When i run the command tail -f /var/log/nginx/error.log result is:

    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"
    

    How can i solve this? Thanks.

    解决方案

    It could be one of the following things:

    1. Your UWSGI socket is not located at /tmp/uwsgi.sock
    2. The uwsgi or www-data user does not have permissions to create uwsgi.sock in your /tmp directory.

    If you installed Uwsgi on Ubuntu 12.04, the config file should be located at:

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

    Here's the default socket config in that file:

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

    You should create an app-specific config file that looks similar to mine. I can confirm it works inside a virtual environment.

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

    The contents of 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
    

    To enable, you need to symlink and restart 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服务器到python烧瓶应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆