That's actually more complicated than it needs to be, see http://wiki.nginx.org/Pitfalls#Taxing_Rewrites
Bad:
rewrite ^/(.*)$ http://domain.com/$1 permanent;
Good: rewrite ^ http://domain.com$request_uri? permanent;
Better: return 301 http://domain.com$request_uri;