在this教程之后,我尝试使用Nginx和Gunicorn部署Flask应用。仅使用Gunicorn启动应用程序并连接到它就可以正常工作。但是,在将Unicorn实例配置为服务项目并配置Nginx之后,连接到端点在浏览器控制台中显示状态502,并在Nginx error.log中显示权限错误。我知道(13: Permission denied) while connecting to upstream:[nginx]中的类似问题。但是,由于我的系统是RHEL 7,并且我不理解答案中的命令,因此我不确定要运行哪些命令。可以在下面找到配置和错误消息。如果您可以找到问题,请帮助我。
/etc/nginx/conf.d/default.conf
server {
listen 9000;
server_name localhost;
location /fasttext{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/ec2-user/projects/vectorbot/vectorbot.sock;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Flask应用
import plyvel
from flask import Flask
from flask_restful import Api
from laserembeddings import Laser
from getters.getIntents import *
from getters.getEntities import *
app = Flask(__name__)
api = Api(app)
path_to_bpe_codes = 'data/laser_models/93langs.fcodes'
path_to_bpe_vocab = 'data/laser_models/93langs.fvocab'
path_to_encoder = 'data/laser_models/bilstm.93langs.2018-12-26.pt'
laser = Laser(path_to_bpe_codes, path_to_bpe_vocab, path_to_encoder)
@app.route('/fasttext/lang/si/<keylist>', methods=['GET'])
def get_si(keylist):
return 'Success'
# Initialize and start the web application
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
wsgi.py
from expose import app
if __name__ == "__main__":
app.run()
/etc/systemd/system/vectorbot.service
[Unit]
Description=Gunicorn instance to serve vectorbot
After=network.target
[Service]
User=ec2-user
Group=ec2-user
WorkingDirectory=/home/ec2-user/projects/vectorbot
Environment="PATH=/home/ec2-user/projects/vectorbot/venv/bin"
ExecStart=/home/ec2-user/projects/vectorbot/venv/bin/gunicorn --workers 3 --bind unix:vectorbot.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
服务成功启动,没有错误
在/var/log/nginx/error.log
2020/03/11 12:10:28 [crit] 13499#13499: *291 connect() to unix:/home/ec2-user/projects/vectorbot/vectorbot.sock failed (13: Permission denied) while connecting to upstream, client: #######, server: localhost, request: "GET /fasttext/lang/si/i%20have%20a%20question HTTP/1.1", upstream: "http://unix:/home/ec2-user/projects/vectorbot/vectorbot.sock:/fasttext/lang/si/i%20have%20a%20question", host: "#######"
在浏览器中
状态码502