Home Nginx HTTPS subdomain redirect
Reply: 1

Nginx HTTPS subdomain redirect

MorganR
1#
MorganR Published in 2018-02-12 12:39:45Z

I have a couple of websites configured with Nginx (nginx/1.10.3 (Ubuntu)) Server Blocks. Browsing to any of the configured domains listed below works as expected:

Those that are configured to use HTTPS get automatic HTTP -> HTTPS redirects (morgrowe.com for example). The issue is when I browse to a subdomain that doesn't exist (for example: doesnotexist.morgrowe.com). If I go to http://doesnotexist.morgrowe.com, I get redirected to the default Nginx page (which is what I want). However, if I go to https://doesnotexist.morgrowe.com, I get redirected to https://api.morgrowe.com.

This wouldn't be so bad, but if I go to https://doesnotexist.carpyslocksmiths.com, I also get redirected to https://api.morgrowe.com.

Where can I find the configuration that makes this happen? Ideally, I'd like the default nginx 404 page to appear like it does for http connections. I imagine I have to configure something in /etc/nginx/sites-available/default, but I don't know where to start. Here's my /etc/nginx/sites-available/default file contents:

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;
    #}
}
solvease
2#
solvease Reply to 2018-02-12 14:32:05Z

use a Default “Catch All” Server Block using _ in server_name both for http and https. _ is just an invalid value which will never trigger on a real host.

Please see example From below link https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30361 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO