When getting a client IP address in Tornado
#!/usr/bin/python
# -*- coding: utf-8 -*-
from tornado import web,ioloop
class IndexHandler(web.RequestHandler):
    def get(self):
        ip = self.request.remote_ip
        self.write(ip)
handlers = [
    (r'/', IndexHandler),
]
settings = dict(
    debug = True,
)
app = web.Application(handlers, **settings)
app.listen(8000)
ioloop.IOLoop.instance().start()
You can get it at.
But with nginx
http {
    upstream example.server.com {
        server 127.0.0.1:8000;
    }
    server {
        listen 80;
        server_name example.server.com;
        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass example.server.com;
            proxy_next_upstream error;
        }
    }
}
If you run tornado with the above settings, the IP will be 127.0.0.1.
So tornado's IndexHandler
class IndexHandler(web.RequestHandler):
    def get(self):
        ip = self.request.headers['X-Real-IP']
        self.write(ip)
If you give it as, I was able to get it safely.
Since nginx behaves like a load balancer, it's natural when you think about it, but at first I didn't notice it and got hooked a little. .. ..
Recommended Posts