Caddy Front end redirect Back end api requests

1. The problem I’m having:

Hello everyone. Am new to caddy. I have a config of react(vite) frontend application. And a golang Backend. I have a strange behaviour. i am able to login to my application (login use (http request) ) but every other requests that uses the middleware is redirected to a blank page and i have a 304 response . its working loccally with docker compose. but i only have this behavior after configuring caddy .
i dont know if its the prefligh request issue. or a Cors problem.
its strange because sometimes i have the result and i can see the page. but it dissappear immediately and redirec to 304. and display a blank screen.
English is not my first language by the way.
Thanks in advance

2. Error messages and/or full log output:

rc/scenes/widgets/PostsWidget.jsx","headers":{"X-Forwarded-Host":["place2connect.com"],"Sec-Fetch-Mode":["cors"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"],"Accept-Encoding":["gzip, deflate, br"],"Sec-Fetch-Dest":["script"],"If-None-Match":["W/\"e76-erm56GaPJevAdoM7HvORDgImtGg\""],"X-Forwarded-For":["91.162.221.146"],"X-Forwarded-Proto":["https"],"Accept-Language":["en-US,en;q=0.5"],"Referer":["https://place2connect.com/src/scenes/profilePage/index.jsx"],"Accept":["*/*"],"Sec-Fetch-Site":["same-origin"],"Te":["trailers"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Access-Control-Allow-Origin":["*"],"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"],"Keep-Alive":["timeout=5"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223852.776393,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.005037852,"request":{"remote_ip":"91.162.221.146","remote_port":"40116","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/src/scenes/widgets/UserWidget.jsx","headers":{"Referer":["https://place2connect.com/src/scenes/profilePage/index.jsx"],"Sec-Fetch-Dest":["script"],"X-Forwarded-For":["91.162.221.146"],"X-Forwarded-Proto":["https"],"X-Forwarded-Host":["place2connect.com"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"],"Accept-Language":["en-US,en;q=0.5"],"Sec-Fetch-Site":["same-origin"],"Te":["trailers"],"Accept-Encoding":["gzip, deflate, br"],"Sec-Fetch-Mode":["cors"],"If-None-Match":["W/\"340a-sQptyUnqj7O6hy+VtiQAMso21Gc\""],"Accept":["*/*"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Keep-Alive":["timeout=5"],"Access-Control-Allow-Origin":["*"],"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223852.7777565,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.003520041,"request":{"remote_ip":"91.162.221.146","remote_port":"40116","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/src/scenes/loginPage/Form.jsx","headers":{"X-Forwarded-Proto":["https"],"X-Forwarded-Host":["place2connect.com"],"Accept-Language":["en-US,en;q=0.5"],"Sec-Fetch-Dest":["script"],"Sec-Fetch-Mode":["cors"],"Sec-Fetch-Site":["same-origin"],"Accept-Encoding":["gzip, deflate, br"],"Te":["trailers"],"X-Forwarded-For":["91.162.221.146"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"],"Referer":["https://place2connect.com/src/scenes/loginPage/index.jsx"],"If-None-Match":["W/\"3222-UB8N4fBzGDDqKaddZH+wHpgIVCg\""],"Accept":["*/*"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"],"Keep-Alive":["timeout=5"],"Access-Control-Allow-Origin":["*"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223852.7782834,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.00531227,"request":{"remote_ip":"91.162.221.146","remote_port":"40116","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/src/scenes/widgets/AdvertWidget.jsx","headers":{"Accept":["*/*"],"Accept-Encoding":["gzip, deflate, br"],"Referer":["https://place2connect.com/src/scenes/homePage/index.jsx"],"Sec-Fetch-Mode":["cors"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"],"Accept-Language":["en-US,en;q=0.5"],"If-None-Match":["W/\"1221-HCgrwlR/1ss5VuzE5SNIYHgzDkw\""],"Te":["trailers"],"X-Forwarded-Proto":["https"],"X-Forwarded-Host":["place2connect.com"],"Sec-Fetch-Dest":["script"],"Sec-Fetch-Site":["same-origin"],"X-Forwarded-For":["91.162.221.146"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Keep-Alive":["timeout=5"],"Access-Control-Allow-Origin":["*"],"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223852.8008473,"logger":"http.handlers.reverse_proxy","msg":"selected upstream","dial":"place2connect-ui:3000","total_upstreams":1}
caddy-service      | {"level":"debug","ts":1684223852.8025815,"logger":"http.handlers.reverse_proxy","msg":"selected upstream","dial":"place2connect-ui:3000","total_upstreams":1}
caddy-service      | {"level":"debug","ts":1684223852.8038332,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.000770139,"request":{"remote_ip":"91.162.221.146","remote_port":"40116","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/src/components/UserImage.jsx","headers":{"X-Forwarded-Host":["place2connect.com"],"If-None-Match":["W/\"9be-R/rTsovBwl4J8f/piCtz50tLUAc\""],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"],"Accept":["*/*"],"Accept-Encoding":["gzip, deflate, br"],"Accept-Language":["en-US,en;q=0.5"],"Sec-Fetch-Dest":["script"],"Sec-Fetch-Site":["same-origin"],"Referer":["https://place2connect.com/src/scenes/widgets/MyPostWidget.jsx"],"Sec-Fetch-Mode":["cors"],"Te":["trailers"],"X-Forwarded-For":["91.162.221.146"],"X-Forwarded-Proto":["https"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Access-Control-Allow-Origin":["*"],"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"],"Keep-Alive":["timeout=5"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223852.8044891,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.003014746,"request":{"remote_ip":"91.162.221.146","remote_port":"40116","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/src/components/FlexBetween.jsx","headers":{"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"],"Referer":["https://place2connect.com/src/scenes/widgets/MyPostWidget.jsx"],"Sec-Fetch-Dest":["script"],"X-Forwarded-For":["91.162.221.146"],"X-Forwarded-Host":["place2connect.com"],"X-Forwarded-Proto":["https"],"Te":["trailers"],"Accept":["*/*"],"Accept-Language":["en-US,en;q=0.5"],"Accept-Encoding":["gzip, deflate, br"],"Sec-Fetch-Site":["same-origin"],"If-None-Match":["W/\"128-J8o7q7WRuM0MpKnN9nUDoATqfk0\""],"Sec-Fetch-Mode":["cors"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Access-Control-Allow-Origin":["*"],"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"],"Keep-Alive":["timeout=5"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223852.8052156,"logger":"http.handlers.reverse_proxy","msg":"selected upstream","dial":"place2connect-ui:3000","total_upstreams":1}
caddy-service      | {"level":"debug","ts":1684223852.8063335,"logger":"http.handlers.reverse_proxy","msg":"selected upstream","dial":"place2connect-ui:3000","total_upstreams":1}
caddy-service      | {"level":"debug","ts":1684223852.80729,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.001744855,"request":{"remote_ip":"91.162.221.146","remote_port":"40116","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/src/components/WidgetWrapper.jsx","headers":{"Referer":["https://place2connect.com/src/scenes/widgets/MyPostWidget.jsx"],"X-Forwarded-For":["91.162.221.146"],"X-Forwarded-Proto":["https"],"Te":["trailers"],"Accept":["*/*"],"Sec-Fetch-Mode":["cors"],"If-None-Match":["W/\"168-pNiZtIbTMzW2qDIMxvhlQY924hQ\""],"Sec-Fetch-Dest":["script"],"X-Forwarded-Host":["place2connect.com"],"Accept-Language":["en-US,en;q=0.5"],"Accept-Encoding":["gzip, deflate, br"],"Sec-Fetch-Site":["same-origin"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Access-Control-Allow-Origin":["*"],"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"],"Keep-Alive":["timeout=5"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223852.8078308,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.001192958,"request":{"remote_ip":"91.162.221.146","remote_port":"40116","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/src/components/Friend.jsx","headers":{"Sec-Fetch-Site":["same-origin"],"Te":["trailers"],"X-Forwarded-Host":["place2connect.com"],"Sec-Fetch-Dest":["script"],"Accept":["*/*"],"Accept-Language":["en-US,en;q=0.5"],"Accept-Encoding":["gzip, deflate, br"],"X-Forwarded-For":["91.162.221.146"],"X-Forwarded-Proto":["https"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"],"Referer":["https://place2connect.com/src/scenes/widgets/FriendListWidget.jsx"],"Sec-Fetch-Mode":["cors"],"If-None-Match":["W/\"18ed-LOsj2KK4GfOSCnuVjCQBxQQcsLI\""]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Keep-Alive":["timeout=5"],"Access-Control-Allow-Origin":["*"],"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223852.8450763,"logger":"http.handlers.reverse_proxy","msg":"selected upstream","dial":"place2connect-ui:3000","total_upstreams":1}
caddy-service      | {"level":"debug","ts":1684223852.846754,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.001055262,"request":{"remote_ip":"91.162.221.146","remote_port":"40116","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/src/scenes/widgets/PostWidget.jsx","headers":{"If-None-Match":["W/\"2593-zdjEyJBiKJQo6TZoawWgSNmnIwQ\""],"Accept-Language":["en-US,en;q=0.5"],"Accept-Encoding":["gzip, deflate, br"],"Referer":["https://place2connect.com/src/scenes/widgets/PostsWidget.jsx"],"Sec-Fetch-Mode":["cors"],"Te":["trailers"],"X-Forwarded-For":["91.162.221.146"],"Sec-Fetch-Site":["same-origin"],"X-Forwarded-Host":["place2connect.com"],"Accept":["*/*"],"Sec-Fetch-Dest":["script"],"X-Forwarded-Proto":["https"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Date":["Tue, 16 May 2023 07:57:32 GMT"],"Connection":["keep-alive"],"Keep-Alive":["timeout=5"],"Access-Control-Allow-Origin":["*"]},"status":304}
caddy-service      | {"level":"debug","ts":1684223853.0201235,"logger":"http.handlers.reverse_proxy","msg":"selected upstream","dial":"place2connect-ui:3000","total_upstreams":1}
caddy-service      | {"level":"debug","ts":1684223853.057576,"logger":"http.handlers.reverse_proxy","msg":"upstream roundtrip","upstream":"place2connect-ui:3000","duration":0.036687882,"request":{"remote_ip":"91.162.221.146","remote_port":"39628","proto":"HTTP/2.0","method":"GET","host":"place2connect.com","uri":"/node_modules/.vite/deps/chunk-QJV3R4PZ.js.map","headers":{"If-None-Match":["W/\"16b615-8KhgXjIFSfaplkzUySli4EpQuRg\""],"Cache-Control":["max-age=0"],"Te":["trailers"],"Accept-Language":["en-US,en;q=0.5"],"Sec-Fetch-Dest":["empty"],"X-Forwarded-Proto":["https"],"Accept":["*/*"],"Sec-Fetch-Site":["cross-site"],"X-Forwarded-Host":["place2connect.com"],"User-Agent":["Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"],"X-Forwarded-For":["91.162.221.146"],"Accept-Encoding":["gzip, deflate, br"],"Sec-Fetch-Mode":["cors"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"h2","server_name":"place2connect.com"}},"headers":{"Access-Control-Allow-Origin":["*"],"Date":["Tue, 16 May 2023 07:57:33 GMT"],"Connection":["keep-alive"],"Keep-Alive":["timeout=5"]},"status":304}

3. Caddy version:

My caddy version is caddy:2.6.4-alpine (image)

4. How I installed and ran Caddy:

a. System environment:

linux and docker

b. Command:

docker compose up

my backend cors setup is
		AllowOrigins: "https://place2connect.com*, http://place2connect.com*",
		// AllowOrigins:     "https://*, http://*",
		AllowHeaders:     "Origin, Content-Type, Accept, Authorization, X-CSRF-Token",
		AllowMethods:     "GET, HEAD, PUT, PATCH, POST, DELETE, OPTIONS",
		AllowCredentials: true,

c. Service/unit/compose file:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

d. My complete Caddy config:

{
    email   contact@owonafx.com
    debug

}

place2connect.com {

    reverse_proxy place2connect-ui:3000
    encode zstd gzip

    @static {
		file
		path *.css *.js *.ico *.gif *.jpg *.jpeg *.png *.svg *.woff
	}
	handle @static {
		header Cache-Control "public, max-age=5184000, must-revalidate"
	}
	handle {
		header Cache-Control "public, max-age=604800, must-revalidate"
	}

    @options {
        method OPTIONS
    }
    header {
        Access-Control-Allow-Origin *
        Access-Control-Allow-Credentials true
        Access-Control-Allow-Methods *
        Access-Control-Allow-Headers *
        defer
    }
    reverse_proxy place2connect-api:8080 {
        header_down -Access-Control-Allow-Origin
    }
    respond @options 204

}

api.place2connect.com {
    reverse_proxy place2connect-api:8080
    root * /srv
    encode zstd gzip
    file_server
}

db.place2connect.com {
    reverse_proxy postgres:5432
}


5. Links to relevant resources:

Please can you explain what’s your problem is, this is point 1

1 Like

I added it

You have reverse_proxy twice in the same site without a matcher to split them. You need to tell Caddy when to use which. A single request can’t go to both your API and the UI.

Same problem here, you have both file_server and reverse_proxy without matchers. Only reverse_proxy will run here because it’s higher on the directive order.

Hi , thanx for your response. i try to edit it as followed. but i still have the same issue.

    email   contact@owonafx.com
    debug

}

place2connect.com {
    
    encode zstd gzip
    reverse_proxy /api/* place2connect-api:8080
    reverse_proxy place2connect-ui:3000
}

api.place2connect.com {
    reverse_proxy /api/* place2connect-api:8080
    root * /srv
    encode zstd gzip
    file_server
}

db.place2connect.com {
    reverse_proxy postgres:5432
}

Am sorry if the config is not correct. Am just starting with caddy. I didnt see a working config with my stack to inspire me.

You’ll need to be more specific. What’s the issue you’re seeing? Show an example with curl -v. Show your logs. Turn on the debug global option for more detailed logs.

Sorry I missed your debug logs earlier.

Status 304 is coming from your proxy upstream, not from Caddy. This means “Not Modified”. There’s no problem there, that just means the browser already has a more up to date version of the file, which the upstream agrees with because of the If-None-Match: W/"16b615-8KhgXjIFSfaplkzUySli4EpQuRg" header.

Thanx . this is strange. but why am i getting a blank sceen if i already have a more up to date version? do you have any idea about how i can avoid it ? thanx btw.

I don’t have anything to suggest because you haven’t really shown any evidence of the problem. A 304 is not a problem, that’s normal. The 304s are not the cause of the page being blank. See 304 Not Modified - HTTP | MDN. The server responding with 304 means the browser should use the previously loaded contents it has in cache for that URL.

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