所以我已經有了這樣的設置,NGINX 似乎運作得很好。但現在的要求是增加對動態子域的支援。不知道該怎麼做。幫助將不勝感激。也能夠針對網域和子網域從 www 重新導向到非 www。我在網上搜索過,但似乎沒有任何方法適用於此解決方案。
我正在嘗試做什麼:目前我對普通非子網域進行了此設定。例如 exampleDomain.com 或www.exampleDomain.com。現在我想介紹一下動態子網域。例如 sub1.exampleDomain.com 或 sub50.exampleDomain.com。我遇到的問題是,如果存在子網域,我無法正確地重新導向到子網域。例如,如果有人造訪 sub2.exampleDomain.com,它將被重定向到 exampleDomain.com。我的問題是如何將子網域引入此設置,以便www.sub3.exampleDomain.com前往 sub3.exampleDomain.com/ sub3.exampleDomain.com 前往 sub3.exampleDomain.com 並www.exampleDomain.com前往 exampleDomain.com,exampleDomain.com 前往 exampleDomain.com。 (如果這有道理的話:))
server {
listen 80;
listen [::]:80;
server_name exampleDomain.com www.exampleDomain.com;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
return 301 https://exampleDomain.com$request_uri?;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.exampleDomain.com;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/exampleDomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/exampleDomain.com/privkey.pem;
ssl_buffer_size 8k;
ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
ssl_ecdh_curve secp384r1;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8;
return 301 https://exampleDomain.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name exampleDomain.com;
server_tokens off;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_min_length 500;
gzip_buffers 4 32k;
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml image/svg;
gzip_vary on;
ssl_certificate /etc/letsencrypt/live/exampleDomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/exampleDomain.com/privkey.pem;
ssl_buffer_size 8k;
ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
ssl_ecdh_curve secp384r1;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8;
location ~* \.(jpg|jpeg|png|gif|ico)$ {
expires 365d;
}
location / {
try_files $uri @nodejs;
}
location @nodejs {
proxy_pass http://nodejs:8080;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
# add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# enable strict transport security only if you understand the implications
}
location /graphql {
proxy_pass http://nodejs:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
}
答案1
舉個例子怎麼樣手動的:
server {
server_name ~^(www\.)?(?<domain>.+)$;
location / {
root /sites/$domain;
}
}
只要適應你的環境。
server_name ~^(www\.)?(?<subdom>[^\.]*)\.your-external-domain\.com$;
location / {
root /sites/$subdom;
}
這僅適用於子網域,忽略 www。