Search code examples
pythonapache.htaccessvirtualhostbottle

Apache + Python Bottle : visitor IP always refers to 127.0.0.1


My server handles multiple websites, most of them with Apache, PHP, etc.

But one of them (www.mywebsite.com) uses a Python webserver, which listens on port 8092. Thus this Apache configuration:

<VirtualHost *:80>
  ServerName mywebsite.com
  ServerAlias *.mywebsite.com
  RewriteEngine On
  RewriteRule /(.*)           http://localhost:8092/$1 [P,L]
</VirtualHost>

Now when a user, coming from internet, connects to www.mywebsite.com, it works : Python handles it and everything is okay.

Problem: the IP that I get, in my Python code using bottle with :

ip = request.environ.get('REMOTE_ADDR')

is always 127.0.0.1.

How to get the real IP address of the visitor instead?


Solution

  • Your virtual host is using proxy rewriting. The client connects to apache which opens a proxy connection to your application, rewrites the URL, and proxies the request. There is no real connection between your application and the client.

    Since there is not a direct connection between your application and the client, you cannot get the "real IP" unless you tell apache to send it to your application. The usual approach is to set something like the X-Forwarded-For header. You may have to explicitly use mod_proxy to do this though.