我在同一台服务器上托管了两个域.在DNS记录中,我有通配符(*)两个域的记录都指向服务器.
所以我期待xyz.domain1.com解析为domain1.com
和xyz.domain2.com到domain2.com.
但是目前除了domain2.com上的www子域之外的所有内容都将重定向到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;
}
}
}
如何将域保持分离,以便每个域的子域都重定向到正确的域?
最佳答案
原文链接:https://www.f2er.com/nginx/435188.html