看到标题描述的这个问题,感觉很奇怪。具体如下:
1) 创建了带有“a1”的 A 记录。表示子域名为:a1.example.com
,
2)通过创建目录"sudo nano /var/www/a1.example.com/html/"
3)创建服务器块"sudo nano /etc/nginx/sites-available/a1.example.com"
4)服务器块内部核心部分:
server {
listen 80;
listen [::]:80;
root /var/www/a1.example.com/html;
index index.html index.htm;
server_name a1.example.com
location / {
try_files $uri $uri/ =404;
}
}
5)创建链接:sudo ln -s /etc/nginx/sites-available/a1.example.com /etc/nginx/sites-enabled/
6)sudo nano /etc/nginx/nginx.conf
,取消注释:server_names_hash_bucket_size 64;
确认:include /etc/nginx/sites-enabled/*;
是否有
7)重新启动nginx:sudo service nginx restart
这样做之后,第一个子域名就可以a1.example.com
正常工作了。
b2.example.com
但当我尝试按照上述步骤创建第二个子域名时,b2.example.com
却无法正常工作:它将重定向到主页,但 URL 仍然保持为b2.example.com
有什么问题呢?
非常感谢!
配置a1.example.com
server {
listen 80;
listen [::]:80;
root /var/www/a1.example.com/html;
index index.php index.html index.htm;
server_name a1.example.com;
rewrite ^/archives/(\d+)$ http://a1.example.com/?p=$1 permanent;
location / {
try_files $uri $uri/ /index.php?q=$uri&args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
配置b2.example.com
server {
listen 80;
listen [::]:80;
root /var/www/b2.example.com/html;
index index.php index.html index.htm;
server_name b2.example.com;
rewrite ^/archives/(\d+)$ http://b2.example.com/?p=$1 permanent;
location / {
try_files $uri $uri/ /index.php?q=$uri&args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
curl -I b2.example.com:
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Thu, 04 Feb 2016 05:54:22 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Set-Cookie: PHPSESSID=4r143k0o1m1l62p3g4vt1jrav1; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Link: <http://www.example.com/wp-json/>; rel="https://api.w.org/"
Link: <http://www.example.com/>; rel=shortlink
curl -I a1.example.com
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Thu, 04 Feb 2016 05:54:32 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Link: <http://a1.example.com/wp-json/>; rel="https://api.w.org/"