# SSL location keep redirecting to non-SSL

**URL:** <https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413>\
**Category:** NGINX\
**Created:** [September 3, 2011, 2:52am UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413 "2011-09-03T02:52:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [September 3, 2011, 2:52am UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/1 "2011-09-03T02:52:03Z")

</div>

Ok…my brain’s been hurting on this one tonight.

My site’s been running fine on nginx for years. have SSL and non-SSL  
locations. PHP runs through .shtml files

Working on a Facebook app and right now need to have it work with both  
the SSL and non-SSL URL. Testing the coding on my server right now.

If I go to [http://www.example.com/myappsname](http://www.example.com/myappsname) the page loads.  
If I go to [https://www.example.com/myappsname/index.shtml](https://www.example.com/myappsname/index.shtml) the page  
loads. \<-note https  
If I go to [https://www.example.com/myappsname/](https://www.example.com/myappsname/) the page redirects to  
[http://www.example.com/myappsname](http://www.example.com/myappsname). \<-note https on first URL

Here are the relevant locations in the SSL server section:

location / {  
root /usr/local/apache/htdocs;  
rewrite ^(.+) [http://www.digitalhit.com$1](http://www.digitalhit.com$1) permanent;  
index index.shtml index.php;  
}

location ~ .(shtml|php|inc)$ {  
root /usr/local/apache/htdocs;  
include /usr/local/nginx/conf/fastcgi-php.conf;  
fastcgi\_pass 127.0.0.1:10004;  
}

location ^~ /myappsname/ {  
index index.shtml  
root /usr/local/apache/htdocs/;  
fastcgi\_intercept\_errors on;  
include /usr/local/nginx/conf/fastcgi-php.conf;  
fastcgi\_pass 127.0.0.1:10004;  
fastcgi\_param HTTPS on;  
}

I’ll probably smack my head when this is pointed out to me. All my other  
SSL locations work fine. Any idea why including the filename stays SSL  
but dropping it redirects?

Thanks.

---

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [September 3, 2011, 10:18am UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/2 "2011-09-03T10:18:07Z")

</div>

Been scratching my head on this one all night.

Been working on it a bit and now if I go to:  
[https://www.example.com/myfacebookapp/index.shtml](https://www.example.com/myfacebookapp/index.shtml) the page loads but  
[https://www.example.com/myfacebookapp](https://www.example.com/myfacebookapp) is tossing a 404

I’m really boggled here because the other subdirs are woring and the  
only difference is they’re password protected.

Here’s the full ssl server conf section:

server {  
server\_name [www.example.com](http://www.example.com);  
listen 443;  
root /usr/local/apache/htdocs/;

ssl on;  
ssl\_certificate /usr/local/apache/conf/server.pem;  
ssl\_certificate\_key /usr/local/apache/conf/server.key;  
ssl\_session\_timeout 5m;  
error\_page 404 /dhe404.shtml;

location / {  
rewrite ^(.+) [http://www.example.com$1](http://www.example.com$1) permanent;  
index index.shtml index.php;  
}

location ~ .(shtml|php|inc)$ {  
include /usr/local/nginx/conf/fastcgi-php.conf;  
fastcgi\_pass 127.0.0.1:10004;  
}

location ^~ /myfacebookapp/ {  
fastcgi\_intercept\_errors on;  
include /usr/local/nginx/conf/fastcgi-php.conf;  
fastcgi\_pass 127.0.0.1:10004;  
fastcgi\_param HTTPS on;  
}

location ^~ /subdir2/ {  
index index.php;  
fastcgi\_intercept\_errors on;  
include /usr/local/nginx/conf/fastcgi-php.conf;  
fastcgi\_pass 127.0.0.1:10004;  
fastcgi\_index index.php;  
fastcgi\_param HTTPS on;  
}

location ^~ /subdir3/ {  
fastcgi\_intercept\_errors on;  
include /usr/local/nginx/conf/fastcgi-php.conf;  
fastcgi\_pass 127.0.0.1:10004;  
fastcgi\_param HTTPS on;  
auth\_basic “example”;  
auth\_basic\_user\_file /usr/local/apache/passwd/passwords;  
}

location /subdir4/ {  
index index.shtml index.php index.html;  
auth\_basic “example”;  
auth\_basic\_user\_file /usr/local/apache/passwd/passwords;  
}

location /subdir5/ {  
index index.html;  
auth\_basic “example”;  
auth\_basic\_user\_file /usr/local/apache/passwd/passwords;  
}

location ~\* ^.+.(png|gif)$ {  
expires off;  
}

# serve static files directly

location ~\* ^.+.(css|gif|jpe?g|bmp|js|png|ico)$ {  
expires 30d;  
}

}

---

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [September 3, 2011, 11:21am UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/3 "2011-09-03T11:21:02Z")

</div>

still expermineting…just looked at the debug log. Even if I change the  
location to location = /myfacebookapp a call to myfacebookapp still  
isn’t matching the location.

Really don’t get what’s happening here. Why are my other ssl locations  
working?

---

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [September 3, 2011, 11:24am UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/4 "2011-09-03T11:24:44Z")

</div>

You should set a serverwide index directive. You should move index  
index.shtml index.php; from location / {} to server {}

Also your rewrite rule rewrite ^(.+) [http://www.example.com$1](http://www.example.com$1) permanent;  
sould be rewritten into rewrite ^ http://www.example.com$request\_uri?  
permanent;

Take a look at [Pitfalls and Common Mistakes | NGINX](http://wiki.nginx.org/Pitfalls)

---

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [September 3, 2011, 11:35am UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/5 "2011-09-03T11:35:12Z")

</div>

On 03/09/2011 5:23 AM, Calin D. wrote:

> You should set a serverwide index directive. You should move index  
> index.shtml index.php; from location / {} to server {}
> 
> Also your rewrite rule rewrite ^(.+) [http://www.example.com](http://www.example.com)  
> [http://www.example.com/](http://www.example.com/)$1 permanent; sould be rewritten into rewrite ^  
> [http://www.example.com](http://www.example.com) [http://www.example.com/](http://www.example.com/)$request\_uri? permanent;

Made your suggested changes.

After doing so tested location = /myfacebookapp which redirected to  
http, not what I want and location ~ /myfacebookapp which tossed a  
404…also not what I want.

5:34 am…argh. 🙂

---

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [September 3, 2011, 12:18pm UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/6 "2011-09-03T12:18:59Z")

</div>

On 03/09/2011 5:23 AM, Calin D. wrote:

> Also your rewrite rule rewrite ^(.+) [http://www.example.com](http://www.example.com)  
> [http://www.example.com/](http://www.example.com/)$1 permanent; sould be rewritten into rewrite ^  
> [http://www.example.com](http://www.example.com) [http://www.example.com/](http://www.example.com/)$request\_uri? permanent;

Just wondering…instead of a blanket redirect and then location that  
try to overide it, would it be better to have a rewrite that says “if  
not subdir1|subdir2|myfacebookapp rewrite to http”. Then locations for  
those subdirs if they require passwords, etc.

How would I do the if not subdir1|subdir2|myfacebookapp rewrite to http  
check?

---

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [September 3, 2011, 12:25pm UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/7 "2011-09-03T12:25:46Z")

</div>

On 03/09/2011 6:14 AM, Stefan C. wrote:

> This looks odd to me, I’ve never seen a rewrite in an ssl config. It  
> will complicate things.
> 
> Leave the index to the server config, and take out the location / rewrite.
> 
> Simplify.  
> Stefan,

The only reason I had the rewrite in there (and I think it was  
recommended to me years ago) was to prevent people or bots from calling  
the non-ssl directories of the site with https and creating duplicates  
in Google, etc. e.g. calling [http://www.example.com/](http://www.example.com/) and  
[https://www.example.com/](https://www.example.com/) when my homepage doesn’t need https.

I only needs https calls on my phpMyAdmin, webmail, facebookapp and site  
admin subdirectories. I just wrote another email in the thread asking if  
I should only do a rewrite if it’s NOT those directories.

---

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [September 3, 2011, 12:14pm UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/8 "2011-09-03T12:14:41Z")

</div>

On Sat, Sep 3, 2011 at 4:17 AM, Ian E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> location / {  
> rewrite ^(.+) [http://www.example.com$1](http://www.example.com$1) permanent;  
> index index.shtml index.php;  
> }

This looks odd to me, I’ve never seen a rewrite in an ssl config. It  
will complicate things.

Leave the index to the server config, and take out the location /  
rewrite.

Simplify.

Get the general app working, and then apply specialized rules for  
directories and urls. Remember, if you have told nginx to do something  
that it _can_ do, the logs won’t show much except that it’s doing what  
it’s been told.

---

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [September 3, 2011, 9:51pm UTC](https://www.ruby-forum.com/t/ssl-location-keep-redirecting-to-non-ssl/211413/9 "2011-09-03T21:51:32Z")

</div>

After a few hours sleep, it appears that the location was missing:

fastcgi\_index index.shtml;

Seems to work now.
