Nuxt.js 程序未在 Ubuntu 中使用 Apache 的主域上 运行

Nuxt.js program not running on the Main Domain with Apache in Ubuntu

我在 Ubuntu OS 中 运行 在 Apache 上有一个 Nuxt.js 程序。当我将它移植到像 www.example.com 这样的子域时,它工作正常;但是当我将它移植到example.com等主域时,我的程序却没有运行。 Ubuntu服务器和Apache设置示例如下:

/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>

现在的问题是当有人访问 运行 主域上的程序并将地址重定向到 www.example.com =73=]example.com地址?

更新

我已经通过以下设置将我的 Node.js 程序移植到主域,但没有 运行:

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

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

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

我什至使用了以下两个代码示例从主域重定向到子域,仍然没有用:

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

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

我不明白为什么 Nuxt.js 在主域上没有 运行,至少 ApacheUbuntu OS. 但是通过编写适当的重定向规则,我能够解决如下问题:

<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>

如果有人想运行HTTPS上的项目,过程如下(以下示例中的SSL许可证是Let's Encrypt Free SSL Wildcard Certificate;更多信息请参考this网页):

<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>