Nginx server redirect loop after SSL

301-redirectnginxUbuntu

So i've had some problems getting SSL to work on my Nginx server. After some time trying and following tutorials i've finally got it to work.

Then when i wanted to access the hipchat server i got suprised with a message that says: "the page isn't redirecting properly".

When i look at the network tab in inspect elements i see the following.
Status code: 301 moved permanently.

Before i got SSL to work, the website worked. But after i got SSL working it keeps looping.

Here is the content of my .conf file that i've put in the /etc/nginx/sites-available folder.

            # This is your Hipchat node's DNS name
            upstream chat {
                server hipchat.example.nl:80;    
                keepalive 32;
            }

            # HTTP to HTTPS redirection
            server {
                listen         80;
                server_name    hipchat.example.nl;
                return         301 https://$host$request_uri;
            }

            # 
            server {
                listen                  443;
                server_name             hipchat.example.nl;
                ssl_certificate /example.com.chained.crt;
                ssl_certificate_key /example.com.key;
                ssl on;
                ssl_session_cache  builtin:1000  shared:SSL:10m;
                ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
                ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
                ssl_prefer_server_ciphers on;

                location / {
                    proxy_http_version          1.1;
                    proxy_set_header Connection "";
                    proxy_set_header            Host $host;
                    proxy_set_header            X-Real-IP $remote_addr;
                    proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header            X-Forwarded-Proto $scheme;
                    proxy_read_timeout          90;
                    proxy_pass                  http://chat;
                }
            }

This might just be a starters mistake since i've never done anything like this, but can someone help me with fixing this problem?`

edit:
So i've just come a little bit further (maybe). But now i cant seem to find the chat.
First i've changed my proxy pass to http://127.0.0.1:8095;
After that i have restarted nginx, then when i try to connect to the website i get access to a standard Atlassian Crowd page. Which is weird because i am supposed to go to hipchat, which works when i remove all references of SSL.

Best Answer

This is your Hipchat node's DNS name

            upstream chat {
                server hipchat.example.nl:80;
                keepalive 32;
            }

            # HTTP to HTTPS redirection
            server {
                listen         80;
                server_name    hipchat.example.nl;
                location / {
                return         301 https://$server_name$request_uri;
                }
            }

            #
            server {
                listen                  443 ssl;
                ssl on;
                server_name             hipchat.example.nl;
                client_max_body_size 10m;
                ssl_session_timeout 180m;

                location / {
                    proxy_pass                  http://localhost:8080;
                }
            }

It was basically fixed when i changed the proxy pass to localhost and changed my connector in Tomcat's config. Here you can read more about it if you need it guide