LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-14-2011, 12:54 AM   #1
LMW
Member
 
Registered: Oct 2010
Location: Russia / USA
Distribution: Arch Linux
Posts: 36

Rep: Reputation: 2
nginx rewrite / redirect


Hi everybody!

I would like to make redirect from root dir to location. For example, I have a web-site spinning in "root /var/www/website" (it's a global directive). What I want to do is that all who request this website at "website.com" (root dir) will be redirected to "location /data" ("website.com/data"). Is that possible?
 
Old 05-14-2011, 01:34 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
Code:
location  = / {root /var/www/website/data}
either
Code:
rewrite  ^/$  /data/  break;
either real redirect
Code:
location  = / {error_page 403 = http://website.com/data;}
 
1 members found this post helpful.
Old 05-14-2011, 02:17 AM   #3
LMW
Member
 
Registered: Oct 2010
Location: Russia / USA
Distribution: Arch Linux
Posts: 36

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by troop View Post
Code:
location  = / {root /var/www/website/data}
either
Code:
rewrite  ^/$  /data/  break;
either real redirect
Code:
location  = / {error_page 403 = http://website.com/data;}
Thank you for reply!
However, that doesn't seem to work. Well, maybe I wasn't clear enough.

Here's my nginx.conf example

Code:
server {
        listen x.x.x.x:80;
        server_name example.com;
        access_log  /var/log/nginx/nagios_access.log;
        root /usr/html;
        location /data/{
                index index.php;
                rewrite ^/data/(.*)$ /$1 break;
                }
}
As you can see, accessing example.com from browser will result in moving to "/usr/html/" folder's content. But I need to move to "/usr/html/data" instead (move from global directive to local).
 
Old 05-14-2011, 06:00 AM   #4
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
you have a recursion.
look
Code:
        root /usr/html;
        rewrite  ^/$  /data/  break;
        location /data/{
           index index.php;
           rewrite ^/data/(.*)$ /$1 break;
        }
so accessing example.com will result in moving to "/usr/html/data". But second rewrite (^/data/(.*)$ /$1) result in moving back to /usr/html...
 
1 members found this post helpful.
Old 05-14-2011, 12:37 PM   #5
LMW
Member
 
Registered: Oct 2010
Location: Russia / USA
Distribution: Arch Linux
Posts: 36

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by troop View Post
you have a recursion.
look
Code:
        root /usr/html;
        rewrite  ^/$  /data/  break;
        location /data/{
           index index.php;
           rewrite ^/data/(.*)$ /$1 break;
        }
so accessing example.com will result in moving to "/usr/html/data". But second rewrite (^/data/(.*)$ /$1) result in moving back to /usr/html...
Now, when I try to access website at root dir I've got that error. The same goes for "website.com/data"

"/usr/share/data/data/index.php" is not found (2: No such file or directory), client: x.x.x.x, server: x.x.x.x, request: "GET / HTTP/1.1", host: "x.x.x.x"
"/usr/share/data/data/index.php" is not found (2: No such file or directory), client: x.x.x.x, server: x.x.x.x, request: "GET /data/ HTTP/1.1", host: "x.x.x.x"

I've added alias to required dir inside "location" directive and it's now working. Thanks a lot for pointing!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nginx Virtual Hosts & Wordpress Rewrite Rules Coburn64 Linux - Server 1 09-15-2010 02:58 PM
mod rewrite redirect nuttynibbles Linux - General 4 06-19-2010 05:57 PM
Problem with dynamic url rewrite and redirect in nginx??? gavin2u Linux - Server 3 05-27-2010 11:25 PM
nginx 0.7.65 rewrite url with querystring problem? gavin2u Linux - Server 0 04-07-2010 08:46 PM
nginx redirect error page cbtshare Linux - Server 0 10-10-2009 02:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration