apache - 子目录上的 Flask App 404

标签 apache flask

我正在尝试让 flask 在 http://<my-server.com>/UserControl 上运行

以下是相关文件:

Apache

<VirtualHost *:80>   
    ServerName <redacted>
    ServerAdmin webmaster@localhost

    # not needed actually because we're proxying everything to Tomcat, but just leave it to make Apache happy
    DocumentRoot /var/www/html
    DirectoryIndex index.php


    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  <Location /alpha>
     AuthType Basic
     AuthName "Restricted Access - Authenticate"
     AuthUserFile /etc/httpd/htpasswd.users
     Require valid-user
   </Location>

   <Location /UserControl>
        AuthType Basic
        AuthName "Restricted Access - Authenticate"
        AuthUserFile /etc/httpd/htpasswd.users
        Require valid-user
   </Location>

  Redirect /alpha /alpha/
  ProxyPass /alpha/ http://127.0.0.1:3838/alpha/
  ProxyPassReverse /alpha/ http://127.0.0.1:3838/alpha/


  WSGIDaemonProcess UserControl user=ubuntu group=ubuntu threads=5
  WSGIScriptAlias /UserControl/ /home/ubuntu/FlaskApps/UserControl/UserControl.wsgi

  <Directory /home/ubuntu/FlaskApps/UserControl>
            WSGIProcessGroup UserControl
            WSGIApplicationGroup %{GLOBAL}
            WSGIScriptReloading On
            Order deny,allow
            #Allow from all
            Require all Granted
   </Directory>



</VirtualHost>

请注意,我还有一些其他目录可以运行。

我的相关 .py 和 .wsgi 文件是:

用户控制.py
from flask import Flask
app = Flask(__name__)

@app.route("/UserControl")
def main():
    return "Welcome!"

if __name__== "__main__":
    app.run()

用户控制.wsgi
import sys, os
sys.path.insert (0,'/home/ubuntu/FlaskApps/UserControl')
#os.chdir("/home/ubuntu/FlaskApps")
from UserControl import app as application

这些文件位于
/home/ubuntu/FlaskApps/UserControl

但是,当我访问:
http://<my-server.com/>UserControl我得到一个 404 (通过 FLASK!——所以 apache 似乎路由正确)

我到底错过了什么?

最佳答案

您在没有使用尾部斜杠的情况下定义了路线。您的反向代理可能会重定向到带有斜杠的路由。当您以这种方式定义路由时,使用尾部斜杠访问 URL 将产生 404“未找到”错误。 quickstart guide 中对此进行了介绍.

您可以禁用“严格斜杠”或使用尾部斜杠定义路由。

从文档中:

Take these two rules:


@app.route('/projects/')
def projects():
    return 'The project page'

@app.route('/about')
def about():
    return 'The about page'

Though they look rather similar, they differ in their use of the trailing slash in the URL definition. In the first case, the canonical URL for the projects endpoint has a trailing slash. In that sense, it is similar to a folder on a filesystem. Accessing it without a trailing slash will cause Flask to redirect to the canonical URL with the trailing slash.

In the second case, however, the URL is defined without a trailing slash, rather like the pathname of a file on UNIX-like systems. Accessing the URL with a trailing slash will produce a 404 “Not Found” error.

This behavior allows relative URLs to continue working even if the trailing slash is omitted, consistent with how Apache and other servers work. Also, the URLs will stay unique, which helps search engines avoid indexing the same page twice.

关于apache - 子目录上的 Flask App 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41930138/

相关文章:

php - Apache 权限,PHP 文件创建,MKDir 失败

java - 嵌入式 Apache FTP 服务器 : Unable to authenticate client using DbUserManager

php - Node js/Express 替代 LAMP

php - UTF-8贯穿始终

python - 如何使用 Python 创建嵌套的 JSON 对象?

python - 属性错误 : module 'jinja2.ext' has no attribute 'autoescape' while trying to use Flask-Babel

php - opcache 对 cgi/fastcgi 的命中率为零

python - 在 before_app_request 中重定向

python - (flask) python - mysql - 在带有来自 URL 的变量的选择查询中使用 where 子句

python - Flask:强制下载pdf文件在浏览器中打开