Report this

What is the reason for this report?

How can I set CORS headers for a static site?

Posted on January 24, 2024

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?



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!

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.

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

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.