Skip to content

Enable Django CORS (Cross Origin Resource Sharing) Headers configuration

Luca Lianas edited this page Oct 17, 2017 · 3 revisions

New versions of OMERO can be configured to support django-cors-headers module following these instructions https://docs.openmicroscopy.org/omero/5.4.0/sysadmins/unix/install-web.html#setting-up-cors

If you are running a version on OMERO lower than 5.4.0 follow these instructions.

Installing Django CORS headers module

In order to enable CORS Headers to integrate ome_seadragon backend with a OpenSeadragon viewer embedded in a web page on a different domain to the one that hosts OMERO.web, we suggest to use the django-cors-header module.

Install the module using PIP

pip install django-cors-headers

On the [OME_HOME]/lib/python/omeroweb/settings.py file add the module to the installed apps

INSTALLED_APPS = (
    ...
    'pipeline',
    'corsheaders'
)

and add the middleware to the available ones

MIDDLEWARE_CLASSES = (
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.BrokenLinkEmailsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
)

Note that CorsMiddleware needs to come before Django's CommonMiddleware if you are using Django's USE_ETAGS = True setting, otherwise the CORS headers will be lost from the 304 not-modified responses, causing errors in some browsers.

Module can be configured in the same file as well, add configuration parameters just before the block

# Load server list and freeze
from connector import Server

def load_server_list():
    for s in SERVER_LIST:  # from CUSTOM_SETTINGS_MAPPINGS  # noqa
        server = (len(s) > 2) and unicode(s[2]) or None
        Server(host=unicode(s[0]), port=int(s[1]), server=server)
    Server.freeze()
load_server_list()

If, for example, you want to allow only one domain to be able to comunicate with the backend your setup will be

CORS_ORIGIN_ALLOW_ALL = False

CORS_ORIGIN_WHITELIST = (
    '<YOUR_DOMAIN>[:PORT]',
)

Avoiding the "too many open files error"

The intensive usage of the random library by the CORS headers plugin can produce the error

OSError: [Errno 24] Too many open files: '/dev/urandom'

To solve this issue it is necessary to update the /etc/security/limit.conf file to ensure a value of 65535 open files like this example

#<domain>                      <type>  <item>         <value>
#
<omero_srv_user>               soft     nofile          65535
<omero_srv_user>               hard     nofile          65535

where <omero_srv_user> is the user running OMERO on your server.

The server must be restarted in order to make this change effective.