Question

How to I redirect from one subdomain:2206 (HTTPS) to another subdomain:8206 (HTTP & HTTP/2.0 - no upgrade)

Posted March 19, 2021 147 views
Nginx

Currently, I’m using Microsoft Azure VM installed with nginx to host and run my webserver for a project of mine. I have two subdomains (server-block-1.test.com:2206 & server-block-2.test.com:8206) Both subdomain is pointing towards the same IP address. Currently, I have no issue accessing both subdomains, however, my webpage would crash and not load when I tried to redirect from one subdomain to another and vice versa. (Problem example below)

Current domains:
server-block-1.test.com:2206 (HTTPS)
server-block-2.test.com:8206 (HTTP & HTTP/2.0 - No upgrade)
both subdomains are only accessible by their own ports, which means server-block-2 can never access port 2206 and server-block-1 can never access port 8206

Redirecting Example:
server-block-2.test.com:2206 would redirect to server-block-2.test.com:8206
&
server-block-1.test.com:8206 would redirect to server-block-1.test.com:2206

Problem:
I was able to redirect the URLs, but my webpage would crash after I tested either one of the redirection. Base on my server-block-1 & 2 configurations below, I add an if condition to return to the appropriate subdomains and ports such that it would redirect base on the “Redirecting Example” shown above. However, as mention, once the redirection was executed, my web page would crash and no longer load for their domains and the web page would only come back online after a couple of minutes being down. How can I solve this redirecting problem ?

Server-Block-1 configuration:

server {

        root /var/serverblock1/test.ml;

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

        server_name server-block-1.test.ml;

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

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

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

    listen [::]:2206 ssl ipv6only=on; # managed by Certbot
    listen 2206 ssl; # managed by Certbot
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_certificate /etc/letsencrypt/live/server-block-1.test.ml/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/server-block-1.test.ml/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    error_page 497 =301 https://$host:$server_port$request_uri; # redirect http to https
    if ($host != server-block-1.test.ml) {
        return 301 http://$host:8206$request_uri;
    }

}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

server {
    if ($host = server-block-1.test.ml) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name server-block-1.test.ml;
    return 404; # managed by Certbot


}

Server-Block-2 Configuration:

server {

        listen 8206 http2;
        listen [::]:8206 http2;

        root /var/serverblock2/test.ml;

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

        server_name server-block-2.test.ml;

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

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
                # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
        if ($host != server-block-2.test.ml) {
                return 301 https://$host:2206$request_uri;
        }

}

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi,

Can you check if it works when you remove http2 parameter in Server-Block-2 config file ? So, it would look like:

server {
        listen 8206;
        listen [::]:8206;
        root /var/serverblock2/test.ml;
        ...
}