apache2上的flask应用未找到404

apache2上的flask应用未找到404

本文介绍了apache2上的flask应用未找到404 URL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Flask写了一个端点来处理json格式的http发布请求数据.尝试在apache2上部署它时,发布请求出现404错误:在此服务器上找不到请求的URL/bridge".我正在使用python 3.5.2,apache 2.4,OpenSSL/1.0.2g,ubuntu 16.04,针对python 3.5.1+编译的mod_wsgi4.3.0

I wrote an endpoint with Flask to handle http post request data in json format. When trying to deploy it on apache2, post requests got 404 error: "The requested URL /bridge was not found on this server" . I'm using python 3.5.2, apache 2.4, OpenSSL/1.0.2g, ubuntu 16.04, mod_wsgi4.3.0 compiled for python 3.5.1+

有人知道吗? Apache无法看到/var/www,但为什么呢?还是SSL配置有问题? (https适用于网站)

Anyone has any idea? Apache cannot see /var/www but why? or there is a problem in SSL configuration? (https works for the webiste)

我有

在/var/www/bridge/bridge.wsgi

at /var/www/bridge/bridge.wsgi

#!/usr/bin/python3
import sys

sys.path.insert(0,'/var/www/bridge')

from bridge import app as application

位于/etc/apache2/sites-enabled/bridge.conf的

conf文件

conf file at /etc/apache2/sites-enabled/bridge.conf

<VirtualHost *:443>
ServerName www.newocto.org

WSGIDaemonProcess bridge user=www-data group=www-data threads=5  home=/var/www/bridge/
WSGIScriptAlias /bridge /var/www/bridge/bridge.wsgi

<Directory /var/www/bridge>
    WSGIProcessGroup bridge
    WSGIApplicationGroup %{GLOBAL}


    Require all granted
</Directory>

我的000-default.conf文件是这样的:

my 000-default.conf file is like this:

<VirtualHost *:443>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName www.newocto.org
DocumentRoot /var/www/html

SSLEngine on
SSLCertificateFile /etc/ssl/certs/newocto_org.crt
SSLCertificateKeyFile /etc/ssl/private/newocto.key
SSLCertificateChainFile /etc/ssl/certs/COMODORSAAddTrustCA.crt
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

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

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

<VirtualHost *:80>
ServerName  newocto.org/
Redirect / https://newocto.org/
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

和我在/var/www/bridge/bridge.py的烧瓶代码是

and my flask code at /var/www/bridge/bridge.py is

#!/usr/bin/python3

from flask import Flask


app = Flask(__name__)

@app.route('/', methods =['POST'])
def deliver():
    from flask import request
    raw=request.get_json(force=True)
    return raw

import mysql.connector
dbconn=  mysql.connector.connect(host='xxxx',port='3306',database='xx',user='root',password='xxx')
cursor=dbconn.cursor()
query="""insert into bridge_test2 (email) values ('{}')""".format(raw)
cursor.execute(query)
dbconn.commit()
dbconn.close()


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



#file permissions are like this:
#4 -rwxr-xr-x 1 root www-data 537 Dec 20 13:46 bridge.py
#4 -rwxr-xr-x 1 root www-data 117 Dec 20 14:10 bridge.wsgi

推荐答案

对于相同的ServerName和端口,您不能有两个VirtualHost节,它将仅使用它看到的第一个.

You can't have two VirtualHost sections for same ServerName and port, it will only use the first it sees.

对于相同的ServerName和端口,将bridge.confVirtualHost的内容复制到000-default.conf中的内容.

Copy the contents of the VirtualHost in bridge.conf into that in 000-default.conf for same ServerName and port.

这篇关于apache2上的flask应用未找到404 URL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:18