Mod_wsgi 和阿帕奇。哪里需要 Access-Control-Allow-Origin?

Mod_wsgi and Apache. Where is Access-Control-Allow-Origin needed?

我正在通过 JS 将数据发送到我的 apache2 服务器 运行 mod_wsgi 通过:

$.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 错误 > No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我尝试将 Header set Access-Control-Allow-Origin "*" 添加到 httpd.conf 以及主要的 Apache 配置中,但这没有用。我一直尝试将 ('Access-Control-Allow-Origin','*') 添加到 response_headers

我如何(以及在​​哪里)启用访问控制允许来源,以便我可以访问 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

在 Graham Dumpleton 的帮助下,我找到了答案。

我在 httpd.conf 和 /mods-available/wsgi.load 中都加载了模块,导致了奇怪的行为。

从 httpd.conf 中删除 LoadModule wsgi_module modules/mod_wsgi.so-2.7 导致模块被正确加载。然后 header 从 httpd.conf

加载

编辑:添加评论信息