Redirects in virtual hosts maxing out CPU

Issues related to applications and software problems
Post Reply
lukewood
Posts: 6
Joined: 2009/11/30 05:50:27
Contact:

Redirects in virtual hosts maxing out CPU

Post by lukewood » 2020/10/07 09:02:38

Hi,

I am running Apache 2.4.6 with PHP 7.0.33 and MariaDB version 5.5.65 on Centos 7.8.2003. I am using redirects in virtual hosts to direct the naked domain and http to https:/www.

I am finding that the CPU is maxing out. If I disable the redirects, the CPU returns to normal. My config is as follows:
<VirtualHost *:80>
ServerName www.domain.my
Redirect "/" "https://www.domain.my/"
</VirtualHost>

<VirtualHost *:443>
ServerName www.domain.my
<Directory /srv/www/domain.my/html>
Options FollowSymLinks Indexes MultiViews
Require all granted
AllowOverride All
</Directory>
DocumentRoot /srv/www/domain.my/html
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.domain.my.2019.crt
SSLCertificateKeyFile /etc/pki/tls/private/domain.my.key
SSLCACertificateFile /etc/httpd/conf/ssl.crt/www.domain.my.2019.ca-bundle
</VirtualHost>

<VirtualHost *:80>
ServerName domain.my
Redirect "/" "https://domain.my/"
</VirtualHost>

<VirtualHost *:443>
ServerName domain.my
<Directory /srv/www/domain.my/html>
Options FollowSymLinks Indexes MultiViews
Require all granted
AllowOverride All
</Directory>
DocumentRoot /srv/www/domain.my/html
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.domain.my.2019.crt
SSLCertificateKeyFile /etc/pki/tls/private/domain.my.key
SSLCACertificateFile /etc/httpd/conf/ssl.crt/www.domain.my.2019.ca-bundle
</VirtualHost>

Is there a better way to handle these redirects that is less resource intensive? Sorry if I am asking something basic but any help would be appreciated.

Kind regards,

Luke

User avatar
KernelOops
Posts: 347
Joined: 2013/12/18 15:04:03
Location: xfs file system

Re: Redirects in virtual hosts maxing out CPU

Post by KernelOops » 2020/10/07 09:45:09

I think redirect works by providing the reason code, like this:

Code: Select all

Redirect 301 / "https://www.domain.my/"
or you could use another common method with rewriterules:

Code: Select all

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.my/$1 [R,L]
--
I love my computer - all my friends live there.
--

lukewood
Posts: 6
Joined: 2009/11/30 05:50:27
Contact:

Re: Redirects in virtual hosts maxing out CPU

Post by lukewood » 2020/10/07 12:11:16

Hi KernelOops,

Thanks for your reply. I have changed the configuration based on your suggestion:

<VirtualHost *:80>
ServerName www.domain.my
Redirect 301 / "https://www.domain.my/"
</VirtualHost>

<VirtualHost *:443>
ServerName www.domain.my
<Directory /srv/www/domain.my/html>
Options FollowSymLinks Indexes MultiViews
Require all granted
AllowOverride All
</Directory>
DocumentRoot /srv/www/domain.my/html
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.domain.my.2019.crt
SSLCertificateKeyFile /etc/pki/tls/private/domain.my.key
SSLCACertificateFile /etc/httpd/conf/ssl.crt/www.domain.my.2019.ca-bundle
</VirtualHost>

<VirtualHost *:80>
ServerName domain.my
Redirect 301 / "https://www.domain.my/"
</VirtualHost>

<VirtualHost *:443>
ServerName domain.my
Redirect 301 / "https://www.domain.my/"
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.domain.my.2019.crt
SSLCertificateKeyFile /etc/pki/tls/private/domain.my.key
SSLCACertificateFile /etc/httpd/conf/ssl.crt/www.domain.my.2019.ca-bundle
</VirtualHost>

It is working fine but the survey is maxing out. It seems strange as the redirects are there just incase someone types them in incorrectly.

Kind regards,

Luke

User avatar
TrevorH
Forum Moderator
Posts: 29681
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Redirects in virtual hosts maxing out CPU

Post by TrevorH » 2020/10/07 12:53:49

Look at your logs. Is it looping re-requesting pages?
CentOS 6 will die in November 2020 - migrate sooner rather than later!
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 is dead, do not use it.
Full time Geek, part time moderator. Use the FAQ Luke

User avatar
KernelOops
Posts: 347
Joined: 2013/12/18 15:04:03
Location: xfs file system

Re: Redirects in virtual hosts maxing out CPU

Post by KernelOops » 2020/10/07 20:21:26

I think the issue is with your ServerNames. I see the following:

ServerName www.domain.my (80)
ServerName www.domain.my (443)
ServerName domain.my (80)
ServerName domain.my (443)

What is more typical, is to use one domain and all others are defined as aliases. Try a setup that looks like this:

Code: Select all

<VirtualHost *:80>
ServerName domain.my
ServerAlias www.domain.my
UseCanonicalName Off
Redirect 301 / "https://www.domain.my/"
</VirtualHost>

<VirtualHost *:443>
ServerName domain.my
ServerAlias www.domain.my
UseCanonicalName Off

<Directory /srv/www/domain.my/html>
  Options FollowSymLinks Indexes MultiViews
  AllowOverride All
</Directory>

DocumentRoot /srv/www/domain.my/html
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.domain.my.2019.crt
SSLCertificateKeyFile /etc/pki/tls/private/domain.my.key
SSLCACertificateFile /etc/httpd/conf/ssl.crt/www.domain.my.2019.ca-bundle
</VirtualHost>

PS:
Obviously you need to re-issue the certificates to include both domains (domain.my & www.domain.my)

I also removed "Require all granted".
--
I love my computer - all my friends live there.
--

lukewood
Posts: 6
Joined: 2009/11/30 05:50:27
Contact:

Re: Redirects in virtual hosts maxing out CPU

Post by lukewood » 2020/10/08 07:06:42

Thanks, this is what I am looking for. I had issues when I tried to implement the changes but I will try again. Thanks so much.

Post Reply

Return to “CentOS 7 - Software Support”