Apache 子域重定向到主域

Bwo*_*kzy 5 dns apache2

我有一个正在尝试设置的 apache 服务器,是的,我使用的是最新版本。我想要一个主域作为网站,然后我想要一个子域作为会员论坛。我有子域设置,并且我还在confapache etc 目录内设置了文件。我不知道我做错了什么,它只是将我重定向到网站的主页,而不是转到.html我设置用于测试的页面。

这是我的文件的代码.conf

<VirtualHost *:443>
    # ServerName forum.example.com

    ServerAdmin root@example.com
    ServerName forum.example.com
    ServerAlias forum.example.com
    DocumentRoot /var/www/forum.example.com

    <Directory /var/www/forum.example.com>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    # ServerName forum.example.com

    ServerAdmin root@example.com
    ServerName forum.example.com
    ServerAlias forum.example.com
    DocumentRoot /var/www/forum.example.com

    <Directory /var/www/forum.example.com>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

我没有.htaccess文件 AFAIK

Joh*_*ion 3

好吧..终于解决了问题..让它工作

首先在你的主机dns上...你需要添加你的服务器名称的记录,我使用namecheap.com..我添加了一个A + Dynamic DNS Record主机john和我的IP地址...与我的www A记录相同.. .我假设它只是A Record其他地方的标准

一旦那里..可能需要一段时间才能真正ping通它一旦可以..如果所有其他设置都正确,那么您就可以开始了

现在好了,对于我使用的配置文件..我在..中创建了sites-available两个

第一个被称为001-john.conf我发现文件结尾很重要,.conf否则您将无法使用似乎可以解决问题的命令。

在该配置中我添加了以下内容:

<VirtualHost *:80>
    ServerAdmin webmaster@mysite.com (of course you would use your domain name instead of mysite.com)
    ServerName john.mysite.com
    ServerAlias john.mysite.com
    DocumentRoot /web/john (of course you would make the root the path to the folder where the files are stored)

<Directory />
    Options FollowSymLinks ExecCGI
    AllowOverride ALL
</Directory>
<Directory /web/john/>
            Options ExecCGI FollowSymLinks Includes Indexes (or whatever directives you want)
            AllowOverride ALL
            order allow,deny
            allow from all
</Directory>

ScriptAlias /cgi-bin/ /web/plus/cgi-bin/  (if you wanted to create a cgi bin use your correct path for this)
<Directory "/web/plus/cgi-bin">
    AllowOverride ALL
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>
ErrorLog /var/log/apache2/errorjohn.log
CustomLog /var/log/apache2/accessjohn.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

第二个配置文件是 ssl 的,我称之为它,001-john-ssl.conf 它看起来像这样:

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin webmaster@mysite.com
        ServerName john.mysite.com
        ServerAlias john.mysite.com
        DocumentRoot /web/john

<Directory />
    Options FollowSymLinks
    AllowOverride ALL
</Directory>

<Directory /web/john/>
    Options Indexes FollowSymLinks Includes MultiViews ExecCGI
    AllowOverride ALL
            Require all granted
</Directory>


ScriptAlias /cgi-bin/ /web/plus/cgi-bin/
<Directory "/web/plus/cgi-bin">
    AllowOverride ALL
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>




    LogLevel warn
    ErrorLog /var/log/apache2/errorjohn.log
    CustomLog /var/log/apache2/accessjohn.log combined

   SSLEngine on
   SSLProtocol all -SSLv2
   SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM
   SSLCertificateFile    /etc/apache2/ssl/your.crt file
   SSLCertificateKeyFile /etc/apache2/ssl/your.key file
   SSLCACertificateFile /etc/apache2/ssl/your.ca-bundle
   SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>
</IfModule>
Run Code Online (Sandbox Code Playgroud)

一旦一切都被保存,我就跑了

sudo a2ensite 001-john.conf
Run Code Online (Sandbox Code Playgroud)

sudo a2ensite 001-john-ssl.conf
Run Code Online (Sandbox Code Playgroud)

然后我跑了

sudo service apache2 reload
Run Code Online (Sandbox Code Playgroud)

然后检查它是否同时具有..之前没有..我刚刚创建了从站点可用到站点启用的链接..这似乎对我不起作用,但在使用标有. conf 扩展..我运行apachectl -t -D DUMP_VHOSTS并得到了这个结果:

VirtualHost configuration:
*:443                  is a NameVirtualHost
         default server mysite.com (/etc/apache2/sites-enabled/000-default-ssl.conf:2)
         port 443 namevhost mysite.com (/etc/apache2/sites-enabled/000-default-ssl.conf:2)
         port 443 namevhost john.mysite.com (/etc/apache2/sites-enabled/001-john-ssl.conf:2)
                 alias john.mysite.com
*:80                   is a NameVirtualHost
         default server mysite.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost mysite.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost john.mysite.com (/etc/apache2/sites-enabled/001-john.conf:1)
             alias john.mysite.com
Run Code Online (Sandbox Code Playgroud)

**正如注释一样..我的原始站点位于目录中/web/public,子域站点位于/web/john...因为您的站点位于.../var/www/html我可能会使用类似于/var/www/forum您子域文件夹的内容来存储您的论坛文件,我会使用forum.yoursite.com作为ServerNameServerAlias但希望通过示例您将看到所有内容的格式和设置。

一旦 DNS 记录从 namecheap 更新..我就能够访问 john.mysite.com 并且它会在那里显示简单的“正在建设”文件,如果我只是访问 www.mysite.com 它会给我正常的不久前设置的页面

所以现在它正在使用子域

希望这将帮助您完成并正确设置事情..很可能是因为配置文件可能没有扩展名.conf并且您没有使用a2ensite它们来启用它们..这似乎是我遇到的问题


小智 1

作为对遇到同样问题的人的提示:在我的例子中,向子域添加 SSL 证书解决了问题。我的主域在 SSL 上运行,显然所有 HTTP 流量都被重定向到主域上的 HTTPS。