我正在尝试使用 apache 2.4 配置两个子域,但似乎存在一个我无法解决的问题。
这是 apache 配置文件
<VirtualHost *:80>
ServerName www.subdomain1.myweb.com
ServerAlias subdomain1.myweb.com
DocumentRoot /srv/webapps/mywebapp
<Directory /srv/webapps/mywebapp>
AllowOverride all
Options -MultiViews
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.subdomain2.myweb.com
ServerAlias subdomain2.myweb.com
DocumentRoot /srv/webapps/mywebapp2
<Directory /srv/webapps/mywebapp2>
AllowOverride all
Options -MultiViews
Require all granted
</Directory>
我尝试定义 ServerName,但没有解决问题:
ServerName www.myweb.com
并尝试在 VirtualHost 中使用服务器 IP、使用通配符、在 VirtualHost 标签中使用域等...一切按预期工作 :(
重点是 apache 服务器subdomain1.myweb.com,但subdomain.myweb.com不要。我无法同时为两个子域提供服务的唯一方法是将最后一个子域设置为:
<VirtualHost *:80>
ServerName www.myweb.com
ServerAlias myweb.com
DocumentRoot /srv/webapps/mywebapp2
<Directory /srv/webapps/mywebapp2>
AllowOverride all
Options -MultiViews
Require all granted
</Directory>
因此,我可以在根路径中提供一个子域和另一个子域,但这不是所需的行为。
我猜测 apache 以某种方式将请求与第一个子域进行匹配,不知道为什么。
Apache 将其显示为虚拟服务器映射:
VirtualHost configuration:
*:80 is a NameVirtualHost
default server www.myweb.com
port 80 namevhost www.myweb.com
alias subdomain1.myweb.com
port 80 namevhost www.subdomain2.myweb.com
alias subdomain2.com
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/srv/http"
Main ErrorLog: "/var/log/httpd/error_log"
Mutex proxy: using_defaults
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex proxy-balancer-shm: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
先感谢您!
编辑
httpd.conf 基本上是默认配置,因为我从一开始就遇到了这个问题,而且除了虚拟主机之外我还没有配置任何东西。
服务器根目录“/etc/httpd”
Listen 80
<IfModule unixd_module>
User http
Group http
</IfModule>
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/srv/http"
<Directory "/srv/http">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "/var/log/httpd/error_log"
LogLevel warn
<Directory "/srv/http/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
# Server-pool management (MPM specific)
Include conf/extra/httpd-mpm.conf
# Multi-language error messages
Include conf/extra/httpd-multilang-errordoc.conf
# Fancy directory listings
Include conf/extra/httpd-autoindex.conf
# Language settings
Include conf/extra/httpd-languages.conf
# User home directories
Include conf/extra/httpd-userdir.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
# Various default settings
Include conf/extra/httpd-default.conf
#PHP
Include conf/extra/php5_module.conf
# Configure mod_proxy_html to understand HTML4/XHTML1
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
http-vhosts.conf 与最初的问题相同
httpd 默认配置文件
#
# This configuration file reflects default settings for Apache HTTP Server.
#
# You may change these, but chances are that you may not need to.
#
#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 60
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5
#
# UseCanonicalName: Determines how Apache constructs self-referencing
# URLs and the SERVER_NAME and SERVER_PORT variables.
# When set "Off", Apache will use the Hostname and Port supplied
# by the client. When set "On", Apache will use the value of the
# ServerName directive.
#
UseCanonicalName Off
#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives. See also the AllowOverride
# directive.
#
AccessFileName .htaccess
#
# ServerTokens
# This directive configures what you return as the Server HTTP response
# Header. The default is 'Full' which sends information about the OS-Type
# and compiled in modules.
# Set to one of: Full | OS | Minor | Minimal | Major | Prod
# where Full conveys the most information, and Prod the least.
#
ServerTokens Full
#
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory
# listings, mod_status and mod_info output etc., but not CGI generated
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of: On | Off | EMail
#
ServerSignature Off
#
# HostnameLookups: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off
#
# Set a timeout for how long the client may take to send the request header
# and body.
# The default for the headers is header=20-40,MinRate=500, which means wait
# for the first byte of headers for 20 seconds. If some data arrives,
# increase the timeout corresponding to a data rate of 500 bytes/s, but not
# above 40 seconds.
# The default for the request body is body=20,MinRate=500, which is the same
# but has no upper limit for the timeout.
# To disable, set to header=0 body=0
#
<IfModule reqtimeout_module>
RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
</IfModule>
我尝试玩http-默认配置文件改变使用规范名称或者主机名查找给他们一个机会,但目前还没有任何效果......
解决方案
最后看来这是一个 DNS 配置问题,而不是虚拟主机配置问题。
在这种情况下使用 Google 域,只需为每个子域添加一个自定义资源记录,并指向将处理请求的 IP 地址(在我的情况下是同一台服务器,它将使用虚拟主机处理请求,如 OP 所示),使其工作。
谢谢你们!
答案1
我注意到在您的 subdomain2 中,您没有包含 。不确定这只是这里的配置,还是您的配置。您是否执行了 service httpd reload(或 service apache2 reload,具体取决于操作系统)您的 DNS 是否使用这些域名指向您的 IP?
答案2
TL;DR 删除 www
www 已经是 example.com 的子域名。
因此您当然可以从原始域名定义 3 级或 4 级域名。但您很可能不希望将 www.yourdomain.com 作为子域名。
您可能想要的是 subdomain.yourdomain.com,因此只需删除 wwww。
还没有测试您的配置,所以我不知道其余的情况。