使用nginx/php-fpm时,浏览器有时会忽略位置标头.为什么? [英] With nginx/php-fpm, location header sometimes ignored by browser. Why?

查看:46
本文介绍了使用nginx/php-fpm时,浏览器有时会忽略位置标头.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个工作正常的apache/mod-php网站移至nginx/php-fpm.

I'm in the process of moving a working apache/mod-php website to nginx/php-fpm.

在Apache/mod-php下,我可以使用header("Location: $url");将浏览器重定向到其他页面,例如在尝试登录后.切换到nginx/php-fpm后,浏览器将不再在某些页面上遵循该重定向.我通过Firebug和Httpfox确认了响应中实际上已收到标头"Location:[url]".此行为也出现在Chrome中(未经IE测试).

Under Apache/mod-php, I could use header("Location: $url"); to redirect the browser to a different page, such as after a login attempt. After switching to nginx/php-fpm, the browser would no longer follow that redirect on certain pages. I confirmed with Firebug and Httpfox that the header "Location: [url]" was actually being received in the response. This behavior also appears in Chrome (not tested in IE).

所以我做了一些实验,读了一些有关http的东西,并使它起作用,但是我不确定它为什么起作用(或为什么不起作用).

So I did a few experiments, read a few things about http, and made it work, but I'm not sure why it works (or why it didn't).

我想出的解决方案是在"Location:[url]"标题之前发送"Status:303"标题.这适用于Chrome和Firefox,当我发送状态:200"或忽略状态"标头时,它们都忽略了位置"标头,但是当我将其更改为状态303"时,便进行了重定向.它与Apache下的Status 200一起使用.

The solution I came up with was to send a "Status: 303" header before the "Location: [url]" header. This works in Chrome and Firefox, which both ignored the Location header when I sent "Status: 200" or omitted the Status header, but did the redirect when I changed it to "Status 303". It worked with Status 200 under Apache.

使用Location标头是否需要Status标头?还是Apache正在做其他事情来使其正常工作?除了使它起作用的header("Status: 303");行之外,我没有更改任何涉及的php代码.这里必须要有其他工作,但是我不知道这可能是什么.

Is the Status header required to use the Location header? Or was Apache doing something else to make it work? I've not changed any of the php code involved other than the header("Status: 303"); line that made it work. There has to be something else at work here, but I have no clue what it could be.

有什么想法吗?

推荐答案

Location标头本身不会触发浏览器重定向.重定向实际上是由3xx系列中的HTTP响应代码触发的. w3c包含所有http响应代码的解释.

The Location header does not, by itself, trigger the browser to redirect. The redirect is actually triggered by an HTTP response code that is in the 3xx series. w3c has explanations for all http response codes.

Apache会自动在响应中看到Location标头,并且如果您之前未设置自己的响应代码,则会将响应代码强制为300系列. Nginx不会这样做-希望您自己设置适当的响应代码.

Apache automatically sees the Location header in the response, and forces the response code to be 300-series if you haven't previously set a response code of your own. Nginx does not do this -- it expects you set the proper response code yourself.

您可以强制php发送修改后的HTTP响应代码,如下所示:

You can force php to send a modified HTTP response code like this:

<?php
  header("HTTP/1.0 301 Moved Permanently");
?>

...然后,当然,您需要确保在发送上面显示的HTTP/1.0...行之后,仍然要发送Location标头 .

...Then, of course, you'll need to be sure you still sent your Location header after sending the HTTP/1.0... line shown above.

这篇关于使用nginx/php-fpm时,浏览器有时会忽略位置标头.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆