# Disable 301 redirect for directory / use relative redirect / change scheme

**URL:** <https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152>\
**Category:** NGINX\
**Created:** [August 26, 2015, 5:31pm UTC](https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152 "2015-08-26T17:31:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [August 26, 2015, 5:31pm UTC](https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152/1 "2015-08-26T17:31:12Z")

</div>

Hi,

I have this setup  
browser → ssl proxy → nginx  
browser to ssl proxy is https only  
ssl proxy to nginx is http only

now i browse to “[exemple.com](https://exemple.com/aaa)”, where aaa is a directory,  
so nginx send back a 301 redirect with “Location:  
[exemple.com](http://exemple.com/aaa/)”

Is it possible to send https instead of http (in Location),  
or send a relative header, like “Location: /aaa/”,  
or just disable this redirection (in my case it’s ok)

Thanks in advance  
Etienne

---

<div class="post-metadata">

**Author:** ![Etienne\_Champetier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/53a042/32.png) [@Etienne\_Champetier](https://www.ruby-forum.com/u/Etienne_Champetier)\
**Post date:** [August 26, 2015, 5:37pm UTC](https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152/2 "2015-08-26T17:37:38Z")

</div>

On Thu, Aug 27, 2015 at 12:30 AM, Etienne Champetier  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible to send https instead of http (in Location),  
> or send a relative header, like “Location: /aaa/”,  
> or just disable this redirection (in my case it’s ok)

see if proxy\_redirect[1] fits your need.

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

---

<div class="post-metadata">

**Author:** ![Etienne\_Champetier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/53a042/32.png) [@Etienne\_Champetier](https://www.ruby-forum.com/u/Etienne_Champetier)\
**Post date:** [August 26, 2015, 5:53pm UTC](https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152/3 "2015-08-26T17:53:24Z")

</div>

2015-08-26 17:36 GMT+02:00 Edho A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > so nginx send back a 301 redirect with "Location:
> 
> nginx isn’t a proxy here, it’s a backend (no proxy\_pass in my conf)  
> ssl proxy is a black box (not an nginx)

---

<div class="post-metadata">

**Author:** ![Etienne\_Champetier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/53a042/32.png) [@Etienne\_Champetier](https://www.ruby-forum.com/u/Etienne_Champetier)\
**Post date:** [August 26, 2015, 5:54pm UTC](https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152/4 "2015-08-26T17:54:34Z")

</div>

On 2015-08-26, 8:36 AM, Edho A. wrote:

> > so nginx send back a 301 redirect with “Location: [http://exemple.com/aaa/](http://exemple.com/aaa/)”  
> > nginx mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)  
> > use a rewrite 302 can easy help. If the client cant reach the intended  
> > site (say due to internal network/firewalls) then you can also Proxy the  
> > connection if needed.

here is how the 302 rewrite would look like:  
rewrite ^ https://$hostname$request\_uri? redirect;

You can also mix this with an if statement in case you only want  
“[http://example.com/aaa](http://example.com/aaa)” to be redirected to HTTPS and not everything

example:  
if ($host = [example.com](http://example.com)) {  
if ($uri = /aaa/) {  
rewrite ^ https://$hostname$request\_uri? redirect;  
}  
}

Something like that, this is untested but should work.  
Keep in mind that using IF like this has a performance cost (though  
small, it all depends on number of req/site load… something to keep in  
mind)

Cheers,  
Payam C.

---

<div class="post-metadata">

**Author:** ![Etienne\_Champetier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/53a042/32.png) [@Etienne\_Champetier](https://www.ruby-forum.com/u/Etienne_Champetier)\
**Post date:** [August 26, 2015, 6:22pm UTC](https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152/5 "2015-08-26T18:22:01Z")

</div>

On Thu, Aug 27, 2015 at 12:52 AM, Etienne Champetier  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > browser to ssl proxy is https only
> > 
> > see if proxy\_redirect[1] fits your need.
> > 
> > [Module ngx\_http\_proxy\_module](http://nginx.org/r/proxy_redirect)
> 
> nginx isn’t a proxy here, it’s a backend (no proxy\_pass in my conf)  
> ssl proxy is a black box (not an nginx)

whoops right, sorry.

This seems to work:

if (-d $request\_filename) { return 301 https://$host$uri/; }

---

<div class="post-metadata">

**Author:** ![Etienne\_Champetier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/53a042/32.png) [@Etienne\_Champetier](https://www.ruby-forum.com/u/Etienne_Champetier)\
**Post date:** [August 26, 2015, 6:29pm UTC](https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152/6 "2015-08-26T18:29:02Z")

</div>

2015-08-26 18:20 GMT+02:00 Edho A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > > I have this setup  
> > > > or just disable this redirection (in my case it’s ok)  
> > > > whoops right, sorry.
> 
> This seems to work:
> 
> if (-d $request\_filename) { return 301 https://$host$uri/; }

thanks, will try tomorrow

---

<div class="post-metadata">

**Author:** ![Etienne\_Champetier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/53a042/32.png) [@Etienne\_Champetier](https://www.ruby-forum.com/u/Etienne_Champetier)\
**Post date:** [August 26, 2015, 7:00pm UTC](https://www.ruby-forum.com/t/disable-301-redirect-for-directory-use-relative-redirect-change-scheme/243152/7 "2015-08-26T19:00:00Z")

</div>

Please try something like that:

proxy\_redirect http://$proxy\_host/ $scheme://$host/;

Regards,  
Biazus

Posted at Nginx Forum:

> **[Re: disable 301 redirect for directory / use relative redirect / change scheme](https://forum.nginx.org/read.php?2,261275,261283#msg-261283)**
>
> Hi, I have this setup browser -\> ssl proxy -\> nginx browser to ssl proxy is https only ssl proxy to nginx is http only now i browse to "https://exemple.com/aaa", where aaa is a directory, so nginx send back a 301 redirect with "L
