# Redirect loop in combination with https and apache

**URL:** <https://www.ruby-forum.com/t/redirect-loop-in-combination-with-https-and-apache/238203>\
**Category:** NGINX\
**Created:** [December 23, 2013, 11:07am UTC](https://www.ruby-forum.com/t/redirect-loop-in-combination-with-https-and-apache/238203 "2013-12-23T11:07:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [December 23, 2013, 11:07am UTC](https://www.ruby-forum.com/t/redirect-loop-in-combination-with-https-and-apache/238203/1 "2013-12-23T11:07:44Z")

</div>

Hi,

We have a setup with tomcat/apache and nginx.

When a redirect occurs from the application from https to http, the  
nginx  
gets trapped in a redirect loop.

In the apache configuration we have this setting:

ExpiresActive On ExpiresDefault "access plus 1 month" ExpiresByType image/gif "access plus 1 year" ExpiresByType image/jpeg "access plus 1 year" ExpiresByType image/png "access plus 1 year" .... more mimetypes

The nginx configuration has this location

```
location / {
    proxy_pass http://def-t-site1/;
    proxy_http_version 1.1;
    proxy_hide_header Expires;
    proxy_hide_header Last-Modified;
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_cache http_def;
    proxy_cache_key $scheme://$host$uri$is_args$args;
    # proxy_cache_key "$scheme://$host$request_uri";
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;
    add_header Cache-Control "public";
    add_header X-Cache-Status $upstream_cache_status;
    add_header X-Via $hostname;
}

```

a similar setting we have for 443 port (https).

If we remove ExpiresDefault “access plus 1 month” from apache, the  
redirect  
loop does not occur. The cache of nginx uses a TTL of 1 month after the  
redirect occurs. This causes a redirect loop, since the https request is  
cached as well.

So far, we tried several things

- proxy\_cache\_valid 200 301 0m;  
No change in the TTL. The redirect loop is not solved, and the TTL is  
still  
a month.

Then we tried to configure the expire headers in nginx. That solves the  
redirect, but unfortunately  
the expire headers are not set. We tried for instance:

if ($upstream\_http\_content\_type ~ “image/jpeg”) {  
expires 2m;  
}

or  
map $upstream\_http\_content\_type $new\_cache\_control\_header\_val {  
default $upstream\_http\_cache\_control;  
“~\*image/jpeg” “max-age=120, must-revalidate”;  
}

but these settings did not have any effect on the TTL of the images.

So, is there a way to avoid the redirect loop and set the expire header  
per  
mimetype in nginx ?

Thanks!
