# External Redirect when expecting internal redirect

**URL:** <https://www.ruby-forum.com/t/external-redirect-when-expecting-internal-redirect/227291>\
**Category:** NGINX\
**Created:** [March 10, 2013, 1:31pm UTC](https://www.ruby-forum.com/t/external-redirect-when-expecting-internal-redirect/227291 "2013-03-10T13:31:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hildegunst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4bbf92/32.png) [@hildegunst](https://www.ruby-forum.com/u/hildegunst)\
**Post date:** [March 10, 2013, 1:31pm UTC](https://www.ruby-forum.com/t/external-redirect-when-expecting-internal-redirect/227291/1 "2013-03-10T13:31:01Z")

</div>

Hello,  
I’m running nginx 1.2.1-2.2 on Debian Wheezy (testing). I try to obtain  
the following:

Depending on the subnet accessing either rewrite internally to a cgi  
script or to a static Website. For the cgi script that works perfectly  
fine, for the static web site nginx always does a HTTP 301 instead of an  
internal rewrite. Here is a stripped down configuration demonstrating  
the  
issue:

(mini) [/etc/nginx] cat nginx.conf  
user www-data;  
pid /var/run/nginx.pid;

events {  
worker\_connections 1024;  
}

http {  
access\_log /var/log/nginx/access.log;  
error\_log /var/log/nginx/error.log;

```
    geo $site {
            127.0.0.0/8 eva;
            default blank;
    }

    server {
            listen 80;
            server_name localhost;

            root /var/www;

            location /eva {
                    internal;
                    gzip off;

                    fastcgi_param QUERY_STRING 

```

$query\_string;  
fastcgi\_param REQUEST\_METHOD  
$request\_method;  
fastcgi\_param CONTENT\_TYPE  
$content\_type;  
fastcgi\_param CONTENT\_LENGTH  
$content\_length;

```
                    fastcgi_param REQUEST_URI 

```

$request\_uri;  
fastcgi\_param DOCUMENT\_URI  
$document\_uri;  
fastcgi\_param DOCUMENT\_ROOT  
$document\_root;  
fastcgi\_param SERVER\_PROTOCOL  
$server\_protocol;

```
                    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
                    fastcgi_param SERVER_SOFTWARE 

```

nginx/$nginx\_version;

```
                    fastcgi_param REMOTE_ADDR 

```

$remote\_addr;  
fastcgi\_param REMOTE\_PORT  
$remote\_port;  
fastcgi\_param SERVER\_ADDR  
$server\_addr;  
fastcgi\_param SERVER\_PORT  
$server\_port;  
fastcgi\_param SERVER\_NAME  
$server\_name;

```
                    fastcgi_param HTTPS $https;

                    fastcgi_pass unix:/var/run/fcgiwrap.socket;
                    fastcgi_param SCRIPT_FILENAME 

```

/home/sithglan/work/scripts/web/eva.pl;  
}

```
            location /blank {
                    internal;
                    autoindex on;
                    autoindex_exact_size off;
            }

            location = / {
                    rewrite ^ /$site last;
            }
    }

```

}  
(mini) [/etc/nginx] /etc/init.d/nginx restart  
Restarting nginx: nginx.  
(mini) [/etc/nginx] curl -I [http://192.168.0.7/](http://192.168.0.7/)  
HTTP/1.1 301 Moved Permanently  
Server: nginx/1.2.1  
Date: Sun, 10 Mar 2013 12:28:44 GMT  
Content-Type: text/html  
Content-Length: 184  
Location: [http://192.168.0.7/blank/](http://192.168.0.7/blank/)  
Connection: keep-alive

(mini) [/etc/nginx] curl -I [http://192.168.0.7/](http://192.168.0.7/)  
HTTP/1.1 301 Moved Permanently  
Server: nginx/1.2.1  
Date: Sun, 10 Mar 2013 12:28:47 GMT  
Content-Type: text/html  
Content-Length: 184  
Location: [http://192.168.0.7/blank/](http://192.168.0.7/blank/)  
Connection: keep-alive

I would like to acomplish that when 127.0.0.0/8 access the webserver the  
client  
is internally redirected to eva.pl, which works, but if another ip  
address  
access a website from static files is displayed, which works was well if  
I drop  
the ‘internal’ keyword from location /blank but not using an internal  
redirect.

What do I have to change in order to make the internal rewrite for the  
files  
work?

Cheers,  
Thomas

---

<div class="post-metadata">

**Author:** ![hildegunst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4bbf92/32.png) [@hildegunst](https://www.ruby-forum.com/u/hildegunst)\
**Post date:** [March 10, 2013, 1:57pm UTC](https://www.ruby-forum.com/t/external-redirect-when-expecting-internal-redirect/227291/2 "2013-03-10T13:57:53Z")

</div>

On Sun, Mar 10, 2013 at 01:30:22PM +0100, Thomas G. wrote:

Hi there,

> Depending on the subnet accessing either rewrite internally to a cgi  
> script or to a static Website. For the cgi script that works perfectly  
> fine, for the static web site nginx always does a HTTP 301 instead of an  
> internal rewrite.

What do you expect the user to see with the static web site? As in,  
what content do you wish nginx to return?

> ```
> location /blank {
> internal;
> autoindex on;
> autoindex_exact_size off;
> }
> 
> location = / {
> rewrite ^ /$site last;
> }
> 
> ```

So, the request is for “/”, nginx does a rewrite (internal) to “/blank”,  
and that is a directory, so nginx does a redirect (external) to  
“/blank/”.

That’s pretty much what I expect to happen.

(Then the browser requests /blank/ and gets rejected because the  
location{} is marked “internal”.)

> I would like to acomplish that when 127.0.0.0/8 access the webserver the client  
> is internally redirected to eva.pl, which works, but if another ip address  
> access a website from static files is displayed, which works was well if I drop  
> the ‘internal’ keyword from location /blank but not using an internal redirect.
> 
> What do I have to change in order to make the internal rewrite for the files  
> work?

You can use an internal rewrite to a file, provided that you actually  
rewrite to a file. Here, you rewrite to a directory without including  
the trailing /.

I confess I’m not sure what it is that you want to do.

Possibly setting the default in the map to “blank/” will help?

But you will have to decide what the next step for the user is.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hildegunst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/4bbf92/32.png) [@hildegunst](https://www.ruby-forum.com/u/hildegunst)\
**Post date:** [March 10, 2013, 2:28pm UTC](https://www.ruby-forum.com/t/external-redirect-when-expecting-internal-redirect/227291/3 "2013-03-10T14:28:51Z")

</div>

Hello Francis,

- Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2013-03-10 13:57]:

> You can use an internal rewrite to a file, provided that you actually  
> rewrite to a file. Here, you rewrite to a directory without including  
> the trailing /.

I wanted to rewrite to a directory. I see my mistake now and it should  
have been obvious to me, but was not.

> Possibly setting the default in the map to “blank/” will help?

Yes, it resolved my issue. Now it works perfectly fine. Thank you for  
helping me out. I was searching for two hours now.

Cheers,  
Thomas
