How to get IP when Tornado + nginx

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

How to get IP when Tornado + nginx
How to get the Python version
How to add sudo when debugging
How to get started with Scrapy
How to get started with Python
How to get started with Django
How to log in to Docker + NGINX
How to get parent id with sqlalchemy
How to get rid of long comprehensions
How to get a stacktrace in python
To get a local IP address programmatically
How to get colored output to the console
How to get started with laravel (Linux)
How to start Python (Flask) when EC2 starts
[Django] How to resolve errors when installing mysqlclient
How to get results from id in Celery
[Django] How to get data by specifying SQL.
How to get help in an interactive shell
How to deal with SessionNotCreatedException when using Selenium
How to fix when Terminal input becomes abnormal
How to get article data using Qiita API
How to remember when you forget a word
How to get dictionary type elements of Python 2.7
How to get the files in the [Python] folder
How to get PyCPX installed (OSX Yosemite, Canopy)
[Rails] How to get location information using Geolocation API
How to get the variable name itself in python
How to get multiple model objects randomly in Django
How to get the number of digits in Python
[Ansible] How to use SSH password authentication when executing ansible
Memo of how to use properly when combining pandas.DataFrame
How to get a logged-in user with Django's forms.py
How to disable the reference feature when outputting PyYaml
How to access with cache when reading_json in pandas
How to get more than 1000 data with SQLAlchemy + MySQLdb
How to exit when using Python in Terminal (Mac)
How to get mouse wheel verdict with Python curses
How to write faster when using numpy like deque
What I was addicted to when using Python tornado
[Linux] How to put your IP in a variable
How to get RGB and HSV histograms in OpenCV
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use shogun
How to use Pandas 2
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header
How to restart gunicorn
How to install python
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON