WebFaction
Community site: login faq

Hi all. I created a "static only" app to serve static content for my django application using this guide: https://docs.webfaction.com/software/static.html#static-only .

Now I want to serve a couple of js files with CORS enabled. What's the best way to do it? I have seen that it's possible in a "Static/CGI/PHP" application setting the appropriate header in .htaccess. I was wondering if this is possible also within a static only app.

Thanks, Sergio

asked 18 Nov, 16:28

mosta
111
accept rate: 0%


You should be serving static media in Django as per, https://docs.webfaction.com/software/django/getting-started.html#configuring-django-to-serve-static-media

Then in your Django apache httpd.conf, set these CORS directives.

LoadModule headers_module    modules/mod_headers.so

<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "POST, GET, OPTIONS,DELETE,PUT"
Header set Access-Control-Allow-Headers "X-Accept-Charset,X-Accept,Content-Type"
Header always set Access-Control-Max-Age "3600"
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}]
</IfModule>

Then ensure you restart apache after the changes.

permanent link

answered 18 Nov, 21:10

NickR ♦♦
4165
accept rate: 18%

edited 18 Nov, 21:12

Hi Nick, thanks for your reply.

Maybe I've setup something wrong because your solution doesn't work for me.

After changing the httpd.conf file as you suggested I see the added headers when I access any resource under my django application (so, any call to a django view now have the Access-Control-Allow-Origin header).

But if I try to open any static resource that is served by the "static only (no .htaccess)" app the custom headers are missing.

My django app is running with DEBUG=False and the static files are actually served; they are read from the STATIC_ROOT folder, so it seems that the configuration is correct; they just don't have the CORS headers.

My opinion is that the static files are not affected by the apache configuration of my django app. To test this I have stopped apache in my django app. Now the django app is not responding (as expected) and the static files are still served even if apache is not running.

Any idea?

(19 Nov, 09:38) mosta

Okay, that is due to the static app being served via our frontend Nginx instance. To work around this, you can either create your own private Nginx instance running under a Custom Application (listening on port) or; You could use a "Static/CGI/PHP" application instead of a Static Only application, and set the header in a .htaccess file.

(19 Nov, 21:26) NickR ♦♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×124
×3

question asked: 18 Nov, 16:28

question was seen: 81 times

last updated: 19 Nov, 21:26

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM