【问题标题】:Nginx HTTPS subdomain redirectNginx HTTPS 子域重定向
【发布时间】:2018-02-12 12:39:45
【问题描述】:

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

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

这还不错,但如果我转到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;
    #}
}

【问题讨论】:

    标签: redirect nginx https


    【解决方案1】:

    使用 _ 在 server_name 中为 http 和 https 使用默认的“Catch All”服务器块。 _ 只是一个无效值,永远不会在真实主机上触发。

    请看下面链接的例子 https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

    【讨论】:

    • 我对 Nginx 有点陌生。你介意提供一个例子吗?我会将这些更改保存在哪个文件中? /etc/nginx/sites-available/default?
    • 我需要你的配置文件来帮助你。
    • 我已将/etc/nginx/sites-available/default 的内容添加到我的原始帖子中。谢谢。
    • 哪个服务器阻止监听 443,从您的默认配置中取消注释监听 443 行
    猜你喜欢
    • 2015-08-26
    • 2018-04-27
    • 1970-01-01
    • 2021-03-26
    • 2017-05-05
    • 1970-01-01
    • 2021-03-25
    • 2021-02-02
    • 2017-02-14
    相关资源
    最近更新 更多