1

Original Question:

Are there any tools for debugging Apache directives that would tell me what line caused a given redirect?

Reason: I have an ALB on AWS. I created a healthCheck.php page on all the instances, but when it does that, the result is always 301, which means all instances are marked as 'Unhealthy'. I could put a 301 as a success, but then I don't know if my server is actually down or not. Checking with curl in the same VPC, that is indeed the case:

curl http://192.0.2.56/healthCheck.php

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.192.0.2.56/healthCheck.php">here</a>.</p>
</body></html>

So I stared at my Apache Config file and couldn't see anything. It's untouched on the standard CentOS 7 config, with a bunch of virtual hosts down below.

As a super quick test I set up another ALB in the exact same way, pulled up a server from the official CentOS 7 AMI, and tested, and it doesn't redirect to www. So then I compared the two outputs. Literally no difference in the main files except the virtual hosts. I've spent a lot of time digging for what it could be. Any ideas? Tools I can use to trace down each step until the final response was generated?

The servers getting this 301 redirect are all port 80, with the ALB stripping off HTTPS. LAMP with PHP-FPM. Is it because my virtualhosts are all *:80?

Solution:

The general tools for debugging redirects can be found below, but here's what was causing my issue. Since I had VirtualHosts, the first (default) Virtual Host recieved the request. It's a Drupal site and we had this turned on to append www:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

So I just added a Directive to ignore my healthCheck.php like

RewriteCond %{HTTP_HOST} .
RewriteCond %{REQUEST_URI} !healthCheck.php
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Then I noticed it was still doing a 301 redirect from healthCheck.php to index.php so a simple search in the .htaccess revealed this was happening when the file wasn't found. I moved the check into the VHost directory and we're done.

If php-fpm is down I get a 503 Service Unavailable and if httpd is down, I get a Connection Refused. Much better and more verbose.

  • The log file usually shows a hint to what happened with a request and consider that you have AllowOverride All in the virtual host configuration and thus also allow sites you host to modify server behavior with .htaccess files ... – HBruijn Aug 2 '19 at 17:24
  • It should go to ErrorLog in your config: ErrorLog logs/error_log – suspectus Aug 2 '19 at 17:36
  • @HBruijn Well I'm assuming that AWS' ALB is doing it's health check at the root of my server and not one of the virtual hosts since I can't specify a URL. I don't have any .htaccess files in my roots, just folders for all the other virtual hosts. – Aaron Chamberlain Aug 2 '19 at 17:37
1

Add a rewrite:trace directive to LogLevel. For more details see Apache 2.4 Documentation.

Also note:

Using a high trace log level for mod_rewrite will slow down your Apache HTTP Server dramatically! Use a log level higher than trace2 only for debugging!

<VirtualHost *:80>
ServerName site1.org
ServerAlias www.site1.org
...
LogLevel alert rewrite:trace3
...
</VirtualHost>

The logs can be found where you specified ErrorLog which is typically /var/log/httpd/error_log. Note that if you have Virtual Hosts the first is the server default, so check in that Error Log as well.

|improve this answer|||||
  • I had noticed this is the docs about a week ago when I had time to deal with it but didn't notice the logs going anywhere. Does it go to the access_log specified in my vhost or something like syslog/journald? Not a production server just yet so I can slow it down. – Aaron Chamberlain Aug 2 '19 at 17:33
  • Hmm... so it looks like for whatever reason it's going into the first VirtualHost and applying the rules from the .htaccess there, which I noticed because the logs were getting put in that error_log. So is the best method to figure out how to get Apache to read healthCheck.php seperately or just add something to the site1 .htaccess? – Aaron Chamberlain Aug 2 '19 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.