Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend / Apache2: Getting 302 Found when requesting url several times

I am programming a REST API with Zend framework.
When calling the url several times (e.g. 1000 times with 1 request per second), in about 0.2 % of the cases instead of getting 200 OK as a response I get 302 Found - so a redirect to a different page.
Here is the entire server response:

302 Found Date: Mon, 04 Mar 2013 11:56:04 GMT
Server: Apache/2.2.17 (Ubuntu)
X-Powered-By: PHP/5.3.5-1ubuntu7.11
Set-Cookie: PHPSESSID=ui9r8jqa63dbom8osknso6eea5; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: /de/default/index/index/error/500
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html; charset=utf-8

So Zend redirects to the error 500 (internal server error) page. The question is why - and I just can't figure it out...
The php page that is called inserts one row into a MySQL database and returns a JSON string - that's all.

Apache2 has about 20 concurrent connections open and the server load is <<1 so I really do not understand why the requests cause problems.

I know this is a really difficult problem to remote diagnose, but good guesses and recommendations how to solve this are more than welcome! Thanks.

This is the apache vhost config as requested by @chris:

<IfModule mod_ssl.c>
<VirtualHost mydomain.tld:443>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    ServerName www.mydomain.tld
    ServerAlias mydomain.tld *.mydomain.tld
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/ssl_access.log combined

#   RewriteLog "/var/log/htaccess.log"
#   RewriteLogLevel 5

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

    #   SSL Engine Switch:
    #   Enable/Disable SSL for this virtual host.
    SSLEngine on
    SSLOptions +StrictRequire
    SSLCertificateFile /etc/apache2/ssl/cert_2013_2014.crt
    SSLCertificateKeyFile /etc/apache2/ssl/www.mydomain.tld.key
    SSLCACertificateFile /etc/apache2/ssl/intermediate.crt

</VirtualHost>
</IfModule>
like image 399
Horen Avatar asked Mar 04 '13 12:03

Horen


People also ask

How do I stop 302 redirects?

If the HTTP library which you are using does not enable you to control this behavior, you can set an additional header X-If-No-Redirect with a value of 1 . This will prevent our servers from responding with a 302 status code in the case of a redirect.

What causes a 302 redirect?

What is an HTTP 302? The 302 status code is a redirection message that occurs when a resource or page you're attempting to load has been temporarily moved to a different location.

What is 302 not found?

A 302 status code is HTTP response status code indicating that the requested resource has been temporarily moved to a different URI.


2 Answers

This looks pretty simple and straight forward to me. This is a 302 redirect and I can't think of anything in ZF that redirects by itself; especially not to a 500 error page. A 500 Error (internal server error) must always return a 500 error and should never ever be 302 redirect. So you are sort of lucky here because you must have some error handling in your RETS API that causes a redirect somewhere (instead of a regular error page).

Search your code for redirects. It could be done with the ZF redirector helper (inside a controller) or manually (anywhere) with header() and exit(). When you found the redirect either show (exit) with a debug_backtrace or dump that into a log file. And also fix the return code or the way the error is handled.

like image 102
Adrian World Avatar answered Nov 01 '22 16:11

Adrian World


Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server.

http://httpd.apache.org/docs/2.2/mod/core.html#errordocument

I assume your're using the ErrorDocument directive in your Apache HTTP server configuration that will then do as configured for 500 errors.

500 errors can be triggered by PHP itself. To find out what happens you need to take a look into both the server error log as well as into php error log (and naturally enable PHP error logging for that).

Or as I commonly write:

A 500 Internal Sever Error is always an invitation to look into the servers error log. It contains more information. As this is PHP, it's also highly likely that it is because of a Fatal Error in PHP, so ensuring that PHP error logging is enabled and looking into the PHP error log is very useful, too. More about the 500 Internal Server Error

like image 43
hakre Avatar answered Nov 01 '22 16:11

hakre