server level url rewrite & config rewrite or internal redirection cycle

et nginx-forum at nginx.us
Sun Dec 6 18:58:38 MSK 2009


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

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.

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?

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).


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;
    }
}


Posted at Nginx Forum: http://forum.nginx.org/read.php?2,28189,28380#msg-28380




More information about the nginx mailing list