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

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

我有一个 Nuxt.js 程序,在 Ubuntu 操作系统的 Apache 上运行。当我将它移植到子域时,例如www.example.com,它工作正常;但当我将它移植到主域时,例如示例.com,我的程序无法运行。Ubuntu Server 和 Apache 设置示例如下:

/etc/主机名

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 [email protected]
    
    ProxyPass / http://localhost:3000/
</VirtualHost>

现在的问题是,如何在主域上运行该程序并将地址重定向到www.example.com当有人去示例.com地址?

更新

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

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

<VirtualHost example.com:80>
    ServerName example.com
    ServerAdmin [email protected]

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

我甚至使用了以下两个代码示例从主域重定向到子域,但仍然不起作用:

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

或者

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

答案1

example.com这是一个从到 的简单重定向www.example.com

<VirtualHost www.example.com:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin [email protected]

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

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

答案2

我不明白为什么Nuxt.js不能在主域上运行,至少不能阿帕奇Ubuntu操作系统。 但通过编写适当的重定向规则,我能够解决这个问题,如下所示:

<VirtualHost www.example.com:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin [email protected]
    
    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 免费 SSL 通配符证书;有关更多信息,你可以参考网页):

<VirtualHost www.example.com:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin [email protected]
    
    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 [email protected]
    
    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>

相关内容