Nginx从不存在的子域重定向

时间:2016-07-05 17:26:47

标签: redirect ssl nginx config

我有https://example.com

还有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重定向到https://example.com

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        return 301 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。

1 个答案:

答案 0 :(得分:0)

  

但是,如果我请求https://xxx.example.com,它会像admin.example.com一样打开SSL证书错误。但是我需要像xxx.example.com请求一样重定向它。

没办法。重定向需要有效的证书,因为重定向仅在检查证书后才会发生。如果没有与URL匹配的证书,则只会出现证书错误,并且无法到达重定向。

相关问题