add a URL suffix path (for mobile AMP) without causing a redirect loop in nginx

Solution 1:

It sounds like you have a redirect loop, because you keep on adding /amp/ at the end of your request URL.

Perhaps you should have a conditional rewrite directive (or two!) instead of a wildcard.

-rewrite ^ http://example.com$request_uri/amp/ break;
+rewrite ^(.*(?<!/amp))/$ http://example.com$1/amp/ break;
+rewrite ^.*(?<!/amp/)$ http://example.com$uri/amp/ break;

The above ensures that the rewrite will only happen if your URL doesn't already end in /amp/ (using the lookbehind assertions of PCRE, which is the library that NGINX is using for support of the regular expressions); additionally, it should also take care of the double // that were appearing in your scheme originally.