# Blocking non-cloudflare proxied requests

**URL:** <https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184>\
**Category:** Traefik v2\
**Tags:** docker\
**Created:** [October 20, 2022, 12:31am UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184 "2022-10-20T00:31:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![youngt2](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/youngt2/32/6055_2.png) [@youngt2](https://community.traefik.io/u/youngt2)\
**Post date:** [October 20, 2022, 12:31am UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184/1 "2022-10-20T00:31:42Z")

</div>

Is it possible to not allow connections when typing in hosted IP? For example, in my access log I see a lot of connections with no router shown. When I access my site using the proper url, the correct router is shown.

```auto
35.216.244.6 - - [19/Oct/2022:19:56:31 -0400] "GET /info.php HTTP/1.1" 404 19 "-" "-" 388 "-" "-" 0ms
68.183.38.167 - - [19/Oct/2022:20:01:02 -0400] "GET /ab2g HTTP/1.1" 404 19 "-" "-" 389 "-" "-" 0ms
68.183.38.167 - - [19/Oct/2022:20:01:03 -0400] "GET /ab2h HTTP/1.1" 404 19 "-" "-" 390 "-" "-" 0ms
68.183.38.167 - - [19/Oct/2022:20:01:07 -0400] "GET / HTTP/1.1" 404 19 "-" "-" 391 "-" "-" 0ms
68.183.38.167 - - [19/Oct/2022:20:01:07 -0400] "GET / HTTP/1.1" - - "-" "-" 392 "-" "-" 0ms
my.ip.is.here - - [19/Oct/2022:20:22:53 -0400] "GET /made/up/webpage HTTP/2.0" 404 43 "-" "-" 783 "portainer@docker" "http://172.18.0.45:9000" 18ms
my.ip.is.here - - [19/Oct/2022:20:22:53 -0400] "GET /favicon.ico HTTP/2.0" 404 43 "-" "-" 784 "portainer@docker" "http://172.18.0.45:9000" 8ms

```

I'm guessing the other connections without a router are a bot or something trying `https://ser.ver.ip.here/info.php ` because that is the only way I can recreate log with no router. Going to the IP like this will produce a `404 page not found`.  
I thought this could be avoided using client authentication, but I guess I don't understand, or I'm making it too hard. Is it possible to not allow this type of request to pass cloudflare somehow?

---

<div class="post-metadata">

**Author:** ![cakiwi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cakiwi/32/3205_2.png) [@cakiwi](https://community.traefik.io/u/cakiwi)\
**Post date:** [October 20, 2022, 3:08pm UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184/2 "2022-10-20T15:08:00Z")

</div>

You can try using [strict-sni-checking](https://doc.traefik.io/traefik/https/tls/#strict-sni-checking)

It won't stop someone directly connecting and then sending a server\_name in the TLS handshake but it will greatly reduce what you are seeing.

To allow **only** proxied requests you'd have to use some other restriction.

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [October 20, 2022, 6:07pm UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184/3 "2022-10-20T18:07:04Z")

</div>

Even when using client authentication, you will still see entries in the Traefik access log. When using browser-supported user/pass, you would usually first see a request with error 401 ("Unauthorized"), telling the browser to ask for user/pass, then 403 ("Forbidden") when wrong user/pass is supplied.

The Internet is a the new wild west, you will continuously see requests to your server using IP only, actors are permanently scanning the net for vulnerabilities or unprotected data.

---

<div class="post-metadata">

**Author:** ![youngt2](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/youngt2/32/6055_2.png) [@youngt2](https://community.traefik.io/u/youngt2)\
**Post date:** [October 25, 2022, 12:52am UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184/4 "2022-10-25T00:52:39Z")

</div>

I have strict-sni-checking in my tls options doesn't seem to help.

```auto
tls:
  options:
    modern:
      minVersion: VersionTLS13
      cipherSuites:
        - ...
      curvePreferences:
        - ...
      sniStrict: true
      preferServerCipherSuites: true
      clientAuth:
        caFiles: /etc/cf_origin_pull_ca.pem
        clientAuthType: RequireAndVerifyClientCert

```

I guess what traefik is doing is correct. It won't allow connections from non-cloudflare. I think I was just expecting it to not log or even be non-existant to those types of connections. It just causes a lot of noise in fail2ban.

---

<div class="post-metadata">

**Author:** ![cakiwi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cakiwi/32/3205_2.png) [@cakiwi](https://community.traefik.io/u/cakiwi)\
**Post date:** [October 27, 2022, 11:36am UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184/5 "2022-10-27T11:36:40Z")

</div>

Do you have a non-tls router too? You should not be seeing any request with strict sni as the connection does not get past the handshake.

It is conceivable they are sending the host in the handshake to the , but seems like too much effort for this scanning.

---

<div class="post-metadata">

**Author:** ![youngt2](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/youngt2/32/6055_2.png) [@youngt2](https://community.traefik.io/u/youngt2)\
**Post date:** [October 31, 2022, 12:51am UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184/6 "2022-10-31T00:51:38Z")

</div>

@cakiwi I'm a bit unsure about that honestly. The TLS portion of my docker-compose is as follows:

```auto
      - --accesslog.filepath=/var/log/access.log
      - --accesslog.filters.statuscodes=400-499
      - --accesslog.fields.names.StartUTC=drop
      - --accesslog.fields.headers.names.RouterName=keep
      - --log.level=DEBUG #DEBUG INFO WARN ERROR PANIC FATAL
      - --entrypoints.web.address=:80
      - --entrypoints.web.http.redirections.entrypoint.to=websecure
      - --entrypoints.websecure.address=:443
      - --entrypoints.websecure.http.tls.certresolver=cloudflare
      - --entrypoints.websecure.http.tls.options=modern@file
      - --entrypoints.websecure.http.tls.domains[0].main=my.site
      - --entrypoints.websecure.http.tls.domains[0].sans=*.my.site

```

My services have labels that look generally like this:

```auto
    labels:
      - traefik.enable=true
      - traefik.http.routers.whoami.rule=Host(`$DOMAINNAME`,`www.$DOMAINNAME`)
      - traefik.http.routers.whoami.entrypoints=websecure
      - traefik.http.routers.whoami.middlewares=secHeaders@file #,authelia-mw@file
      - traefik.http.routers.whoami.tls=true

```

but not every service has the `tls=true` ATM. Do I need to add the specific line `- traefik.http.routers.whoami.tls=true` to each service or can I somehow add it to the websecure so every service uses that. The TLS documentation is a bit confusing to me and I'm unsure if that specific line is redundant when TLS is defined on the entrypoint.

---

<div class="post-metadata">

**Author:** ![mloiseleur](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mloiseleur/32/4987_2.png) [@mloiseleur](https://community.traefik.io/u/mloiseleur)\
**Post date:** [October 31, 2022, 8:54am UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184/7 "2022-10-31T08:54:26Z")

</div>

You can restrict more with [IPWhiteList middleware](https://doc.traefik.io/traefik/middlewares/http/ipwhitelist/).

It's [not dynamic yet](https://github.com/traefik/traefik/issues/4145), but you can generate it dynamically with a (bash) cronjob like this:

```bash
IPS=$(curl https://www.cloudflare.com/ips-v4 --fail) &&
IPSLEN=$(echo $IPS | wc -w) && test $IPSLEN -gt 2 && \
cat <<EOF > /tmp/cloudflare-ips.yaml
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: cloudflare-ipwhitelist
  namespace: traefik
spec:
  ipWhiteList:
    ipStrategy:
      depth: 0
    sourceRange:
$(for ip in $IPS; do echo " - $ip"; done)
EOF

kubectl -n $${NAMESPACE} delete middleware cloudflare-ipwhitelist || echo 1 && \
kubectl -n $${NAMESPACE} apply -f /tmp/cloudflare-ips.yaml

```

As an alternative, you can also use [Authenticated Origin Pull](https://developers.cloudflare.com/ssl/origin-configuration/authenticated-origin-pull/).

---

<div class="post-metadata">

**Author:** ![youngt2](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/youngt2/32/6055_2.png) [@youngt2](https://community.traefik.io/u/youngt2)\
**Post date:** [October 31, 2022, 2:40pm UTC](https://community.traefik.io/t/blocking-non-cloudflare-proxied-requests/16184/8 "2022-10-31T14:40:42Z")

</div>

I don't think my intent is very clear. I think all of these suggestions are correct for dening requests from random scans of my webservers IPs.

However, after doing more reading I think what I was actually looking to solve is something more like the [444 status code seen on nginx](https://www.webfx.com/web-development/glossary/http-status-codes/what-is-a-444-status-code/). Meaning the request would be _dropped_, rather than _denied_.

I don't think this is possible at the moment in traefik?

[How nginx processes a request](https://nginx.org/en/docs/http/request_processing.html)

> **How to prevent processing requests with undefined server names**
> 
> If requests without the “Host” header field should not be allowed, a server that just drops the requests can be defined:
> 
> ```auto
> server {
> listen 80;
> server_name "";
> return 444;
> }
> 
> ```
> 
> Here, the server name is set to an empty string that will match requests without the “Host” header field, and a special nginx’s non-standard code 444 is returned that closes the connection.
