Apache + Python Bottle : visitor IP always refers to 127.0.0.1

三世轮回 提交于 2019-12-08 03:15:38

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/36954669/apache-python-bottle-visitor-ip-always-refers-to-127-0-0-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!