Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to redirect https site to another url - nginx server block

I am trying to attach a promo subdomain to my site which is already on https, and then use a redirect url to redirect to another page in the site. For eg., basically if my site were https://example.com and had a page https://example.com/xyz/xyz/promo then I want a browser redirect when I type in https://promo.example.com to this page. I have set up all the relevant AWS route 53 settings.

My nginx server blocks have this

   server {                                                                                                                                                                             
            listen 80 default_server;
            listen [::]:80 default_server;
        return 301 https://example.com$request_uri;
    }

    server {
            server_name www.example.com;
            return 301 https://example.com$request_uri;
    }

    server {
        server_name example.com;
        return 301 https://example.com$request_uri;
    }

    server {
        server_name promo.example.com;
        return 301 https://example.com/xyz/xyz/promo;
    }
ssl_certificate /..path/..;
ssl_certificate_key //..path/..;
ssl_dhparam /..path/...;
ssl_trusted_certificate /..path/..;

add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload';
ssl_prefer_server_ciphers on;
ssl_ciphers .......; //hidden
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
ssl_session_cache   shared:SSL:10m;
ssl_session_timeout 10m;
ssl_buffer_size 1400;
spdy_headers_comp 0;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=86400;
resolver_timeout 10;

server {
    listen 443 ssl spdy;
    server_name example.com;
    include /etc/nginx/helper.conf;
    root /var/www/example/  ;
    index index.php index.html;
    charset utf-8;

    location / {
            add_header "Access-Control-Allow-Origin" "*";
            try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location ~ /\.ht {
            deny all;
    }
} 

Current behaviour:

It redirects correctly when I type in promo.example.com directly without the https. But if I type in https://promo.example.com it just shows me example.com, with the url being https://promo.example.com

Expected behaviour:

If I type in https://promo.example.com, it should redirect to https://example.com/xyz/xyz/promo

I can't put https://promo.example.com and then redirect with the server blocks, because nginx throws an error.

How can I redirect https://promo.example.com to go to https://example.com/xyz/xyz/promo

like image 745
Vrashabh Irde Avatar asked Aug 07 '15 12:08

Vrashabh Irde


People also ask

How do I redirect HTTPS to NGINX?

Nginx Redirect all HTTP traffic to HTTPS Here is a breakdown of the commands: Listen 80 : This instructs the system to catch all HTTP traffic on Port 80. Server_name _; : This will match any hostname. Return 301 : This tells the browser (and search engines) that this is a permanent redirect.


2 Answers

Due to use Strict-Transport-Security header the browser provides 301 redirect automatically by itself so this server block has never been used:

server {
    server_name promo.example.com;
    return 301 https://example.com/xyz/xyz/promo;
}

Port redirection 80->443 happens before even browser connected to server, so Nginx always serves latest server block based on port 443. This should help you:

server {
    listen 443 ssl;
    listen 80;
    server_name promo.example.com;
    return 301 https://example.com/xyz/xyz/promo;
}
like image 78
Anatoly Avatar answered Oct 06 '22 02:10

Anatoly


Try this:

server {
server_name promo.example.com;
rewrite ^ https://example.com/xyz/xyz/promo permanent;
like image 23
Curlas Avatar answered Oct 06 '22 00:10

Curlas