Apache-2.4

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

  • March 23, 2021

我有一個 Nuxt.js 程序,我在 Ubuntu 作業系統的 Apache 上執行。當我將它移植到像**www.example.com這樣的子域時,它工作正常;但是當我將它移植到example.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 webmaster@example.com
   
   ProxyPass / http://localhost:3000/
</VirtualHost>

現在的問題是,當有人訪問example.com地址時,如何在主域上執行程序並將地址重定向到例如**www.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}/$1 [R=301,L]

我不明白為什麼Nuxt.js不能在主域上執行,至少在Ubuntu作業系統上使用Apache 。 但是通過編寫適當的Redirect Rule,我能夠解決這個問題,如下所示:

<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;有關它的更多資訊,您可以參考網頁):

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

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>

引用自:https://serverfault.com/questions/1055748