ubuntu - Flask + UWSGI + NGinx = 504 超时,除了 root
问题描述
我一直在研究和尝试数百种配置,但一无所获。我只有 2 条路线,“/”和“/search/”。该网站在 / 位置加载正常,但是当我单击 img 执行搜索时,它挂起并给出 504。
如果我像 python3 app.py 一样直接运行服务器,它可以完美运行。
如果我运行它 uwsgi, uwsgi --socket 0.0.0.0:8080 --protocol=http -w run:app ,它也可以完美运行,所以我认为问题出在 nginx 配置上,但我不知道可能发生什么可能是错的(我是新手)
这是我的文件:
运行.py
from server import app
if __name__ == '__main__':
app.run()
服务器.py
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
uploaded_imgs = os.listdir('static/quick_demo')
uploaded_imgs_rdn = set(random.sample(uploaded_imgs, 7))
if request.method == 'POST':
file = request.files['query_img']
ts = int(time.time())
img = Image.open(file.stream) # PIL image
uploaded_img_path = "static/uploaded/{}_{}".format(ts, file.filename)
img.save(uploaded_img_path)
//some code here ...
return render_template('index.html',
query_path=uploaded_img_path,
query_upload_path=uploaded_imgs_rdn,
scores=scores)
else:
return render_template('index.html', query_upload_path=uploaded_imgs_rdn)
@app.route('/search/<img_id>', methods=['GET'])
def search_demo(img_id):
img_name = img_id
uploaded_imgs = os.listdir('static/quick_demo')
uploaded_imgs_rdn = set(random.sample(uploaded_imgs, 7))
if request.method == 'GET':
# file = request.files['query_img']
# ts = int(time.time())
img = Image.open("static/quick_demo/{}".format(img_name)) # PIL image
uploaded_img_path = "/static/quick_demo/{}".format(img_name)
# img.save(uploaded_img_path)
//some code here ...
return render_template('index.html',
query_path=uploaded_img_path,
query_upload_path=uploaded_imgs_rdn,
scores=scores)
else:
return render_template('index.html', query_upload_path=uploaded_imgs_rdn)
nginx_site.conf
server {
listen 80;
listen [::]:80;
root /var/www/site;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name XXX.XXX.XXX.XXX;
location / {
#try_files $uri $uri/ =404;
include uwsgi_params;
uwsgi_pass unix:/var/run/uwsgi/site.sock;
}
}
/etc/uwsgi/vassals/site.ini
[uwsgi]
chdir = /var/www/site
module = run:app
touch-reload = /var/www/site/run.py
logto = /var/log/uwsgi/site.log
# master with 2 worker process (based on CPU number)
master = true
processes = 2
# use unix socket for integration with nginx
socket = /var/run/uwsgi/site.sock
chmod-socket = 660
# enable socket cleanup when process stop
vacuum = true
# ensure compatibility with init system
die-on-term = true
我正在关注本教程 https://code.luasoftware.com/tutorials/nginx/setup-nginx-and-uwsgi-for-flask-on-ubuntu/
请帮忙。
再次感谢
解决方案
推荐阅读
- c# - 从 C# 将布尔值传递给 View
- docker - 有没有办法使用现有的 Docker Image 创建 NuxtJS 应用程序而不使用 npm create-app 命令
- shell - 在 bash shell 脚本中将字符串与文字连接起来
- android - android项目中项目级gradle文件中的google()和mavenCentral()有什么区别?
- html - 在 img 上居中 h5。目前正试图将“最热门的项目”放在我的图像上。图像和每个 div 将是垂直的
- amazon-web-services - 所有 ec2 实例的单一 IP 地址
- r - 缺少分组变量时如何避免计算均值?
- python-3.x - 正则表达式拆分特定长度的数字
- python-3.x - 返回响应并重定向——Django
- javascript - JS中的播放、暂停和删除