cha*_*rsi 3 domain-name-system nginx
我在同一台服务器上托管了两个域。在 DNS 记录中,两个域的通配符 (*) A 记录都指向服务器。
所以我期待xyz.domain1.com以决心domain1.com
及xyz.domain2.com对domain2.com。
但是,目前除www子域之外的所有内容都domain2.com重定向到domain1.com.
我对两个域都有相同的 nginx 配置,所以我不明白是什么原因造成的。这就是我的 nginx 配置的样子——
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout main;
sendfile on;
keepalive_timeout 65;
# Listen for non-HTTPS requests and redirect them to HTTPS
server {
server_name www.domain1.com domain1.com;
return 301 https://domain1.com$request_uri;
}
# Listen for www requests with HTTPS and redirect them to non www site
server {
listen 443 ssl;
server_name www.domain1.com;
ssl_certificate /etc/letsencrypt/live/www.domain1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.domain1.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
return 301 https://domain1.com$request_uri;
}
# Listen for non-www HTTPS requests and serve the app
server {
listen 443 ssl;
#add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
server_name domain1.com api.domain1.com;
ssl_certificate /etc/letsencrypt/live/www.domain1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.domain1.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location ^~ /.well-known/ {
root /usr/share/nginx/html;
allow all;
}
location / {
root /var/www/domain1;
}
}
# Listen for non-HTTPS requests and redirect them to HTTPS
server {
server_name www.domain2.com domain2.com;
return 301 https://domain2.com$request_uri;
}
# Listen for www requests with HTTPS and redirect them to non www site
server {
listen 443 ssl;
server_name www.domain2.com;
ssl_certificate /etc/letsencrypt/live/www.domain2.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.domain2.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
return 301 https://domain2.com$request_uri;
}
# Listen for non-www HTTPS requests and serve the app
server {
listen 443 ssl;
#add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
server_name domain2.com;
ssl_certificate /etc/letsencrypt/live/www.domain2.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.domain2.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location ^~ /.well-known/ {
root /usr/share/nginx/html;
allow all;
}
location / {
root /var/www/domain2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何将域分开,以便每个子域都重定向到正确的域?
您必须为每个域包含一个通配符 server_name 条目以选择要重定向到的域。否则未知的 server_names 将被分派到第一个条目(在这种情况下,重定向到 domain1.com)。
添加*.domain2.com到行中server_name www.domain2.com,它应该正确分离 domain2.com 子域。
| 归档时间: |
|
| 查看次数: |
527 次 |
| 最近记录: |