Score:0

how do I configure nginx to send user ip in response headers

br flag

I have an nginx server I installed on ubuntu 20.04 server. I'm trying to get the user IP address but nginx doesn't seem to be configured to pass it. when looking at access logs /var/log/nginx/access.log - I can see the correct IP address.

I'm testing this issue with a basic flask program below:

from flask import Flask,jsonify,request

app = Flask (__name__)
@app.route('/')
def home():
    return jsonify({"request.remote_addr" : request.remote_addr })
if __name__ == "__main__":
    app.run(host='0.0.0.0')

nginx configuration file minus SSL lines for this is:

server{
     server_name test.testweb.com;
     location / {
         proxy_pass http://unix:/home/flask/test/test.sock;
     }
}

when I run this with flask dev server, it works as expected. when I run it on nginx/gunicorn, I get an empty string for IP address.
what configuration is required on nginx to pass the user IP address to the server?

Ivan Shatsky avatar
gr flag
Try `proxy_set_header X-Forwaded-For $proxy_add_x_forwarded_for;`
afshin avatar
br flag
tried it - didn't work at first. changed your suggestion to the following: add_header Access-Control-Allow-Origin *; add_header X-Forwaded-For $proxy_add_x_forwarded_for; it worked - first time I've seen user ip passed in the headers in the last 2 day. thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.