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

时间:2017-12-22 09:38:52

标签: flask apache2 mod-wsgi

我想处理一个在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:error] [pid 28906:tid 139849921148672](70008)部分结果有效但处理不完整:[client 57.39.118.158:35172] mod_wsgi(pid = 28906):无法获得铲斗旅请求。" 之前,问题" url未找到"在404 url not found error for flask app on apache2解决了。

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

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

<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
文件是

#!/usr/bin/python3
import sys


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

from bridge import app as application
文件是

#!/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 privilage的用户。启用的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

可能会重写。导致重定向问题?有什么建议?

1 个答案:

答案 0 :(得分:0)

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

当时URL的安装点为/bridge,这就是您在URL路径中使用的内容。这被转换为:

SCRIPT_NAME=/bridge
PATH_INFO=

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

SCRIPT_NAME=/bridge
PATH_INFO=/

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

问题是你的处理程序只需要POST,并且通常重定向总是会导致后续请求为GET,这导致找不到处理程序,因为你的处理程序只接受{{1 }}

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

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

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