Nginx HTTPS子域重定向

时间:2018-02-12 12:39:46

标签: redirect nginx https

我有几个网站配置了Nginx(nginx / 1.10.3(Ubuntu))服务器块。浏览到下面列出的任何已配置域都可以按预期工作:

List of configured Server Blocks

配置为使用HTTPS的那些获取自动HTTP - > HTTPS重定向(例如morgrowe.com)。问题是当我浏览到不存在的子域时(例如:doesnotexist.morgrowe.com)。如果我转到 http ://doesnotexist.morgrowe.com,我会被重定向到默认的Nginx页面(这就是我想要的)。但是,如果我转到 https ://doesnotexist.morgrowe.com,我会被重定向到https://api.morgrowe.com

HTTPS redirect example

这不会太糟糕,但如果我转到https://doesnotexist.carpyslocksmiths.com,我也会被重定向到https://api.morgrowe.com

我在哪里可以找到实现这一目标的配置?理想情况下,我希望默认的nginx 404页面与http连接一样。我想我必须在/etc/nginx/sites-available/default中配置一些内容,但我不知道从哪里开始。这是我的/etc/nginx/sites-available/default文件内容:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php7.0-fpm:
    #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

1 个答案:

答案 0 :(得分:1)

使用 _ _ 的默认“全部捕获”服务器阻止,用于http和https。 _ 只是一个无效的值,永远不会在真实主机上触发。

请参阅示例从下面的链接 https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/