Forum: NGINX Rewrite directory lookup to another directory

2974d09ac2541e892966b762aad84943?d=identicon&s=25 AjaySawant (Guest)
on 2014-08-24 07:06
(Received via mailing list)
Hello All,

I am trying to add a rewrite rule to my nginx configuration. But each
time
it is giving me 404/302 error. My scenarios is given below

1. If someone request URL http://192.168.44.163/testa or
http://192.168.44.163/testa/ then it should be redirected to
http://192.168.44.163/testa/web/index.php or
http://192.168.44.163/testa/web/index.html
2. If someone request any file inside testa directory it should be
redirected to testa/web directory and serve the file.

I tried with location /testa {} directive as well but in vain.

[root@symp html]# pwd
/usr/share/nginx/html
[root@symp html]# ls
404.html  50x.html  index.html  index.php  nginx-logo.png  poweredby.png
testa  testb  testc
[root@symp html]# tree
.
├── 404.html
├── 50x.html
├── index.html
├── index.php
├── nginx-logo.png
├── poweredby.png
├── testa
│   └── web
│       └── index.php
├── testb
│   └── web
│       └── index.php
└── testc
    └── index.php

5 directories, 9 files
[root@symp html]# cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;
pid        /run/nginx.pid;
events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local]
"$request"
'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
    index   index.php index.html index.htm;
    server {
        listen       80;
        server_name  192.168.44.163;
        root         /usr/share/nginx/html;
        access_log  /var/log/nginx/192.168.44.163.access.log  main;
        error_log /var/log/nginx/192.168.44.163.error.log;
        location / {
        }
        rewrite ^/testa/(.*)$   /testa/web/$1 permanent;
        rewrite ^/testb/(.*)$   /testb/web/$1 permanent;
        error_page  404              /404.html;
        location = /40x.html {
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }
        # pass the PHP scripts to FastCGI server listening on
127.0.0.1:9000
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME
$document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

[root@symp html]# curl http://192.168.44.163/testa/
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.6.1</center>
</body>
</html>

[root@symp html]# curl http://192.168.44.163/testa
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.6.1</center>
</body>
</html>

Regards,
Ajay

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,252780,252780#msg-252780
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.