Flask w / Apache& FCGI路由问题

时间:2015-12-15 12:16:36

标签: python apache twitter-bootstrap-3 flask fastcgi

所以我一直在和Flask& Apache服务器上的Bootstrap。我已经达到了可以访问应用程序的程度。使用以下路线渲染“第一个”或“主要”模板:

来自view.py的

@app.route('/')
def fn_home():
    return render_template("main.html")

不幸的是,每次尝试从main.html路由到另一个网页/功能都会失败。我正在使用导航栏列表href中的“url_for”函数,尝试让烧瓶向Apache提供xls-upload.html网页。

来自main.html的

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
  <ul class="nav navbar-nav">
    <li><a href="{{ url_for('upload') }}">Upload Spreadsheets </a></li>
来自view.py的

@app.route('/upload')
def upload():
    return render_template("xls-upload.html")

看起来函数正在使用,因为URL更改为http://myapp/upload,但html页面不会被函数呈现/返回 - 而是收到404“Not Found”。我似乎无法从函数返回任何内容,甚至return "Hello World"

似乎“看起来像”Apache正在尝试解析http://myapp/upload路径,而不是向Flask应用程序打开一个套接字,然后通过该套接字发送html。我不确定这是否是一个FCGI问题,如果我错过了相对/绝对路径问题,误解了Flask的一般工作原理,或者是所有的一些组合等等。

我是Flask的新手,所以我希望有人可以帮助我,因为我真的觉得自己走到了尽头。

提前致谢!

我的烧瓶应用程序结构如下:

  • var / www / cgi-bin / myapp /(root dir)
    • start.fcgi
    • view.py(烧瓶路由/ app文件)
    • 静态(dir)
      • 引导程序文件
    • 模板(目录)
      • main.html中
      • XLS-upload.html

以下是我的适用文件:

1)/etc/httpd/conf.d/myapp:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/cgi-bin/myapp/static/
    ServerName myapp

    Alias /static/ /var/www/cgi-bin/myapp/static/
    ScriptAlias / /var/www/cgi-bin/myapp/start.fcgi

    <Directory "var/www/cgi-bin/myapp">
        AllowOverride None
        Order allow,deny
        Allow from all
        AuthType Basic
        AuthUserFile /etc/httpd/.htpasswd
        AuthName 'Enter Password'
        Require valid-user
    </Directory>

</VirtualHost>

2)/var/www/cgi-bin/myapp/start.fcgi:

#!/usr/bin/python

#  IMPORTS:

from flup.server.fcgi import WSGIServer
from view import app

if __name__ == '__main__':
    WSGIServer(app).run()

3)/var/www/cgi-bin/myapp/view.py:

#!/usr/bin/python

# IMPORTS:
import os
from flask import Flask, render_template, url_for, request, session, redirect
from werkzeug import secure_filename

# STATIC VARIABLES
UPLOAD_FOLDER = 'var/www/cgi-bin/myapp/xls-dir'
ALLOWED_EXTENSIONS = set(['xls'])

## flask:
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# FUNCTIONS
def fn_allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/')
def fn_home():
    return render_template("main.html")

@app.route('/upload')
def upload():
    return render_template("xls-upload.html")
    #return "HI there"

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

4)/var/www/cgi-bin/myapp/templates/main.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>ALG Tools HOME</title>

    <!-- Bootstrap -->
    <link href="{{ url_for('static', filename = 'css/bootstrap.min.css') }}" rel="stylesheet">
  </head>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/">ALG Tool - HOME</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="{{ url_for('upload') }}">Upload Spreadsheets </a></li>
        <li><a href="/xls-download.html">Download Spreadsheets</a></li>
        <li><a href="/cfg-generate.html">Generate Configs</a></li>
      </ul>
    </div>
  </div>
</nav>

  <body>
    <h2>ALG stuff</h2>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="{{ url_for('static', filename = 'js/bootstrap.min.js') }}"></script>
  </body>

</html>

5)/var/www/cgi-bin/myapp/templates/xls-upload.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>ALG XLS Upload</title>

    <!-- Bootstrap -->
    <link href="{{ url_for('static', filename = 'css/bootstrap.min.css') }}" rel="stylesheet">
  </head>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/">ALG Tool - HOME</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li><a href="/xls-upload.html">Upload Spreadsheets </a></li>
        <li><a href="/xls-download.html">Download Spreadsheets</a></li>
        <li><a href="/cfg-generate.html">Generate Configs</a></li>
      </ul>
    </div>
  </div>
</nav>

  <body>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="{{ url_for('static', filename = 'js/bootstrap.min.js') }}"></script>

  </body>

</html>

1 个答案:

答案 0 :(得分:0)

FCGI并不是一种推荐的Python Web应用程序服务方式。您应该研究运行WSGI的众多方法之一。

但是,假设您出于某种原因需要执行此操作,则会出现轻微的配置问题,这是导致问题的原因;你需要在ScriptAlias路径上使用尾部斜杠。

ScriptAlias / /var/www/cgi-bin/myapp/start.fcgi/

有了这个,Apache将传递start.fcgi脚本的完整路径,而不是替换它。

请注意,即使使用FCGI,也不应将您的应用代码放在cgi-bin中。它并不需要在那里,因为它不像CGI应用程序那样由Web服务器运行。实际上,您的代码根本不应该在/ var / www下。