简体   繁体   English

为 Python Flask 应用程序创建代理

[英]Create Proxy for Python Flask Application

we have an internal application, the backend was written in Java and the front end was written in HTML, Javascript..etc.我们有一个内部应用程序,后端是用 Java 编写的,前端是用 HTML、Javascript.. 等编写的。 The app was hosted using nginx server .该应用程序是使用nginx 服务器托管的。 However, I have another web application was written in Python flask where the boss want to integrate those two tools together.但是,我有另一个 Web 应用程序是用 Python flask 编写的,老板想将这两个工具集成在一起。

say for example, the initial project(Java solution) has the domain比如说,初始项目(Java 解决方案)有域

inv.datafireball.com 

And the admin showed me that he can change the configuration file of nginx in that way, a route can be mapped/proxy to the second app running on a different server管理员向我展示了他可以通过这种方式更改 nginx 的配置文件,可以将路由映射/代理到在不同服务器上运行的第二个应用程序

inv.datafireball.com/competitor -> datafireball.com:5000

However, based on my research, all the paths in the Python application need to be re-coded in a way: /static/js/d3.js need to be changed to /competitor/static/js/d3.js ...但是,根据我的研究,Python 应用程序中的所有路径都需要以某种方式重新编码: /static/js/d3.js需要更改为/competitor/static/js/d3.js ...

We harded coded a few paths it looked very promising, however, our Python application is pretty big and it is really a big mass after to manually change all the path.我们硬编码了一些看起来很有希望的路径,但是,我们的 Python 应用程序非常大,手动更改所有路径后确实是一个很大的质量。

Can anyone give me a guidance if there is an easy way to map/proxy a Python flask application to an existing application written in Java with changing the existing Python code?如果有一种简单的方法可以通过更改现有的 Python 代码将 Python Flask 应用程序映射/代理到用 Java 编写的现有应用程序,谁能给我一个指导?

Here's a solution that requires miminal change to the Flask application and works pretty well:这是一个需要对 Flask 应用程序进行最小更改并且运行良好的解决方案:

Here's what it does essentially: We tell Nginx to pass some special headers to our proxy flask application and then we create a wrapper (ReverseProxied) that intercepts each request, extracts the headers set by Nginx and modifies the request processing environment so that it matches the urls we want.它的本质是这样的:我们告诉 Nginx 将一些特殊的标头传递给我们的代理烧瓶应用程序,然后我们创建一个包装器(ReverseProxied)来拦截每个请求,提取 Nginx 设置的标头并修改请求处理环境,使其匹配我们想要的网址。

In Nginx, add the following inside the server directive:在 Nginx 中,在 server 指令中添加以下内容:

location /competitor {
    proxy_pass http://127.0.0.1:5001;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header X-Script-Name /competitor;
}

In Flask, add the class below to your main application file (wherever you create the main Flask app object)在 Flask 中,将下面的类添加到您的主应用程序文件中(无论您在何处创建主 Flask 应用程序对象)

class ReverseProxied(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
        if script_name:
            environ['SCRIPT_NAME'] = script_name
            path_info = environ['PATH_INFO']
            if path_info.startswith(script_name):
                environ['PATH_INFO'] = path_info[len(script_name):]

        scheme = environ.get('HTTP_X_SCHEME', '')
        if scheme:
            environ['wsgi.url_scheme'] = scheme
        return self.app(environ, start_response)

Finally, force your app to use the new ReverseProxied middleware最后,强制您的应用程序使用新的 ReverseProxied 中间件

app = Flask(__name__)
app.wsgi_app = ReverseProxied(app.wsgi_app)

You can find the original solution and code snippets here http://flask.pocoo.org/snippets/35/您可以在这里找到原始解决方案和代码片段http://flask.pocoo.org/snippets/35/

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

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