# Nginx causes redirects when server root is changed

**URL:** <https://www.ruby-forum.com/t/nginx-causes-redirects-when-server-root-is-changed/221789>\
**Category:** NGINX\
**Created:** [July 31, 2012, 9:07am UTC](https://www.ruby-forum.com/t/nginx-causes-redirects-when-server-root-is-changed/221789 "2012-07-31T09:07:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 31, 2012, 9:07am UTC](https://www.ruby-forum.com/t/nginx-causes-redirects-when-server-root-is-changed/221789/1 "2012-07-31T09:07:47Z")

</div>

My nginx version is :  
root@v-enterprise15:/etc/nginx/conf.d# nginx -v  
nginx: nginx version: nginx/1.0.5

My application is installed at  
/usr/share/nginx/www/magento/current

When I access  
[http://myserver:81/magento/current/index.php](http://myserver:81/magento/current/index.php)  
it works fine. This is because the server root is set to  
/usr/share/nginx/www;

Now I want to acces the app using [http://myserver.co.in:81/index.php](http://myserver.co.in:81/index.php).  
When I change the server root to /usr/share/nginx/www/magento/current  
and type the above URL, it REDIRECTS with http 302 to  
[http://myserver:81/magento/current](http://myserver:81/magento/current).  
What might be the reason for this?

It then applies other rules from my .conf and gives a 404.

I see this in the access log:  
[31/Jul/2012:11:19:23 +0530] “GET /index.php HTTP/1.1” 302 5 “-”  
“Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.64”

My .conf file is:  
server {  
listen 81 default;  
## SSL directives might go here  
server\_name myserver;  
#root /usr/share/nginx/www;  
root /usr/share/nginx/www/magento/current;  
error\_log /var/log/nginx.error.log notice;  
access\_log /var/log/nginx.access.log ;

```
    rewrite_log on;

    location / {
        auth_basic "Restricted"; ## Message shown in login

```

window  
auth\_basic\_user\_file htpasswd; ## See /etc/nginx/htpassword  
index index.html index.php; ## Allow a static html file to  
be shown first  
try\_files $uri $uri/ @magehandler; ## If missing pass the  
URI to Magento’s front handler  
#try\_files $uri $uri/ ; ## If missing pass the URI to  
Magento’s front handler  
#expires 30d; ## Assume all files are cachable  
}

```
    location ^~ /. { ## Disable .htaccess and other hidden files
        return 404;
    }

    location @magehandler { ## Magento uses a common front handler
        #rewrite / /index.php;
        rewrite ^(.*) index.php$1 last;
    }

    #The more-specific regex are at the top.
    #The regex's are evaluated in the order in which they appear.
    location ~ .php$ { ## Execute PHP scripts
        #if (!-e $request_filename) { rewrite / /index.php last; }

```

## Catch 404s that try\_files miss

```
            proxy_read_timeout 120;
            proxy_connect_timeout 120;
        expires off; ## Do not cache dynamic content
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_param HTTPS $fastcgi_https;
        fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
fastcgi\_param MAGE\_RUN\_CODE default; ## Store code is  
defined in administration \> Configuration \> Manage Stores  
fastcgi\_param MAGE\_RUN\_TYPE store;  
include fastcgi\_params; ## See  
/etc/nginx/fastcgi\_params  
}  
location ~ /(media|skin|js)/ { }  
location ~  
/(tag|admin|customer|wishlist|checkout|catalog|app)._$ { #store URL  
rewrite /(._)$ /index.php/$1 last;  
}

```
    location ~ /[a-zA-Z]+$ { #store URL
            rewrite ^/([a-zA-Z]+) ?store=$1 last;
    }
    location ~ /[a-zA-Z]+/ { #store URL
            rewrite ^/([a-zA-Z]+)(.*) /$2?store=$1 last;
    }

```

Posted at Nginx Forum:

> **[Nginx causes redirects when server root is changed](https://forum.nginx.org/read.php?2,229127,229127#msg-229127)**
>
> My nginx version is : root@v-enterprise15:/etc/nginx/conf.d# nginx -v nginx: nginx version: nginx/1.0.5 My application is installed at /usr/share/nginx/www/magento/current When I access http://myserver:81/magento/current/index.php it

---

<div class="post-metadata">

**Author:** ![yashgt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bcef8e/32.png) [@yashgt](https://www.ruby-forum.com/u/yashgt)\
**Post date:** [July 31, 2012, 7:33pm UTC](https://www.ruby-forum.com/t/nginx-causes-redirects-when-server-root-is-changed/221789/2 "2012-07-31T19:33:32Z")

</div>

On Tue, Jul 31, 2012 at 03:06:58AM -0400, yashgt wrote:

Hi there,

> and type the above URL, it REDIRECTS with http 302 to  
> [http://myserver:81/magento/current](http://myserver:81/magento/current).  
> What might be the reason for this?

Might it be the application doing the redirect, and not nginx?

What is the output from running the command

curl -i [http://myserver.co.in:81/index.php](http://myserver.co.in:81/index.php)

? Does it include anything like “X-Powered-By: PHP”?

From your configuration, I see no obvious reason why nginx should be  
issuing the redirect.

If it _is_ the application, then you’ll probably want to configure it  
to do what you want it to do.

Good luck with it,

## f

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

---

<div class="post-metadata">

**Author:** ![yashgt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bcef8e/32.png) [@yashgt](https://www.ruby-forum.com/u/yashgt)\
**Post date:** [August 1, 2012, 9:03am UTC](https://www.ruby-forum.com/t/nginx-causes-redirects-when-server-root-is-changed/221789/3 "2012-08-01T09:03:01Z")

</div>

That was exactly the problem. The app was causing the redirect. Thanks a  
ton.

Posted at Nginx Forum:

> **[Re: Nginx causes redirects when server root is changed](https://forum.nginx.org/read.php?2,229127,229168#msg-229168)**
>
> My nginx version is : root@v-enterprise15:/etc/nginx/conf.d# nginx -v nginx: nginx version: nginx/1.0.5 My application is installed at /usr/share/nginx/www/magento/current When I access http://myserver:81/magento/current/index.php it
