我為我的燒瓶應用程式設定了 apache 反向代理,一切都在 cloudflare 之后。 我已使用此https://support.cloudflare.com/hc/en-us/articles/200170786-Restoring-original-visitor-IPs恢復原始遠程 IP
在 apache 訪問日志中恢復了 IP,因此可以正常作業,但在我的燒瓶應用程式中,我只能看到 localhost 127.0.0.1。
我還嘗試檢查:
print(request.headers.get('cf-connecting-ip'))
print(request.remote_addr)
但在這兩種情況下,只有 localhost IP。
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 773-646-133
None
127.0.0.1
127.0.0.1 - - [08/Oct/2022 13:28:45] "GET / HTTP/1.1" 200 -
這是我的 apache 配置:
<VirtualHost *:80>
ServerName xxx.xxx
ServerAdmin [email protected]
RemoteIPHeader CF-Connecting-IP
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName xxx.xxx
ServerAdmin [email protected]
SSLCertificateFile /etc/cloudflare/xxx.pem
SSLCertificateKeyFile /etc/cloudflare/xxx.key
Protocols h2 h2c http/1.1
RemoteIPHeader CF-Connecting-IP
Include /etc/letsencrypt/options-ssl-apache.conf
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</IfModule>
有什么辦法,我怎樣才能在燒瓶中獲得遠程 IP?
先感謝您。
uj5u.com熱心網友回復:
以下方法對我們有用:
from flask import request
def ipaddress():
try:
if request.environ.get('HTTP_X_FORWARDED_FOR') is None:
ip_addr = request.environ['REMOTE_ADDR']
else:
ip_addr = request.environ['HTTP_X_FORWARDED_FOR']
return ip_addr
except Exception as err:
print(f"Error getting ipaddress : {err}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511154.html
