wsgi错误:无法获取请求的桶旅和apache2重定向帖子来获取?

问题描述 投票:0回答:1

我想处理一个在apache2上部署Flask应用程序的帖子请求,但我得到了意想不到的重定向。它也失去了请求体。我使用网络应用程序和访问日志触发帖子请求:57.39.118.158 - [22 / Dec / 2017:11:44:32 +0300]“POST / bridge HTTP / 1.1”301 3830“ - ”“ - ” 57.39.118.158 - - [22 / Dec / 2017:11:44:32 +0300]“GET / bridge / HTTP / 1.1”500 860“ - ”“ - ”和错误日志:“[Fri Dec 22 11:44: 51.864122 2017] [wsgi:错误] [pid 28906:tid 139849921148672](70008)部分结果有效但处理不完整:[client 57.39.118.158:35172] mod_wsgi(pid = 28906):无法获取请求的存储桶旅。 “之前,404 url not found error for flask app on apache2的“url not found”问题已经解决。

我正在使用python 3.5.2,apache 2.4,OpenSSL / 1.0.2g,ubuntu 16.04,mod_wsgi4.3.0编译为python 3.5.1+

我启用了一个独特的conf文件,如下所示:

<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 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


# 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

WSGIDaemonProcess bridge user=dogacandu group=dogacandu 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>

# 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>

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

文件/var/www/bridge/bridge.wsgi是

#!/usr/bin/python3
import sys


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

from bridge import app as application

/var/www/bridge/bridge.py上的文件是

#!/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)
    import mysql.connector
    dbconn= mysql.connector.connect(host='xx.20.xxx.245',port='3306',database='xxa',user='root',password='Jxxxo')
    cursor=dbconn.cursor()
    query1="""insert into bridge_test2 (email) values ('blah')"""
    query2="""insert into bridge_test2 (email) values ('{}')""".format(raw)
    cursor.execute(query1)
    dbconn.commit()
    cursor.execute(query2)
    dbconn.commit()
    dbconn.close()
    return 'ok'

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

文件权限:

4 -rwxr-xr-x 1 root dogacandu 654 Dec 21 17:31 bridge.py  
4 -rwxr-xr-x 1 root dogacandu 117 Dec 20 18:26 bridge.wsgi

dogacandu是具有sudo权限的用户。启用的mod是:

access_compat.load authn_core.load authz_user.load cgid.load dir.load mime.load negotiation.load socache_shmcb.load status.load alias.conf authn_file.load autoindex.conf deflate.conf env.load mpm_event.conf rewrite.load ssl。 conf wsgi.conf alias.load authz_core.load autoindex.load deflate.load filter.load mpm_event.load setenvif.conf ssl.load wsgi.load auth_basic.load authz_host.load cgid.conf dir.conf mime.conf negotiation.conf setenvif .load status.conf

可能rewrite.load导致重定向问题?有什么建议?

flask apache2 mod-wsgi
1个回答
0
投票

我会说这种行为可能是预期的。

然后URL的挂载点是/bridge,这是您在URL的路径中使用的。这被转换为:

SCRIPT_NAME=/bridge
PATH_INFO=

当传递给Flask时。通过路线的设置方式,Flask期待看到:

SCRIPT_NAME=/bridge
PATH_INFO=/

因此,Flask强制重定向以强制浏览器添加尾部斜杠。

问题是您的处理程序只需要POST,并且通常重定向将始终导致后续请求为GET,导致找不到处理程序,因为您的处理程序只接受POST

简而言之,在路径上有一个POST处理程序是一个坏主意,它可以成为自动尾随斜杠重定向的主题。在这种情况下,当挂载点是子URL时,在WSGI应用程序的挂载处具有处理程序时会发生这种情况。

要测试处理程序是否有效,请在浏览器中使用/bridge/而不是/bridge。最好还是将POST处理程序移动到WSGI应用程序的挂载点以外的其他路径。

至于其他奇怪的Apache错误,当使用安全连接时,由于错误导致连接被拉下,有时可以得到这些错误。

© www.soinside.com 2019 - 2024. All rights reserved.