2016-07-05 64 views
0

我有https://example.comNginx的非現有子域

重定向也有https://admin.example.com

https://forum.example.com

當請求example.com或admin.example.com或forum.example.com nginx的重定向到SSL連接(https://example.com等..)

example.com.conf:

server { 
charset utf-8; 
client_max_body_size 128M; 

listen 443 ssl; 

server_name example.com www.example.com; 
root  /var/www/example.com/...; 
index  index.php; 

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
ssl_prefer_server_ciphers on; 
ssl_dhparam /etc/ssl/certs/dhparam.pem; 
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AE$ 
ssl_session_timeout 1d; 
ssl_session_cache shared:SSL:50m; 
ssl_stapling on; 
ssl_stapling_verify on; 
add_header Strict-Transport-Security max-age=15768000; 

access_log /var/log/nginx/example.com-access.log; 
error_log /var/log/nginx/example.com-error.log; 

location/{ 
    # Redirect everything that isn't a real file to index.php 
    try_files $uri $uri/ /index.php$is_args$args; 
} 

location ~* \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { 
    try_files $uri =404; 
    access_log off; 
    expires  7d; 
} 
error_page 404 /404.html; 

location ~ \.php$ { 
    try_files $uri =404; 
    fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    include fastcgi_params; 
} 

location ~ /\.(ht|svn|git) { 
    deny all; 
} 

# let's encrypt (ssl folder) 
location ~ /.well-known { 
    allow all; 
} 
} 

server { 
     listen 80; 
     listen [::]:80; 
     server_name example.com www.example.com; 
     return 301 https://$server_name$request_uri; 
    } 

如果我請求不存在的子域等xxx.example.com它經由default.conf

server { 
     listen 80 default_server; 
     listen [::]:80 default_server; 
     server_name _; 
     return 301 https://example.com; 
} 

重定向到https://example.com但是,如果我請求https://xxx.example.com它打開像admin.example.com與SSL SERT錯誤。但我需要重定向它也像xxx.example.com請求。

如果我追加default.conf與

server { 
     listen 443 ssl default_server; 
     listen [::]:443 ssl default_server; 
     server_name _; 
     return 301 https://example.com; 
} 

我越來越ERR_CONNECTION_REFUSED我的所有主機。

+0

清除default.conf中的default_server並將其添加到example.com.conf。是的,當請求'https://xxx.example.com'時,我有sert錯誤,但在重定向到'https://example.com'後,我想要。謝謝你們! – ZeiZ

回答

0

但是,如果我要求https://xxx.example.com它打開像admin.example.com SSL證書錯誤。但我需要重定向它也像xxx.example.com請求。

沒辦法。重定向需要有效的證書,因爲只有在檢查證書後重定向纔會發生。如果沒有與URL匹配的證書,則只能獲得證書錯誤,並且不會達到重定向。

+0

非常感謝。我瞭解證書錯誤,但爲什麼任何不存在的子域(如https://xxx.example.com)都能訪問https://admin.example.com? – ZeiZ

+0

@ZeZ:因爲它們全部在同一個IP地址上,你可以在IP地址上監聽SSL,否則你不會。如果沒有與請求的名稱匹配的特定配置,它只會使用這個IP地址的默認條目,這恰好就是您的管理子域名。 –

+0

「此IP地址的默認條目恰好是您的管理員子域名」。所以,如果我不想讓管理員作爲默認條目?我需要做什麼? – ZeiZ