我的 Apache 配置出现问题,当我输入包含 WWW (https://www.example.com)但当我不这样做时它工作正常(https://example.com)。
我尝试添加单独的行:
- 服务器别名 www.example.com
- 服务器别名 *example.com
对于所有的VirtualHosts但这并没有什么区别。
从我的测试中,我看到重定向有效,但我只能通过某些 URL 访问主站点:
我想知道是否有人能帮我解决这个问题,这样我就可以从任何 URL 访问我的主站点。另外,我很快就要向客户演示这个站点,所以我正在努力寻找解决方案!
这是我的 Apache 配置:
000-默认-conf
<VirtualHost *:80>
Alias /static /home/user/project_1/static
<Directory /home/user/project_1/static>
Require all granted
</Directory>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
默认 SSL 配置文件
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
Alias /static /var/www/example.com/static
<Directory /home/user/project_1/static>
Require all granted
</Directory>
Alias /media /home/user/project_1/media
<Directory /home/user/project_1/media>
Require all granted
</Directory>
...
...
<Directory /home/user/project_1/mywebapp>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess user python-home=/home/user/project_1/env_mywebapp python-path=/home/user/project_.......
WSGIProcessGroup mywebapp
WSGIScriptAlias / /home/user/project_1/mywebapp/wsgi.py
ServerName example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
le-redirect-example.com.conf (由 let's encrypt certbot 创建)
<VirtualHost _default_:80>
ServerName example.com
ServerSignature Off
RewriteEngine On
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ErrorLog /var/log/apache2/redirect.error.log
LogLevel warn
</VirtualHost>
感谢您的帮助!
[更新]
以下是运行命令“curl -I”的输出
root@host1:~# curl -I www.example.com
HTTP/1.1 301 Moved Permanently
Date: Mon, 16 Apr 2018 18:23:20 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://example.com/
Content-Type: text/html; charset=iso-8859-1
root@host1:~# curl -I example.com
HTTP/1.1 301 Moved Permanently
Date: Mon, 16 Apr 2018 18:24:31 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://example.com/
Content-Type: text/html; charset=iso-8859-1
root@host1:~# curl -I https://example.com
HTTP/1.1 200 OK
Date: Mon, 16 Apr 2018 18:25:23 GMT
Server: Apache/2.4.18 (Ubuntu)
Vary: Cookie,Accept-Encoding
X-Frame-Options: SAMEORIGIN
Set-Cookie: csrftoken=zxxxxxxxxxxxxxxxxxxxxxzBcrBOvUDUdxxxxxxxxxxxxxxxx; expires=Mon, 15-Apr-2019 18:25:23 GMT; Max-Age=31449600; Path=/
Content-Type: text/html; charset=utf-8
root@host1:~# curl -I https://www.example.com
HTTP/1.1 200 OK
Date: Mon, 16 Apr 2018 18:26:15 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Tue, 03 Jan 2017 02:48:33 GMT
ETag: "1234-12347b43...."
Accept-Ranges: bytes
Content-Length: 11321
Vary: Accept-Encoding
Content-Type: text/html
答案1
你的配置中有这个:
ServerName example.com
这与 example.com 匹配,但不与 www.example.com 匹配。这是两个不同的名称。如果您使用 https(根据配置,您确实这样做了),您还需要具有多个名称、通配符的证书或针对 example.com 和 www.example.com 的单独证书。
用于ServerAlias www.example.com
为 ServerName 添加别名。这也记录在优秀的 Apache 文档,当出现问题时,这应该是第一个停止的地方:
当请求到达时,服务器将根据请求使用的 IP 地址和端口找到最佳(最具体)匹配参数。如果有多个虚拟主机包含此最佳匹配地址和端口组合,Apache 将进一步将 ServerName 和 ServerAlias 指令与请求中存在的服务器名称进行比较。