Score:1

Redirecting specific subdomain with NGINX to point to a different website while using SSL

ky flag

I have the following NGINX configuration:

# HTTP Redirect
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

# HTTPS Server
server {
    # Port to listen on, can also be set in IP:PORT format
    listen  443 ssl;

    ssl_certificate      bitnami/certs/server.crt;
    ssl_certificate_key  bitnami/certs/server.key;

    include  "/opt/bitnami/nginx/conf/bitnami/*.conf";

    location /status {
        stub_status on;
        access_log   off;
        allow 127.0.0.1;
        deny all;
    }

    error_page 404 403 500 503 /404.html;
    location = /404.html {
        root /opt/bitnami/nginx/html;
        internal;
    }
}

This works spectacularly well for what it does, has my website and redirects any HTTP to HTTPS as needed. What I would like to do now is given a subdomain, such as media.website.com, I would like to redirect it to a different domain/location, but leave everything intact. (i.e. if I go to website.com it still takes me to my main site.) I also want this redirect to work for both HTTP and HTTPS.

How can I accomplish this?

Score:1
by flag

You should add new server blocks so you can handle your specific subdomain, here your configuration file with the 2 new blocks

# HTTP Redirect
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

# HTTPS Server
server {
    listen  443 ssl;

    ssl_certificate      bitnami/certs/server.crt;
    ssl_certificate_key  bitnami/certs/server.key;

    server_name website.com www.website.com;

    include  "/opt/bitnami/nginx/conf/bitnami/*.conf";

    location /status {
        stub_status on;
        access_log   off;
        allow 127.0.0.1;
        deny all;
    }

    error_page 404 403 500 503 /404.html;
    location = /404.html {
        root /opt/bitnami/nginx/html;
        internal;
    }
}

# Redirect media.website.com (HTTP)
server {
    listen 80;
    server_name media.website.com;

    return 301 https://media.website.com$request_uri;
}

# Redirect media.website.com (HTTPS)
server {
    listen 443 ssl;
    server_name media.website.com;

    ssl_certificate      bitnami/certs/server.crt;
    ssl_certificate_key  bitnami/certs/server.key;

    return 301 https://newdomain.com$request_uri;
}

We also added the server_name directive to the existing HTTPS server block to explicitly specify the main domain and its www subdomain

Rietty avatar
ky flag
Hi I just want to say this worked perfectly, thank you so much!
Saxtheowl avatar
by flag
You welcome :))
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.