504 gateway timeout when accessing by hostname, but not by IP

amazon ec2domain-name-systemgatewaytimeout

I'm using Amazon EC2 with Elastic Load Balancing. On my web-based application, end-users connect to app.myapp.com get a 504 gateway timeout error when performing certain operations like converting a document to PDF. Otherwise, the entire application is working normally.

However, there is NO ERROR when connecting to one of the servers directly via IP address and performing the same action. Never.

On my registrar's DNS entries I had already made a CNAME record which points app.myapp.com to my Elastic Load Balancer DNS name, and then my server instances sit behind that. Note: the exact problem still occurs even when I stop using the load balancer and point the DNS settings so that app.myapp.com goes directly to the IP address of one of my servers.

The bottom line is that this operation, though no URLs are hard-coded, times out when you've connected to the application via hostname >> but never happens when connecting via IP address. Over the course of months, I've tried just about every troubleshooting step I can think of. Willing to try anything.

Summary: what could cause a 504 gateway timeout when making requests with a hostname but not an IP?

MY RELEVANT VHOSTS ENTRY:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/application/public
    <Directory /var/www/application/public>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            Allow from all
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} -s [OR]
            RewriteCond %{REQUEST_FILENAME} -l [OR]
            RewriteCond %{REQUEST_FILENAME} -d
            RewriteRule ^.*$ - [NC,L]
            RewriteRule ^.*$ index.php [NC,L]
    </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 ${APACHE_LOG_DIR}/error.log

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

    CustomLog ${APACHE_LOG_DIR}/access.log combined

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>

Best Answer

Add a ServerName attribute in your VirtualHost config with the hostname you want to use.