使用nginx运行部署cherrypy时出错

使用nginx运行部署cherrypy时出错,nginx,flask,cherrypy,spark-submit,Nginx,Flask,Cherrypy,Spark Submit,我在flask中创建了一个应用程序,并用cherrypy部署了它 def run_server(app): # Enable WSGI access logging via Paste app_logged = TransLogger(app) app.config['JSON_AS_ASCII'] = False # Mount the WSGI callable object (app) on the root directory cherrypy.tree.graft(app_logg

我在flask中创建了一个应用程序,并用cherrypy部署了它

def run_server(app):

# Enable WSGI access logging via Paste
app_logged = TransLogger(app)
app.config['JSON_AS_ASCII'] = False 
# Mount the WSGI callable object (app) on the root directory
cherrypy.tree.graft(app_logged, '/')

# Set the configuration of the web server
cherrypy.config.update({
    'engine.autoreload.on': True,
    'log.screen': True,
    'server.socket_port': 5050,
    'server.socket_host': '0.0.0.0'
})

# Start the CherryPy WSGI web server
cherrypy.engine.start()
cherrypy.engine.block()

if __name__ == "__main__":
# Init spark context and load libraries
sc = init_spark_context()
app = create_app(sc)

# start web server
run_server(app)
通过添加此代码,我使用nginx锁定了端口5050

server {
    listen 5050;
    listen [::]:5050;
    server_name _;
    root /var/www/html;
    location / {
            try_files $uri $uri/ =404;
    }

}
/etc/nginx/sites available/default

我在运行cherrypy(或spark submit)时遇到此错误

nginx和cherrypy(Python)是独立的HTTP服务器,因此它们都需要自己的端口。您将看到
端口5050不是免费的
错误,因为这些进程正在竞争同一个进程。(在本例中,看起来nginx进程是先启动的,所以它“击败”了cherrypy)。以下是相关配置:

樱桃糖

cherrypy.config.update({
    'engine.autoreload.on': True,
    'log.screen': True,
    'server.socket_port': 5050,      # <-- port 5050
    'server.socket_host': '0.0.0.0'
})
这将创建一个运行在端口8080上的nginx服务器,该服务器将
/foo
请求代理到运行在端口5050上的cherrypy服务器

换句话说,对于在Flask应用程序中实现的假设端点
/bar
,您应该看到这两个调用的相同结果:

curl localhost:5050/bar      # hits the cherrypy server directly, bypassing nginx
curl localhost:8080/foo/bar  # hits nginx, which matches on the `foo` prefix and forwards the request to cherrypy
这有点做作,但希望能说明主要问题:nginx和cherrypy是独立的服务器,因此它们都需要自己的端口。(如果您同时运行多个cherrypy服务器,则此模式特别方便,因为所有internet流量都可以定向到nginx,然后nginx可以向相应的cherrypy服务器发送调用。)

server {
    listen 5050;  # <-- port 5050
server {
    listen 8080;
    server_name localhost;

    upstream my_cherrypy_svc {
        server localhost:5050;
    }

    location /foo {
        proxy_pass http://my_cherrypy_svc;
        proxy_redirect default;
    }
}
curl localhost:5050/bar      # hits the cherrypy server directly, bypassing nginx
curl localhost:8080/foo/bar  # hits nginx, which matches on the `foo` prefix and forwards the request to cherrypy