1

Possible Duplicate:
404 header - HTTP 1.0 or 1.1?

I have a simple PHP code which send 404 using the header

header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); 
echo $_SERVER["SERVER_PROTOCOL"];

But when using the curl command and force 1.0, it return..

curl -0 -v 'http://www.example.com/test.php'


> GET /test.php HTTP/1.0

< HTTP/1.1 404 Not Found
< Server: nginx
< Date: Sat, 27 Oct 2012 08:51:27 GMT
< Content-Type: text/html
< Connection: close
< 
* Closing connection #0
HTTP/1.0

As you can see $_SERVER["SERVER_PROTOCOL"] is 1.0 but header give me 1.1, what is the reason?

Community
  • 1
  • 1
Ryan
  • 10,041
  • 27
  • 91
  • 156

2 Answers2

1

Great question.

You have to understand that this is not something specific to PHP so don't think there is a bug or something. It's specific to:

  1. A web server's configuration
  2. A client's (think browser) configuration

Usually clients, when sending a request to a web server, indicate which protocol they support. The web server then responds with the protocol it supports or it is configured to.

In your PHP code, when you flush that 404 header you are hinting to the server which protocol to use. You can't force it - at least I am not aware of a way to force it from PHP. The web server will ultimately settle for the highest protocol supported by both itself and the requesting client.

Unless the client forces it to, nginx will always respond over HTTP/1.1. I am not aware of a method of configuring it like Apache for example. But that is the reason for the behavior you are seeing.

Maybe you should edit your question, or open a new question, to be more specific to nginx since, like I explained this is not a problem with PHP. That way more people might be able to find the question and help out.

9ee1
  • 1,078
  • 1
  • 10
  • 25
  • I am using nginx/php5-fpm, the problem is when the client tell they only support 1.0, it still make sense to respond with 1.1? – Ryan Oct 27 '12 at 10:44
  • @Yoga Yeah, I just realized that it was clear from the response headers you posted that you were using nginx. Sorry about that :). Anyway, see my edit for details. – 9ee1 Oct 27 '12 at 11:02
  • my `curl` command already specified I only want to chat with `http 1.0`, i also agree it is the problem of `nginx`, then I will report in serverfault. thanks. – Ryan Oct 27 '12 at 15:11
-1

If you're using FastCGI (which you probably are) between nginx and PHP, you should instead use

header("Status: 404 Not Found");

as advised by PHP's documentation for header.

Ilya O.
  • 1,500
  • 13
  • 19
  • Yes I have tried, still respond with 1.1 – Ryan Oct 27 '12 at 10:43
  • `$_SERVER["SERVER_PROTOCOL"]` is only the version of the protocol that the request was sent with, not necessarily the one that the response will be sent with. Using that header should set your 404 appropriately. – Ilya O. Oct 28 '12 at 13:55