Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FastCGI and Nginx - Return HTTP Status

Tags:

nginx

fastcgi

I have a custom FastCGI application behind Nginx and I'm struggling to get Nginx to return anything other than a 200 status code.

I've tried the following:

  • Setting fast_cgi_intercept_errors on.

  • Returning codes via ApplicationStatus in the EndRequest.

  • Returning Errors on the StdError stream.

  • Sending any of following headers:

    • "Status: 404 Not Found"

    • "HTTP/1.1 404 Not Found"

    • "X-PHP-Response-Code: 404"

    • "Status: 404 Not Found;"

    • "HTTP/1.1 404 Not Found;"

    • "X-PHP-Response-Code: 404;"

Any help would be great, I'm very stuck.

like image 952
DwayneBull Avatar asked Feb 17 '14 10:02

DwayneBull


People also ask

How does Nginx keep connections open in FastCGI?

By default, a FastCGI server will close a connection right after sending the response. However, when this directive is set to the value on , nginx will instruct a FastCGI server to keep connections open.

What is FastCGI proxying?

FastCGI Proxying Basics. In general, proxying requests involves the proxy server, in this case Nginx, forwarding requests from clients to a backend server. The directive that Nginx uses to define the actual server to proxy to using the FastCGI protocol is fastcgi_pass.

What does buffering of responses from the FastCGI server do?

When buffering of responses from the FastCGI server is enabled, limits the total size of buffers that can be busy sending a response to the client while the response is not yet fully read. In the meantime, the rest of the buffers can be used for reading the response and, if needed, buffering part of the response to a temporary file.

How does Nginx handle buffering and buffering?

When buffering is enabled, nginx receives a response from the FastCGI server as soon as possible, saving it into the buffers set by the fastcgi_buffer_size and fastcgi_buffers directives. If the whole response does not fit into memory, a part of it can be saved to a temporary file on the disk.


2 Answers

nginx discards the "HTTP/1.1 304 Not Modified\r\n".

nginx uses (and eats) the Status header.

If my fastcgi program returns the header "Status: 304\r\n".

Then I get this response:

HTTP/1.1 304
Server: nginx/1.6.2
Date: Sat, 21 May 2016 10:49:27 GMT
Connection: keep-alive

As you can see there is no Status: 304 header. It has been eaten by nginx.

like image 197
neoneye Avatar answered Sep 19 '22 22:09

neoneye


Here is an example of how you can return a 404 status code using fcgi and C++.

#include <iostream>
#include "fcgio.h"

using namespace std;

int main(void)
{
  streambuf * cin_streambuf = cin.rdbuf();
  streambuf * cout_streambuf = cout.rdbuf();
  streambuf * cerr_streambuf = cerr.rdbuf();

  FCGX_Request request;

  FCGX_Init();
  FCGX_InitRequest(&request, 0, 0);

  while (FCGX_Accept_r(&request) == 0)
  {
    fcgi_streambuf cin_fcgi_streambuf(request.in);
    fcgi_streambuf cout_fcgi_streambuf(request.out);
    fcgi_streambuf cerr_fcgi_streambuf(request.err);

    cin.rdbuf(&cin_fcgi_streambuf);
    cout.rdbuf(&cout_fcgi_streambuf);
    cerr.rdbuf(&cerr_fcgi_streambuf);

    cout << "Status: 404\r\n"
         << "Content-type: text/html\r\n"
         << "\r\n"
         << "<html><body>Not Found</body></html>\n";
  }

  cin.rdbuf(cin_streambuf);
  cout.rdbuf(cout_streambuf);
  cerr.rdbuf(cerr_streambuf);

  return 0;
}
like image 33
Kjetil Østerås Avatar answered Sep 17 '22 22:09

Kjetil Østerås