简体   繁体   English

如何使用unix套接字将URL参数传递给Nginx proxy_pass后面的Flask

[英]How to pass url arguments to Flask behind Nginx proxy_pass with unix socket

I have a Flask app with bjoern as python server. 我有一个将bjoern作为python服务器的Flask应用程序。 An example url I have is something like: 我有一个示例网址,例如:

http://example.com/store/junihh
http://example.com/store/junihh/product-name

Where "junihh" and "product-name" are arguments that I need to pass to python. 其中“ junihh”和“ product-name”是我需要传递给python的参数。

I try to use unix socket after reading about the performance against TCP/IP calls. 在阅读了有关TCP / IP调用的性能后,我尝试使用unix套接字。 But now I get a 502 error on the browser. 但是现在我在浏览器上收到502错误。

This is an snippet of my conf: 这是我的conf的片段:

upstream backend {
    # server localhost:1234;
    # server unix:/run/app_stores.sock weight=10 max_fails=3 fail_timeout=30s;
    server unix:/run/app_stores.sock;
}

server {
    listen                      80 default_server;
    listen                      [::]:80 default_server;
    server_name                 example.com www.example.com; 
    root                        /path/to/my/public;

    location ~ ^/store/(.*)$ {
        include                 /etc/nginx/conf.d/jh-proxy-pass.conf;
        include                 /etc/nginx/conf.d/jh-custom-headers.conf;

        proxy_pass              http://backend/$1;
    }
}

How to pass the url arguments to Flask through Nginx proxy_pass with unix socket? 如何通过带有unix套接字的Nginx proxy_pass将url参数传递给Flask?

Thanks for any help. 谢谢你的帮助。

Here is my conf, it can works. 这是我的conf,它可以工作。 502 is because it cannot find route to the upstream server(ie. change http://127.0.0.1:5000/ $1 to http://localhost:5000/ $1) will cause 502. 502是因为它找不到到上游服务器的路由(即,将http://127.0.0.1:5000/ $ 1更改为http:// localhost:5000 / $ 1)将导致502。

nginx.conf nginx.conf

http {
    server {
        listen       80; 
        server_name  localhost;

        location ~ ^/store/(.*)$ {
            proxy_pass http://127.0.0.1:5000/$1;
        }   
    }   
}

flask app.py 烧瓶应用程序

#!/usr/bin/env python3

from flask import Flask

app = Flask(__name__)

@app.route('/')
def world():
    return 'world'

@app.route('/<name>/<pro>')
def shop(name, pro):
    return 'name: ' + name + ', prod: ' + pro

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

Update 更新

or you can use unix socket like this, but relay on uwsgi . 或者您可以像这样使用unix套接字,但要在uwsgi上进行中继。

nginx.conf nginx.conf

http {
    server {
        listen       80; 

        location /store {
            rewrite /store/(.+) $1 break;
            include uwsgi_params;
            uwsgi_pass unix:/tmp/store.sock;
        }   
    }   
}

flask app.py 烧瓶应用程序

like above, not change 像上面一样,不变

uwsgi config uwsgi配置

[uwsgi]
module=app:app
plugins=python3
master=true
processes=1
socket=/tmp/store.sock

uid=nobody
gid=nobody

vaccum=true
die-on-term=true

save as config.ini , then run uwsgi config.ini 另存为config.ini ,然后运行uwsgi config.ini

after nginx reload, you can visit your page ;-) 重新加载nginx之后,您可以访问您的页面;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM