Answer the question
In order to leave comments, you need to log in
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
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
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; }
^ / 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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question