server level url rewrite & config rewrite or internal redirection cycle

Igor Sysoev igor at sysoev.ru
Sun Dec 6 22:43:42 MSK 2009


On Sun, Dec 06, 2009 at 10:58:38AM -0500, et wrote:

> Hi Igor,
> 
> Thanks a lot for the reply.
> 
> Below are what I want:
> 1. some static files can be directly accessed. but also, access static files with a fake query string is also allowed since this is useful to walk around browser cache for debug purpose
>     e.g. /favor.ico or /favor.ico?xxx=yyy => /new_path/favor.ico

nginx does not a query string in location, since anyone may request
anything in the query string, so

  location = /favor.ico {
      root  /new_path;
  }

will work for any query string.

> 2. all other request will be passed to a single PHP file 'common.php', but search engine friendly url needs to be parsed into files + query strings.
>     e.g. /module/func/param1-value1-param2-value2... => /common.php?__mod__=module&__func__=func&param1=value1&param2=value2...
> 
> 3. common.php will be handled by upstream PHP fastcgi env.
> 
> 4. prevent any direct access to /common ....
> 
> Usually, 2 is done by many web frameworks on the application level, but I want to make it into the web server to see if there's a tiny performance gain. I've already done above for Apache and Lighttpd. Both work well.

The configuration below looks to me as overengineered thing.
I never understand why PHP developers can not parse an original request
to get arguments. I believe, you will no see any performance gain,
at least in nginx and Apache case.

> Since Nginx ignores rewrite flag 'break' or 'last' on the server level and no 'N|Next' flag like Apache, rewrite rules to achieve above took me some time. My solution is as below. Any suggestions to make it better?

On the application level.

> As I've asked, is there a hidden config directive to control the number of internal rewrite cycles? The current value 10 restricts the number of params to only 7, namely /module/func/param1-value1-param2-value2-p3-v3-p4-v4-p5-v5-p6-v6-p7-v7 (no more p-v pairs on the tail).

10 is hardcodeded limit.

> server {
> 	...
> 
> 	root /var/html/webroot;
> 
> 	...
> 
>     location / {
>         rewrite ^/favicon.ico$ /frontend/template/default/img/favicon.ico break;
>         rewrite ^/img/(.*\.(jpg|png|gif))$ /frontend/template/default/img/$1 break;
>         rewrite ^/js/(.*\.js)$ /frontend/template/default/js/$1 break;
>         rewrite ^/b_js/(.*\.js)$ /bframe/frontend/cache/js_cache/$1 break;
>         rewrite ^/css/(.*\.css)$ /frontend/template/default/css/$1 break;
>         rewrite ^/html/(.*)$ /frontend/html/$1 break;
>         rewrite ^(.*)$ :///:$1;
>     }
>     location :///: {
>         rewrite ^:///:/common(|[.\-/]+.*)$ :///////:/?__mod__=404 last;
>         rewrite ^:///:/([^.\-/=&]+)[\-/]*(.*) :///:$2?__mod__=$1;
>         rewrite ^:///:([^.\-/=&]+)[\-/]*(.*) :///:$2?__func__=$1;
>         rewrite ^:///:(.*)$ ://///:$1;
>     }
> 
>     location ://///: {
>         rewrite ^://///:([^.\-/=&]+)[\-/]([^.\-/=?&]+)[\-/]*(.*) ://///:$3?$1=$2 last;
>         rewrite ^://///:(.*)$ :///////:$1;
>     }
> 
>     location :///////: {
>         rewrite .* /path/to/common.php break;
>         include /home/y/conf/nginx/fastcgi_params;
>         fastcgi_pass  127.0.0.1:9000;
>     }
> }


-- 
Igor Sysoev
http://sysoev.ru/en/



More information about the nginx mailing list