Python Mod_wsgi和Apache。哪里需要访问控制?
我正在通过JS将数据发送到运行mod_wsgi的apache2服务器,通过:Python Mod_wsgi和Apache。哪里需要访问控制?,python,apache,apache2,mod-wsgi,access-control,Python,Apache,Apache2,Mod Wsgi,Access Control,我正在通过JS将数据发送到运行mod_wsgi的apache2服务器,通过: $.ajax({ method: "POST", url: "https://annotatie01.io.tudelft.nl/app", data: "test-data", success: function(response) { console.log(response) } }); 如果我要发送
$.ajax({
method: "POST",
url: "https://annotatie01.io.tudelft.nl/app",
data: "test-data",
success: function(response) {
console.log(response)
}
});
如果我要发送到的.wsgi文件如下所示,则它可以工作:
def application(environ, start_response):
status = '200 OK'
length = int(environ.get('CONTENT_LENGTH', '0'))
data = environ['wsgi.input'].read(length)
response_headers = [('Content-type', '*'),
('Content-Length', str(len(data)))]
start_response(status, response_headers)
return data
当我尝试使用f=open之类的工具进行保存时,或者当我尝试访问SQL数据库时:
import sqlite3
def application(environ, start_response):
status = '200 OK'
length = int(environ.get('CONTENT_LENGTH', '0'))
data = environ['wsgi.input'].read(length)
conn = sqlite3.connect('/var/database/material3.db')
c = conn.cursor()
c.execute('SELECT image from image_table limit 1')
data = c.fetchall()
response_headers = [('Content-type', '*'),
('Content-Length', str(len(data)))]
start_response(status, response_headers)
return data
我得到一个XMLHttpRequest错误>请求的资源上不存在“Access Control Allow Origin”头。因此,不允许访问源“null”
我尝试将头集访问控制Allow Origin*添加到httpd.conf以及主Apache配置中,但没有成功。我一直尝试在响应头中添加“访问控制允许源文件”、“*”
如何以及在何处启用访问控制allow origin,以便访问sqlite数据库,或使用f=open编写内容
编辑:我的httpd.conf文件
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
<VirtualHost *:443>
Header set Access-Control-Allow-Origin "*"
ServerName annotatie01.io.tudelft.nl:443
SSLEngine on
SSLCertificateFile /var/www/experiment/CSR/annotatie01_io_tudelft_nl.crt
SSLCertificateKeyFile /var/www/experiment/CSR/annotatie01_io_tudelft_nl.key
WSGIScriptAlias /app /var/www/experiment/experiment/py/mod_wsgi-test/myapp.wsgi
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Header set Access-Control-Allow-Origin "*"
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
LoadModule wsgi_module modules/mod_wsgi.so-2.7
</Directory>
</VirtualHost>
<VirtualHost *:80>
Header set Access-Control-Allow-Origin "*"
DocumentRoot /data_nfs/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /data_nfs/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
还有我的apache2.conf:
SSLCipherSuite ALL:!aNULL:RC4+RSA:+HIGH:+MEDIUM:+LOW:+EXP:+eNULL
SSLProtocol TLSv1
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
Header set Access-Control-Allow-Origin "*"
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
Include /etc/apache2/httpd.conf
在格雷厄姆·邓普尔顿的帮助下,我终于找到了答案 我在httpd.conf和/mods available/wsgi.load中加载模块,导致了奇怪的行为 从httpd.conf中删除LoadModule wsgi_module modules/mod_wsgi.so-2.7会导致正确加载模块。然后从httpd.conf加载头
编辑:added comments info显示Apache配置文件中的mod_wsgi配置,以便查看指定头指令的位置。@GrahamDumpleton我已经添加了httpd.conf文件。我相信这也是您所指的文件。如您所见,我不确定将其放置在何处。我很惊讶Apache在启动时没有给出错误,因为LoadModule不能在VirtualHost中的任何位置使用。只要我不附加到应用程序之外的访问,它就可以工作。应该在哪里加载模块?在您的设置中,mods available目录中包含一个wsgi.load文件。当模块启用时,该wsgi.load文件将被符号链接到mods enabled目录中。这将被IncludeOptional mods enabled/*.conf行包含。与mod_wsgi的全局初始化有关的任何内容都将放在该wsgi.conf文件中,如有必要,例如用于Python共享库的LoadFile和WSGIPythonHome。由于它与mod_wsgi模块无关,因此通常不会将Header指令添加到wsgi.load。你可以把它保存在虚拟主机中。