Question

How can I set CORS headers for a static site?

Hi, I’m having CORS issues. I have a backend running in digitalocean as an App and another App running the frontend as a static site. When I’m trying to access the backend from the frontend I get a CORS error. I tried setting my backend url in the app spec as follows:

ingress:
  rules:
  - component:
      name: guest-list-frontend
    cors:
      allow_origins:
      - prefix: https://my-backend-app.ondigitalocean.app
    match:
      path:
        prefix: /

Where the url mentioned in prefix is the URL of my backend. But I still get this issue. Can anybody help me out?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
January 27, 2024

Hey!

To set CORS headers for your static site on DigitalOcean App Platform, you need to configure CORS policies for your app’s resources. Here’s a brief guide on how to do it:

  1. Go to your DigitalOcean control panel at cloud.digitalocean.com/apps.
  2. Click on your app, then select ‘Settings’.
  3. Click on the resource (like your backend app) for which you want to edit CORS policies.
  4. Under ‘HTTP Request Routes’, click ‘Edit’, then ‘Configure CORS’.
  5. In the ‘Configure CORS’ window, you can add an origin and specify the match type (Exact, Prefix, or Regex).

This approach will help you set the Access-Control-Allow-Origin header based on the match type you select. Keep in mind that the ‘Exact’ match type will set the header only if the client’s origin exactly matches the value you provide, while ‘Prefix’ and ‘Regex’ offer more flexibility in matching the origin.

For more detailed instructions, you can refer to DigitalOcean’s documentation on configuring CORS policies.

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel