W
W
wumvi2012-03-25 20:55:52
Nginx
wumvi, 2012-03-25 20:55:52

Rewrite as not rewrite?

Hello!
There was a problem with mod_rewrite in nginx.
I read forums, sysoev and Google, I can’t find the reason.
The essence of the problem:
There is a rewrite record
rewrite ^/blog/([^/]+)/?(.*)?$ /blog/category/$2?$1 break;
there are directories on the server
blog
category
name
according to my logic, requests like
/blog/test/ are sent to /blog/category
/blog/test/name/ are sent to /blog/category/name/
so in practice, except for the request
/blog/test /name
does not have a slash at the end, then in this case nginx for no reason does a redirect to
blog/category/name/?category=test
why is that?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
W
wumvi, 2012-03-26
@wumvi

I figured out the problem
. If you set rewrite_log on
, you can see what is the reason for
the looping and it can be seen from the logic of nginx-a that the
last cyclic block flies out as a redirect

W
wumvi, 2012-03-25
@wumvi

Config example

server {
	listen 8080;
    server_name site.lo www.site.lo;

    access_log W:/hosting/site.lo/nlogs/access.log main;
 
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/local/www/nginx-dist;
    }
		
	location ^~ /res/ {}
		
	location = /{}

    location ~ /\.ht {
        deny all;
    }
		
	rewrite ^/blog/([^/]+)/?(.*)?$ /blog/category/$2?$1 break;

	location / {}
		
	root W:/hosting/site.lo/www/;
	index index.php index.html index.htm;
		
	#For debug only
	autoindex on;
}

W
Wott, 2012-03-25
@Wott

^ / b l o g / ( [ ^ / ] + ) / ? ( . * ) ? $
Why is there a '?' before '$'?
/blog/category/$2?$1
what the hell is '?$1'?

there is no slash at the end, then in this case nginx for no reason does a redirect to
blog/category/name/?category=test

generally should do an internal rewrite to /blog/category/name?test, but apparently there is some script at this address that redirects thinking that test is a category
, the address with a slash at the end is conscientiously directed to /blog/category/?test and / blog/category/name/?test, where the given url rolls.

W
wumvi, 2012-03-25
@wumvi

Removed $ - the problem remained
^/blog/([^/]+)/?(.*)? /blog/category/$2?$1 break;
regexp logic is such that queries
/blog/test/
/blog/unix/
would end up in /blog/category/
/blog/test/ name - the name directory is in category
/blog/unix/name
would end up in /blog/category/name
(.*) - this is necessary so that everything else goes as parameters, for this ? $ 1

W
wumvi, 2012-03-25
@wumvi

Another interesting behavior mod_rewrite found
on a request like
site.lo/blog/category
rewrite ^/blog/([^/]+)/(.*)$ /blog/category/ break;
it gives the page (the one you need) and puts the redirect header on
site.lo/blog/category /

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question