0

I have a Nuxt.js program that I run on Apache in Ubuntu OS. When I port it to a subdomain like www.example.com, it works fine; But when I port it to the main domain such as example.com, my program does not run. Examples of Ubuntu Server and Apache settings are as follows:

/etc/hostname

example

/etc/hosts

127.0.0.1   localhost
127.0.1.1   ubuntu
100.200.300.40  example.com
100.200.300.40  www.example.com
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

/etc/resolv.conf

nameserver 100.200.300.40
search example.com

/etc/bind/named.conf.options

options {
    directory "/var/cache/bind";
    dnssec-validation auto;

    auth-nxdomain no;    # conform to RFC1035
    listen-on-v6 { any; };
    
    forwarders {
        100.200.300.40;
        100.200.300.40;
    };
};

/etc/bind/named.conf.local

zone "example.com" IN {
type master;
file "/etc/bind/db.example.com";
};

zone "300.200.100.in-addr.arpa" IN {
type master;
file "/etc/bind/db.100";
};

/etc/bind/db.example.com

$TTL    604800
@   IN  SOA ns1.example.com. root.example.com. (
            7
            7200
            900
            1209600
            86400 )
;
@   IN  NS  ns1.example.com.
@   IN  NS  ns2.example.com.
@   IN  A   100.200.300.40
@   IN  AAAA    ::1
ns1 IN  A   100.200.300.40
ns2 IN  A   100.200.300.40
www IN  A   100.200.300.40

/etc/bind/db.100

$TTL    604800
@   IN  SOA ns1.example.com. root.example.com. (
                  6     ; Serial
             7200       ; Refresh
              900       ; Retry
            1209600     ; Expire
             86400 )    ; Negative Cache TTL
;
@   IN  NS  ns1.
@   IN  NS  ns2.
@   IN  PTR ns1.example.com.
@   IN  PTR ns2.example.com.

/etc/apache2/apache2.conf

DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>
<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

/etc/apache2/sites-available/www.example.com.conf

<VirtualHost www.example.com:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin webmaster@example.com
    
    ProxyPass / http://localhost:3000/
</VirtualHost>

Now the question is how can I run the program on the main domain and redirect the address for example to www.example.com when someone goes to example.com address?

Update

I already ported my Node.js program to the main domain through the following settings, which did not run:

/etc/apache2/sites-available/example.com.conf

<VirtualHost example.com:80>
    ServerName example.com
    ServerAdmin webmaster@example.com

    ProxyPass / http://localhost:3000/
</VirtualHost>

And I even used the following two code examples to redirect from the main domain to the subdomain, which still did not work:

Redirect permanent / http://www.example.com/

OR

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
0

This is a simple redirect from example.com to www.example.com:

<VirtualHost www.example.com:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin webmaster@example.com

    RewriteEngine On
    RewriteCond %{SERVER_NAME} example.com
    RewriteRule /(.*) http://www.example/com/$1 [R]

    ProxyPass / http://localhost:3000/
</VirtualHost>
5
  • I tried it and it didn't work <VirtualHost www.example.com:80> ServerName www.example.com ServerAdmin webmaster@example.com RewriteEngine On RewriteCond %{SERVER_NAME} example.com RewriteRule /(.*) example.com/$1 [R] ProxyPass / localhost:3000 </VirtualHost> – MRS1367 Mar 3 at 10:30
  • The redirect works but the Nuxt.js program does not run and gives the following error: ERR_TOO_MANY_REDIRECTS – MRS1367 Mar 3 at 10:35
  • And if I delete ServerAlias, the Apache page will be displayed. – MRS1367 Mar 3 at 10:36
  • To me it seems that the Nuxt.js program constantly tries to redirect to example.com from www.example.com. This means that unless you convince it to accept the host name given to it as is, you won't be able to host it on any other address, as it will always try to redirect the clients back to example.com. – Lacek Mar 3 at 12:45
  • The reason for this redirect is that ServerAlias is present in the Apache Configurations; Of course, with deleting it does not happen anything and only the Apache page is displayed. The problem is not with Nuxt.js – MRS1367 Mar 4 at 4:35
0

I did not understand why Nuxt.js does not run on the main domain, at least with Apache on the Ubuntu OS. But by writing a proper Redirect Rule, I was able to solve this problem which is as follows:

<VirtualHost www.example.com:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin webmaster@example.com
    
    ProxyPass / http://localhost:3000/
    
    RewriteEngine on
    RewriteCond %{REQUEST_SCHEME}://%{SERVER_NAME} =http://example.com [OR]
    RewriteCond %{REQUEST_SCHEME}://%{SERVER_NAME} =http://www.example.com
    RewriteRule ^ http://www.example.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

If someone wanted to run the project on HTTPS, the process is as follows (SSL license in the following example is Let's Encrypt Free SSL Wildcard Certificate; for more information about it, you can refer to this web page):

<VirtualHost www.example.com:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin webmaster@example.com
    
    ProxyPass / http://localhost:3000/
    
    RewriteEngine on
    RewriteCond %{REQUEST_SCHEME}://%{SERVER_NAME} =http://example.com [OR]
    RewriteCond %{REQUEST_SCHEME}://%{SERVER_NAME} =http://www.example.com
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost www.example.com:443>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin webmaster@example.com
    
    ProxyPass / http://localhost:3000/
    
    RewriteEngine on
    RewriteCond %{REQUEST_SCHEME}://%{SERVER_NAME} =https://example.com
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [END,NE,R=permanent]

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

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.