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

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.