如何使用 unix 套接字将 url 参数传递给 Nginx proxy_pass 后面的 Flask
How to pass url arguments to Flask behind Nginx proxy_pass with unix socket
我有一个使用 bjoern 作为 python 服务器的 Flask 应用程序。我有一个例子 url 是这样的:
http://example.com/store/junihh
http://example.com/store/junihh/product-name
其中 "junihh" 和 "product-name" 是我需要传递给 python 的参数。
在阅读了针对 TCP/IP 调用的性能后,我尝试使用 unix 套接字。但是现在我在浏览器上收到 502 错误。
这是我的会议片段:
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/;
}
}
如何使用 unix 套接字通过 Nginx proxy_pass 将 url 参数传递给 Flask?
感谢您的帮助。
这是我的配置文件,它可以工作。 502是因为找不到上游服务器的路由(即更改http://127.0.0.1:5000/ to http://localhost:5000/$1)会导致502.
nginx.conf
http {
server {
listen 80;
server_name localhost;
location ~ ^/store/(.*)$ {
proxy_pass http://127.0.0.1:5000/;
}
}
}
烧瓶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)
更新
或者你可以像这样使用 unix 套接字,但是在 uwsgi.
上中继
nginx.conf
http {
server {
listen 80;
location /store {
rewrite /store/(.+) break;
include uwsgi_params;
uwsgi_pass unix:/tmp/store.sock;
}
}
}
烧瓶app.py
同上,不变
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
另存为config.ini
,然后运行uwsgi config.ini
nginx 重新加载后,您可以访问您的页面;-)
我有一个使用 bjoern 作为 python 服务器的 Flask 应用程序。我有一个例子 url 是这样的:
http://example.com/store/junihh
http://example.com/store/junihh/product-name
其中 "junihh" 和 "product-name" 是我需要传递给 python 的参数。
在阅读了针对 TCP/IP 调用的性能后,我尝试使用 unix 套接字。但是现在我在浏览器上收到 502 错误。
这是我的会议片段:
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/;
}
}
如何使用 unix 套接字通过 Nginx proxy_pass 将 url 参数传递给 Flask?
感谢您的帮助。
这是我的配置文件,它可以工作。 502是因为找不到上游服务器的路由(即更改http://127.0.0.1:5000/ to http://localhost:5000/$1)会导致502.
nginx.conf
http {
server {
listen 80;
server_name localhost;
location ~ ^/store/(.*)$ {
proxy_pass http://127.0.0.1:5000/;
}
}
}
烧瓶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)
更新
或者你可以像这样使用 unix 套接字,但是在 uwsgi.
上中继nginx.conf
http {
server {
listen 80;
location /store {
rewrite /store/(.+) break;
include uwsgi_params;
uwsgi_pass unix:/tmp/store.sock;
}
}
}
烧瓶app.py
同上,不变
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
另存为config.ini
,然后运行uwsgi config.ini
nginx 重新加载后,您可以访问您的页面;-)