gpt4 book ai didi

apache - 子目录上的 Flask App 404

转载 作者:行者123 更新时间:2023-12-03 17:00:07 28 4
gpt4 key购买 nike

我正在尝试让 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com