Python 烧瓶:不断得到404错误

Python 烧瓶:不断得到404错误,python,nginx,flask,Python,Nginx,Flask,我在尝试访问www.root.com/website时经常遇到404错误(示例)。Flask不呈现错误处理程序,但不呈现应用程序中定义的“/”路由或任何其他路由。这在几个月前曾经奏效: app.py: import os import datetime from flask import Flask, render_template, redirect, url_for, session from flask import jsonify from flask_oauthlib.client

我在尝试访问www.root.com/website时经常遇到404错误(示例)。Flask不呈现错误处理程序,但不呈现应用程序中定义的“/”路由或任何其他路由。这在几个月前曾经奏效:

app.py:

import os
import datetime

from flask import Flask, render_template, redirect, url_for, session
from flask import jsonify
from flask_oauthlib.client import OAuth

from mod_db import db
from mod_form import form

application = Flask(__name__)
application.debug = False
WTF_CSRF_ENABLED = True
dir_route = str(os.path.dirname(os.path.realpath(__file__)))
base_dir = '' + dir_route.split('/')[-1]

[...]

@application.route('/')
def index():
    return render_template('cover.html', bd=base_dir)

@application.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404
nginx配置:

server {
    listen 80;
    server_name root.com www.root.com;
        # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name root.com www.root.com;
    root /var/www/KRAKEN/public;
    error_log /var/www/KRAKEN/public/nginx.error.log;
    ssl_certificate /etc/letsencrypt/live/root.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/root.com/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;
    # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
    ssl_dhparam /etc/nginx/dhparam.pem;
    # modern configuration. tweak to your needs.
    ssl_protocols TLSv1.2;
    ssl_ciphers 'yadayada';
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    add_header Strict-Transport-Security max-age=15768000;
    # OCSP Stapling ---
    # fetch OCSP records from URL in ssl_certificate and cache them
    ssl_stapling on;
    ssl_stapling_verify on;


    ssl_trusted_certificate /etc/letsencrypt/live/root.com/fullchain.pem;
    resolver 8.8.8.8 8.8.4.4;

        location / {
                index index.php index.html index.htm;
    }
        location /website {
                include uwsgi_params;
                uwsgi_pass unix:///run/uwsgi/website.sock;
        }
        location /website/static {
                root /var/www/KRAKEN/public/website/static/;
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }
}
应用程序结构:

/var/www/KRAKEN/public/website/

.
├── app.py
├── website.ini
├── bin
├── include
├── lib
├── mod_db
├── mod_form
├── multimedia
├── static
├── templates
└── uwsgi.log

为什么不尝试使用@application.before_request decorator创建一个before_request方法,并让该方法打印出所请求的url,这样您就可以确保/website实际上在应用程序中被解释为“/”。我怀疑nginx没有将/website从请求的路径中剥离出来,因此您的应用程序收到了/website(not/)的请求,而您没有相应的路径。@ChristopherShroba,而不是您Chris。试图得到一个你建议的例子,但找不到,想详细说明一下吗?非常感谢。我做了:@application.before_request def test():return str(request.url)出错。下面是我的意思示例;试着把我的before_request函数复制到你的函数中,看看路径是什么:谢谢。我刚刚收到502个错误的网关。为什么不尝试使用@application.before\u request decorator创建一个before\u request方法,并让该方法打印出所请求的url,以便您可以确保/website实际上在应用程序中被解释为“/”。我怀疑nginx没有将/website从请求的路径中剥离出来,因此您的应用程序收到了/website(not/)的请求,而您没有相应的路径。@ChristopherShroba,而不是您Chris。试图得到一个你建议的例子,但找不到,想详细说明一下吗?非常感谢。我做了:@application.before_request def test():return str(request.url)出错。下面是我的意思示例;试着把我的before_request函数复制到你的函数中,看看路径是什么:谢谢。我刚收到502个严重的网关错误。