Error enabling CORS

Hello guys, recently I set my domain in the server, originally I was using the IP and everything was working great, now, after set my domain, when I connect my local development environment (a react app) to the server I get the classic CORS error:

Access to XMLHttpRequest at ‘https://api./api/v0/auth/login’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.

checking some post I changed my caddy file to

api.<URL> {
    header Access-Control-Allow-Methods "POST,PATCH,PUT,DELETE, GET, OPTIONS"
    header Access-Control-Allow-Headers "*"
    header Access-Control-Allow-Origin *
    header Cache-Control max-age=3600
    reverse_proxy localhost:1200
}

in my understand it must be enough in order to enable CORS, also my webserver has enabled CORS

api.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
	}))

these are the caddy logs

root@xxxx-01:~# 2021/03/31 20:02:02.117	ERROR	http.log.error	dial tcp 127.0.0.1:1200: connect: connection refused	{"request": {"remote_addr": "xxx:20259", "proto": "HTTP/2.0", "method": "OPTIONS", "host": "api.<url>", "uri": "/api/v0/auth/register", "headers": {"Sec-Fetch-Dest": ["empty"], "Access-Control-Request-Headers": ["content-type"], "Origin": ["http://localhost:3000"], "Sec-Fetch-Mode": ["cors"], "Sec-Fetch-Site": ["cross-site"], "Accept-Encoding": ["gzip, deflate, br"], "Accept-Language": ["es-419,es;q=0.9,es-ES;q=0.8,en;q=0.7,en-GB;q=0.6,en-US;q=0.5"], "Accept": ["*/*"], "Access-Control-Request-Method": ["POST"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4437.0 Safari/537.36 Edg/91.0.831.1"], "Referer": ["http://localhost:3000/"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "api.rockets-test.ml"}}, "duration": 0.000448227, "status": 502, "err_id": "set557z0w", "err_trace": "reverseproxy.statusError (reverseproxy.go:817)"}
2021/03/31 20:02:38.409	ERROR	http.log.error	dial tcp 127.0.0.1:1200: connect: connection refused	{"request": {"remote_addr": "186.167.250.106:36246", "proto": "HTTP/2.0", "method": "OPTIONS", "host": "api.rockets-test.ml", "uri": "/api/v0/auth/login", "headers": {"Access-Control-Request-Method": ["POST"], "Access-Control-Request-Headers": ["content-type"], "Origin": ["http://localhost:3000"], "Sec-Fetch-Dest": ["empty"], "Referer": ["http://localhost:3000/"], "Accept-Encoding": ["gzip, deflate, br"], "Accept": ["*/*"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63"], "Sec-Fetch-Mode": ["cors"], "Sec-Fetch-Site": ["cross-site"], "Accept-Language": ["es-419,es;q=0.9,es-ES;q=0.8,en;q=0.7,en-GB;q=0.6,en-US;q=0.5"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "api.rockets-test.ml"}}, "duration": 0.000473975, "status": 502, "err_id": "qypyxxcre", "err_trace": "reverseproxy.statusError (reverseproxy.go:817)"}

I’m curious about that reverseproxy.statusError (reverseproxy.go:817)

do you know which could be the issue here? any help will be appreciated…

thank you so much guys, have a great day…

1. Caddy version (caddy version):

v2.4.0-beta.1 h1:Ed/tIaN3p6z8M3pEiXWJL/T8JmCqV62FrSJCHKquW/I=

2. How I run Caddy:

caddy run

a. System environment:

ubuntu

b. Command:

paste command here

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

api. {
header Access-Control-Allow-Methods “POST,PATCH,PUT,DELETE, GET, OPTIONS”
header Access-Control-Allow-Headers “*”
header Access-Control-Allow-Origin *
header Cache-Control max-age=3600
reverse_proxy localhost:1200
}

paste config here, replacing this text
use `caddy fmt` to make it readable
DO NOT REDACT anything except credentials
or helpers will be sad

3. The problem I’m having:

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

This just means Caddy couldn’t connect to your backend app. Are you sure you have it running on port 1200? Is it running on the same machine as Caddy?

You didn’t completely fill out the help topic template, so it’s unclear how you’re actually running Caddy, are you running it as a systemd service, or in Docker? Because that affects the proxy address you need to use.

This topic was automatically closed after 30 days. New replies are no longer allowed.