drwj4061 2013-06-29 13:37
浏览 373
已采纳

通过VPN连接:PHP $ _SERVER仍然提供本地IP

I have setup a PPTP VPN server on AWS and everything works perfectly. When connected to my VPN e.g. www.getip.com shows the IP address of the VPN server.

So far so good...

On the same server I installed an Apache server where I run a PHP script to get me my own IP address:

$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];

Regardless if I am connected to VPN this code shows my local IP. I am not using varnish or any similar caching.

Any idea why this is the case? - Are the $_SERVER variables saved in the PHP session?

Your help is very much appreciated.

[UPDATE] The problem is definitely linked to the fact that the VPN server and the PHP script are on the same server. If I put the script on a different server everything works as desired. If connected to the VPN I expect to see the public IP of the server (which is the VPN server at the same time).

  • 写回答

1条回答 默认 最新

  • dongxia1390 2014-08-18 12:16
    关注

    If you are using a VPC, then there is a problem in that the local IP will be used for all communication inside the VPC. The Public IP (EIP I assume) does not exist inside the VPC, it is assigned to the network interface and only translated in the IGW going in or out.

    This means that when you check with www.getip.com you get the EIP just as expected since you pass the IGW, but inside the local net you will only see the local IP. Also, PPTP VPN works as it also passes the IGW.

    Amazon suggests that you use the FDQN, even if you are on the inside and look up the external FDQN, you will get the local IP.

    I do not know how this is in the Classic EC2, but I can only guess its similar.

    If you absolutely need to have the public IP, you find it by using the Amazon service for this:

    curl http://169.254.169.254/latest/meta-data/public-ipv4
    

    or

    curl http://169.254.169.254/latest/meta-data/local-ipv4
    

    (See here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?