2

I am trying to detect the visitors country. I have the geoip option checked in the cloudflare dash and it adds a CF-IPCountry header to request headers but I am unable to pass this to my backend app through the nginx proxy. What am I doing wrong?

location / {
    # forward application requests to the gunicorn server
    proxy_pass http://localhost:8080;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header CF_IPCountry $http_cf_ipcountry; #this line is the culprit
 }

EDIT: The backend does not see this header. I am using flask and made a route to output all the request headers.

@app.route('/headers')
def header():
    headers = request.headers
    header_list = []
    for h in headers:
        header_list.append(h)
    return jsonify(header_list)
jinyus
  • 153
  • 1
  • 2
  • 6
  • Explain things more. The backend does not see this header at all? Sees it but with an unexpected value? Did you double check that you indeed receive this header at your Nginx instance? – Patrick Mevzek Jul 18 '18 at 22:54
  • I updated my question. The backend is not seeing the header. How would I check if nginx is receiving this header? – jinyus Jul 18 '18 at 23:48
  • "How would I check if nginx is receiving this header?" either more logging or sniffing the traffic arriving, if it is HTTP – Patrick Mevzek Jul 19 '18 at 00:02

1 Answers1

2

By default, nginx ignores HTTP headers which contain underscores.

You have:

    proxy_set_header CF_IPCountry $http_cf_ipcountry; #this line is the culprit

But this should be:

    proxy_set_header CF-IPCountry $http_cf_ipcountry; #this line is the culprit
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972