非根路径的附加NGINX路由



我在https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04设置我的NGINX/UWSGI/FLASK应用程序,但它只记录了在导航到root(/(时让应用程序工作的说明。到目前为止,一切都按照教程中的描述进行,但我如何使其适用于其他路线,如/tool/project。如果我尝试使用与root相同的参数将/tool/projectlocation添加到nginx-config中,它将不起作用,只返回404。我错过了什么?

我在/usr/share/html/nginx/tools中有一个指向模板文件夹的符号链接,该文件夹包含项目的index.html

user@vm:/usr/share/nginx/html/tools$ ls -l
total 0
lrwxrwxrwx 1 root root 47 Sep 23 14:32 project -> /home/username/git/project/templates/

我的NGINX配置/etc/NGINX/sites-enabled/project:

server {
listen 80;
server_name 192.168.0.x;
location / {
include         uwsgi_params;
uwsgi_pass      unix:/home/username/git/project/project.sock;
}
location /tools/project/ {
include uwsgi_params;
uwsgi_pass unix:/home/username/git/project/project.sock;
}
}

项目的WSGI配置:

[Unit]
Description=uWSGI instance to serve project
After=network.target
[Service]
User=username
Group=www-data
WorkingDirectory=/home/username/git/project
Environment="PATH=/home/username/git/project/env/bin"
ExecStart=/home/username/git/project/env/bin/uwsgi --ini project.ini
[Install]
WantedBy=multi-user.target

烧瓶代码:

app = Flask(__name__, static_url_path='',
static_folder='/usr/share/nginx/html/'
)
resource = '/usr/share/nginx/html/Common/custom/template/v1.0.0'
##############################################################################
@app.route("/")
def build_form() -> Response:
return render_template('index.html')
##############################################################################
if __name__ == "__main__":
app.run(host='0.0.0.0')
~                           

我不是专家,但我想把它想象成ngnix为你的flask应用程序提供服务(通过uwsgi(并将路由处理传递给它。所以,与其在ngnix配置中创建另一个location,不如从你的flank应用程序中处理所有内部路由,比如:

@app.route("/tools/projects")
def do_smthg_re_projects():
return "something"

但是,如果你想通过一条路径来提供除flask应用程序之外的其他东西,那么你可以在ngnix配置中将其配置为一个单独的位置(就像通过路由/static提供静态文件一样(

最新更新