Health Check with code 301

Hi, I’m having difficulty in getting HTTP 200 OK from health check.

My stack: Django with Django Rest Framework.

This is my http_service.checks:

[[http_service.checks]]
grace_period = "60s"
interval = "60s"
method = "get"
path = "/v1/"
protocol = "http"
timeout = "60s"

What I get is:

INFO: 172.19.5.9:33690 - "GET /v1/ HTTP/1.1" 301 Moved Permanently

The funny thing is, I try to disable the health check, deploy, it’s deployed normally and get HTTP 200 OK response code from the same endpoint (“/v1/”). Anyone can help? Thanks.

This is the response if I try to deploy without health check:

INFO: 172.16.1.50:50078 - "GET /v1/ HTTP/1.1" 200 OK

As you can see it returns response with status code 200.

That’s weird indeed. But the redirection is coming within your Django app for sure. Can you reproduce a 301 on that route when running your app locally using curl? Maybe some headers in the request trigger it?

Can you log incoming requests in your app and output the sent headers? Maybe that will give you some more insights.

Strange! Can you add some additional logging to print relevant stuff like request object? Then compare the request object from the request made by Fly and one made by yourself.

Hi, thanks for the response. As what’s suggested, I create a middleware in my Django app. I placed it as the first middleware so that it will definitely be triggered.

The log from the health check:

2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] URL Path: /v1/
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] REQUEST ID: b69e9db1ae2dbf641d23510c1f5ddee3
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] REQUEST HEADERS:
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] Host: 172.19.135.202:9000
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] User-Agent: Consul Health Check
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] Accept: text/plain, text/*, */*
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] Accept-Encoding: gzip
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] Connection: close
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] RESPONSE HEADERS:
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] Content-Type: text/html; charset=utf-8
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] Location: https://172.19.135.202:9000/v1/
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] X-Content-Type-Options: nosniff
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] Referrer-Policy: strict-origin-when-cross-origin
2023-10-04T10:20:05.996 app[3d8d34df190d89] sin [info] Cross-Origin-Opener-Policy: same-origin
2023-10-04T10:20:05.997 app[3d8d34df190d89] sin [info] INFO: 172.19.135.201:40344 - "GET /v1/ HTTP/1.1" 301 Moved Permanently

Here is the request log if I do request manually to the app without health check:

2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] URL Path: /v1/
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] REQUEST ID: 1526a7c857bef749116d9905f24f13bc
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] REQUEST HEADERS:
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] User-Agent: PostmanRuntime/7.33.0
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Accept: */*
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Postman-Token: f4eddd51-22b8-4eee-8c3f-98f758c1ef56
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Host: scalev-api.fly.dev
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Accept-Encoding: gzip, deflate, br
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] X-Request-Start: t=1696415020301145
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Fly-Client-Ip: 118.99.110.161
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] X-Forwarded-For: 118.99.110.161, 66.241.125.77
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Fly-Forwarded-Proto: https
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] X-Forwarded-Proto: https
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Fly-Forwarded-Ssl: on
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] X-Forwarded-Ssl: on
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Fly-Forwarded-Port: 443
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] X-Forwarded-Port: 443
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Fly-Region: sin
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Fly-Request-Id: 01HBX3DX8D1CD3PS6D6GR6CE1S-sin
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Via: 1.1 fly.io
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Fly-Traceparent: 00-bd619ccbf7324a96d457775d888dee09-075cda210f3a0d53-00
2023-10-04T10:23:41.246 app[178113d1a24d68] sin [info] Fly-Tracestate:
2023-10-04T10:23:41.396 app[178113d1a24d68] sin [info] RESPONSE HEADERS:
2023-10-04T10:23:41.397 app[178113d1a24d68] sin [info] Content-Type: application/json
2023-10-04T10:23:41.397 app[178113d1a24d68] sin [info] Vary: Accept, Origin
2023-10-04T10:23:41.397 app[178113d1a24d68] sin [info] Allow: GET, HEAD, OPTIONS
2023-10-04T10:23:41.397 app[178113d1a24d68] sin [info] X-Frame-Options: DENY
2023-10-04T10:23:41.397 app[178113d1a24d68] sin [info] Content-Length: 15
2023-10-04T10:23:41.397 app[178113d1a24d68] sin [info] X-Content-Type-Options: nosniff
2023-10-04T10:23:41.397 app[178113d1a24d68] sin [info] Referrer-Policy: strict-origin-when-cross-origin
2023-10-04T10:23:41.397 app[178113d1a24d68] sin [info] Cross-Origin-Opener-Policy: same-origin
2023-10-04T10:23:41.398 app[178113d1a24d68] sin [info] INFO: 172.16.13.202:41442 - "GET /v1/ HTTP/1.1" 200 OK

Could you tell what’s wrong with my config from the log above? @brian @jeroen1

The health check seems to run at port 9000 whereas with postman you use port 41442?

Don’t think that’s related. The Postman request has the APP_NAME.fly.dev domain for the host.

Do you have any kind of HTTP to HTTPS redirect middleware enabled. I noticed the Location response header for the health check request is set to:

Location: https://172.19.135.202:9000/v1/

If your Django has something that automatically redirects http:// to https://, that could be the problem. The proxy is going to be making a http:// request. If that’s the case, maybe you could either make the redirect only apply to requests with APPNAME.fly.dev or your custom domain as the host? Alternatively, maybe make a health check endpoint that bypasses the HTTP to HTTPS redirect?

Ah yeah spot on @brian did not notice the s

1 Like

Thanks! Yeah I have configuration in my that redirect http to https. I will remove it since it will be handled by fly anyway.

1 Like

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