Johan S
Johan S

Reputation: 3591

NGINX redirect HTTP to HTTPS with angular hash-tag syntax

I have a server I want to run only in HTTPS. I'm running Node and Nginx on Amazon AMI.

To redirect to HTTPS I simply put this in my nginx.conf file:

server {
    listen 80;
    server_name my_server;
    return         301 https://$server_name$request_uri;
}

And then the SSL is configured. However when Angular urls such as http://www.mypage.com/#/whatever comes in, the resulting redirect is https://www.mypage.com/ without the vital Angular syntax.

How can I make it work for the Angular hash-tag syntax? Nginx version is 1.6.2.

Thanks

Upvotes: 1

Views: 1238

Answers (1)

JohnnyJS
JohnnyJS

Reputation: 1472

This is not possible. Im not going to talk about nginx best approach of redirecting to different protocol.

The problem is that hash is never sent to server.

Every browser that i know will ignore the hash portion in the URL.

So if for example you will send this URL to example server:

http://localhost/6/?e#hash_here

The request will send:

http://localhost/6/?e

The browser simply strip it off.

From JS's view i will recommend using some EventListener on onhashchange, so you can intercept all requests and store the location.hash in a cookie.

Now when this request will arrive to the server you will be able to use nginx's $http_cookie (it will be something like angularLocation=#/whatever so use some regex to find if it is exist)

Upvotes: 6

Related Questions