0

I am running into slightly annoying issues setting up Cloudflare's custom error page functionality, so I would like to figure out how I can proxy_pass to cloudflare only when my server does not return an error code as I really only want to use these in order to show a custom page I create as opposed to Cloudflare's default ones.

Here is the nginx sites-available config:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

upstream app {
    server 127.0.0.1:8000;
    # server CLOUDLFARE_ADDRESS_FOR_PROXY
}

server {
    listen 443 ssl;
    server_name example.com www.example.com;

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://app;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Host $server_name;
                proxy_headers_hash_max_size 512;
                proxy_headers_hash_bucket_size 128;
                # can this work somehow?
                # proxy_next_upstream [non-error] CLOUDLFARE_ADDRESS_FOR_PROXY
        }

        location ~*  \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
                expires 1d;
        }

        error_page 500 502 503 504 /custom_50x.html;
        location = /custom_50x.html {
                        root /usr/share/nginx/html;
                        internal;
        }
        ssl_certificate /etc/letsencrypt/live/exactestate.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/exactestate.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

What I want is to use Cloudflare as a proxy only if my server throws no error codes, how can I accomplish this?

DNS settings using the cloudlfare interface are more powerful than server blocks I suppose so I would like a way to do this using Nginx's server block as opposed to a full DNS Proxy.

ViaTech
  • 101
  • 1
  • That doesn't make sense. CloudFlare must always be your proxy, otherwise it can't possibly show a cached version of your page. – Michael Hampton Aug 17 '20 at 18:09
  • @MichaelHampton Let's say I do not really care about caching a version of my page and more or less only care about showing a custom error page on error responses, I edited the first paragraph of the question to be a little more clear. – ViaTech Aug 17 '20 at 18:14
  • nginx is perfectly capable of serving custom error pages when your app returns an error. Why aren't you just doing that? – Michael Hampton Aug 17 '20 at 18:16
  • I seem to only be able to show custom pages, as I have setup in my config posted above, when I am not proxying through cloudflare, when I have the proxy live cloudflare takes control of the error page automatically. I would like to keep the functionality cloudflare provides but I also, even with support, cannot get their custom pages working so I am trying to find a way to both use cloudflare and my custom error pages running from the nginx config – ViaTech Aug 17 '20 at 18:20
  • Having CloudFlare serve your own origin-generated error pages requires a paid Enterprise subscription though. – Michael Hampton Aug 17 '20 at 18:24

0 Answers0