Enabling CORS on DigitalOcean / Docker install


(Patrick Walters) #1

I’m sure there is something I am missing but I have a DigitalOcean install of Discourse and I am hosting it from a subdomain of my site (forums.mysite.com) and from my main site I want to pull in the comments.

So I make a call to my Discourse installation -

http://forums.mysite.com/latest.json

But the problem is Modern CORS Supported browsers throw no ALLOW-ACCESS-CONTROL-ORIGIN header error. I went in to /var/docker/containers and edited the app.yml file and added

DISCOURSE_ENABLE_CORS: true
DISCOURSE_CORS_ORIGIN: '*'

As per this post - How to enable Cross-origin Resource Sharing with docker

But when I do

./launcher stop app 
./launcher bootstrap app
./launcher start app

And make the call again I don’t get the response headers. How can I check whether Discourse was bootstrapped with the new properties properly? Do I need to change some other configuration to allow my main domain mysite.com to get the OPTIONS to return with the proper headers?

Thanks in advance if anyone can help with this.


(Arpit Jalan) #2

I think you just stopped the container instead of destroying it, here’s the difference between two:

stop:       Stop a running container
destroy:    Stop and remove a container

So, if you would have done:

./launcher destroy app 
./launcher bootstrap app
./launcher start app

It would have taken configuration changes into account.

Also you can use rebuild, if you want to destroy, bootstrap and start your app, wrapped in a single command:

rebuild:    Rebuild a container (destroy old, bootstrap, start new)

So running:

./launcher rebuild app

Will take your configuration settings into account and start your app.


(Sam Saffron) #3

have you tried adding ?bla to the end of the url. are you certain headers are not cached incorrectly locally.


(Patrick Walters) #4

I tried destroying and restarting but no dice. There doesn’t appear to be a rebuild command that is available though, there is a restart though but sounds like it does the same thing as stop and start.


(Patrick Walters) #5

Yes I did a bunch of cache clearing and busting but no dice. I think there is something else going on (I don’t think it is a problem in Discourse) because it doesn’t appear to return anything from the OPTIONS call.

The only reason I even ask here is because I don’t have any SysAdmin or anyone else to ask so I was hoping someone had ran into this before with DigitalOcean / Docker.


(Jeff Atwood) #6

If rebuild is not available this implies your docker is out of date. Do a git pull in the /var/docker folder.


(Patrick Walters) #7

So I did a git pull on the docker folder, did a rebuild, but it failed the first time. Went back and did it again and for whatever reason it is all working now. Thanks for the help everyone.


(Patrick Walters) #8

And it rears it’s ugly head again - without explanation everything was working fine last night but now this morning with nothing at all changed it is no longer including the headers for CORS anymore…

If anyone cares to see the problem in action http://sotaexchange.com/ click on Housing on the left and then ‘Lot’ in it’s sub menu with your console open. When it loads the three sub-items it will make a call to get comments and fail. I switched to using the IP address of the DigitalOcean Docker Discourse instance instead of the sub-domain and it was working fine last night, no idea what could have sparked the change…

Any ideas would be greatly appreciated. (And thanks for all of the previous replies as well)

Note It appears to be returning a 301 Moved Permanently response now?


(Patrick Walters) #9

This is so odd - it has randomly started working again - I have no idea why it is turning off and on -

Access-Control-Allow-Credentials:true
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:http://sotaexchange.com
Access-Control-Expose-Headers:
Access-Control-Max-Age:1728000

These are the headers being returned now.


(Sam Saffron) #10

No idea how this has ever worked, tried to get it working today, got frustrated, and just rewrote it in functional way.

https://github.com/discourse/discourse/commit/46c406360d7ac851a3f4977f89178c5480cc063b


(Jeff Atwood) #11

Because GEMS!


(Patrick Walters) #12

Today is my birthday and Sam fixed this issue. Basically Sam did this for my birthday. Thank you for the birthday present, Sam.