简体   繁体   English

Apache / PHP重定向循环

[英]Apache/php redirect loop

Here is the situation in the smallest explanation I can give. 这是我能给出的最小解释的情况。

From php, I do a redirect with the following function 从PHP,我使用以下功能进行重定向

function redirect($loc)
{
    session_write_close();
    header("Location: $loc");
    exit();
}

For example, redirect('http://www.google.com/'); 例如, redirect('http://www.google.com/');

The problem is that when I attempt to do redirect('https://local.application.com') the browser gets a header that says Location: https://local-apps.application.com . 问题是,当我尝试进行redirect('https://local.application.com') ,浏览器将获得一个标题为Location: https://local-apps.application.com的标头Location: https://local-apps.application.com https://local-apps.application.com is the uri that the original request comes through. https://local-apps.application.com是原始请求通过的uri。

I've used xdebug to step through the php code right up to header('Location: $loc'); 我已经使用xdebug逐步遍历php代码直到header('Location: $loc'); ;。 call and I can verify 100% that $loc == 'https://local.application.com' because xdebug tells me so. 致电,我可以100%验证$loc == 'https://local.application.com' //local.application.com $loc == 'https://local.application.com'因为xdebug告诉了我。 However, for SOME reason, the browser gets Location: https://local-apps.application.com . 但是,由于某些原因,浏览器将获取Location: https://local-apps.application.com This causes an infinite loop as https://local-apps.application.com once again redirects to https://local.application.com . 这将导致无限循环,因为https://local-apps.application.com再次重定向到https://local.application.com

What is maybe going on?! 可能是怎么回事?!

The extremely weird part is that putting the SAME EXACT code in our qa environment yields the expected behavior and NO INIFINITE loop is caused. 极其奇怪的是,将相同的精确代码放入我们的qa环境中会产生预期的行为,并且不会引起INIFINITE循环。 In qa the urls are qa-apps.application.com and qa.application.com. 在qa中,网址为qa-apps.application.com和qa.application.com。

I don't know if my local Apache is doing this. 我不知道我的本地Apache是​​否正在执行此操作。 I doubt it's php's fault because xdebug shows me for certain that the $loc variable is set to the right uri and I'm doing an exit right after. 我怀疑这是php的错,因为xdebug确实向我展示了$ loc变量设置为正确的uri,然后我立即exit

I've also tried using Charles web proxy to act as a man in the middle to look at traffic but it also shows Location: https://local-apps.application.com instead of the expected Location: https://local.application.com . 我还尝试使用Charles Web代理充当中间人来查看流量,但它还会显示Location: https://local-apps.application.com而不是预期的Location: https://local.application.com

Is there an Apache setting that would alter outgoing headers? 是否有Apache设置会更改传出标头? What should I do? 我该怎么办?

EDIT 1 编辑1

I altered my Apache access log to print out the Location header in the response and it is the correct value. 我更改了Apache访问日志,以在响应中打印出Location标头,它是正确的值。 So apache isn't doing anything weird here. 因此,apache在这里没有做任何奇怪的事情。

EDIT 2 编辑2

I removed all entries from my /etc/hosts file. 我从/ etc / hosts文件中删除了所有条目。 I then installed dnsmasq and set it up as the primary DNS server for my computer. 然后,我安装了dnsmasq并将其设置为我的计算机的主要DNS服务器。 I tested this by doing dig @127.0.0.1 local.application.com and then dig @127.0.0.1 www.google.com to make sure I had set it up correctly. 我先进行dig @127.0.0.1 local.application.com ,然后再进行dig @127.0.0.1 www.google.com ,以确保设置正确。

I added address=/local.application.com/127.0.0.1 and address=/local-apps.application.com/127.0.0.1 to the dnsmasq config and restarted dnsmasq. 我在dnsmasq配置中添加了address=/local.application.com/127.0.0.1address=/local-apps.application.com/127.0.0.1并重新启动了dnsmasq。 I even restarted my computer after setting it up as a daemon that starts on reboot. 在将计算机设置为可重新启动的守护程序后,我什至重新启动了计算机。

I get the same weird behavior on my local machine. 我在本地计算机上得到了同样奇怪的行为。 My webbrowser goes into an infinite loop. 我的网络浏览器陷入无限循环。

More info if you are curious 如果您感到好奇,请获取更多信息

I'm putting together an oauth2 implementation in my workplace. 我正在工作场所中集成一个oauth2实现。

The authorize point exists on my local machine at local-apps.application.com/authorize (local-apps) because that's where mobile apps will access it. 授权点存在于我的本地计算机上,位于local-apps.application.com/authorize(local-apps),因为移动应用程序可以在其中访问它。 In production it would be apps.application.com/authorize. 在生产中将是apps.application.com/authorize。 (Sorry, I can't control the subdomains being used). (对不起,我无法控制正在使用的子域)。

Depending on the client_id in the initial oauth2 auhorize request I will first redirect to a client appropriate domain. 根据初始oauth2 auhorize请求中的client_id,我将首先重定向到客户端适当的域。 From local-apps.application.com/authorize I'll go to local.client-1.com/authorize and then from there I would redirect to local.client-1.com/login if the oauth2 credentials are validated successfully.. Why the extra redirect? local-apps.application.com/authorize我将转到local.client-1.com/authorize ,然后如果成功验证了oauth2凭据,则将从那里重定向到local.client-1.com/login 。为什么要进行额外的重定向? 1. A limitation in the way our templating system works and the fact that it depends on the domain to load the proper html. 1.模板系统工作方式的局限性以及它依赖于域来加载正确的html的事实。 2. The sessions for php don't transfer across domains so I have to start the php session on the right domain. 2. php的会话不会跨域转移,因此我必须在正确的域上启动php会话。 I can't change it. 我不能改变 I just have to deal with it. 我只需要处理它。 Please don't make suggestions related to this url scheme thingy. 请不要提出与此网址方案相关的建议。 I just want to know why php redirects to one url and the browser get a different url in the header. 我只想知道为什么php重定向到一个URL,而浏览器在标题中得到一个不同的URL。

It seems, you have a same ip addresses in /etc/hosts file or you can check your Apache virtual host file to make sure the correct path. 看来,/ etc / hosts文件中的IP地址相同,或者可以检查Apache虚拟主机文件以确保正确的路径。 Note: Apache configuration files location and directory structure depends on the linux distro. 注意:Apache配置文件的位置和目录结构取决于linux发行版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM