在服务器上运行时跳过Flask路由

时间:2019-12-12 19:51:44

标签: nginx flask

烧瓶路径有奇怪的问题。这是配置

from flask import Flask, send_from_directory
from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
from config import config
from flask_login import LoginManager
from flask_pagedown import PageDown
from flask_cors import CORS
import os
import logging

bootstrap = Bootstrap()
moment = Moment()
db = SQLAlchemy()
mail = Mail()
login_manager = LoginManager()
login_manager.session_protection = 'strong'
pagedown = PageDown()

'''The login_view
attribute sets the endpoint for the login page. Recall that because the login route is inside
a blueprint, it needs to be prefixed with the blueprint name.'''

if os.environ.get('FLASK_CONFIG', '').startswith('development'):
    config_name = 'development'
elif os.environ.get('FLASK_CONFIG', '').startswith('docker_development'):
    config_name = 'docker_development'
elif os.environ.get('FLASK_CONFIG', '').startswith('production'):
    config_name = 'production'
else:
    config_name ='default'

app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)

# ====================================== LOGGING =========================================
# ========================================================================================
logging.basicConfig(level=logging.INFO)
logging.info('C_O_N_F_I_G with %s' % config_name)
logging.info(__name__)
app.config.update( DEBUG=True, MAIL_SERVER='smtp.gmail.com',
                   MAIL_PORT=587, MAIL_USE_SSL=False, MAIL_USE_TLS=True, MAIL_USERNAME = 'test.me@gmail.com')

bootstrap.init_app(app)
moment.init_app(app)
db.init_app(app)
mail.init_app(app)
login_manager.init_app(app)
pagedown.init_app(app)
CORS(app, supports_credentials=True)

from app.api_books import api as api_1_0_blueprint_books
app.register_blueprint(api_1_0_blueprint_books, url_prefix='/books/v1.0')

@app.route('/<path:path>', methods=['GET'])
def static_proxy(path):
  logging.info('path:path')
  return send_from_directory('./dist/', path)

@app.route('/')
def root():
  logging.info('path:/')
  return send_from_directory('./dist/', 'index.html')

with app.app_context():
    logging.info(db.engine.url)

当我在本地计算机上运行它时,以'books'字符串开头的路径会加载书籍蓝图中的视图。

第1步:当我将其上传到nginx服务器时,并加载了包含“ books”的相同URL,该站点将加载默认路由(默认url从dist文件夹加载index.html)。

步骤2。然后Cntr + F5重新加载页面,并调用书路径,并在浏览器中显示它。太好了。

第3步。但是,如果我单击“书籍”页面上的某个链接(该链接指向另一个书籍页面),则会再次重定向到默认路由。

所以我不明白为什么仅使用Cntr + F5。另外我也不明白为什么带有“ books”的网址会加载默认网址。

以下是烧瓶问题,可能与此有关。: https://askubuntu.com/questions/1173951/all-routes-except-return-404-from-nginx-running-flask-wsgi

NGINX:

upstream app_servers {
        server web:8000;
}

server {
    listen 80;
    server_name test.me;
    return 301 https://$host$request_uri;
}

# main server block
server {
    listen 443 ssl;

    # enable subfolder method reverse proxy confs
    include /config/nginx/proxy-confs/*.subfolder.conf;

    # all ssl related config moved to ssl.conf
    include /config/nginx/ssl.conf;

    # enable for ldap auth
    #include /config/nginx/ldap.conf;

    client_max_body_size 0;

    # Configure NGINX to deliver static content from the specified folder
    location /static {
        alias                       /usr/src/app/vinci_flask/app/static/dist;
    }

    location / {
            proxy_pass         http://app_servers;
            proxy_redirect     off;
            proxy_set_header   Host $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-Host $server_name;
    }

    # location / {
    #   try_files $uri $uri/ /index.html /index.php?$args =404;
    # }

}

# enable subdomain method reverse proxy confs
include /config/nginx/proxy-confs/*.subdomain.conf;
# enable proxy cache for auth
proxy_cache_path cache/ keys_zone=auth_cache:10m;

0 个答案:

没有答案