-1

I'm trying to set up a "coming soon" temporary page on a website:

rewrite ^/(css|img|js)/ -                break;
rewrite ^/$             /comingsoon.html last;
rewrite ^               /?               redirect;

But I'm having trouble with the break flag: it's supposed to stop processing rewrite rules, but it doesn't seem to work.

My intention here is to:

  1. serve all css, img, and js files just as they are;
  2. serve the comingsoon.html page in place of the homepage;
  3. (temporary) redirect all other pages to the homepage.

The problem is that directive 1. is not working: its URLs fall through to the 3rd rule and are redirected to the homepage, as if the first rule did not have a break flag.

The regexp in the first directive does match, because if I replace break with redirect, I get a 302 to -. So this leaves the break flag as the culprit.

Am I using it incorrectly?


Edit: I solved it with a negative lookahead:

rewrite ^/$                 /comingsoon.html last;
rewrite ^/(?!css/|img/|js/) /?               redirect;

But I'm still interested in understanding why break does not seem to work.

5
  • 1
    OMG. Do NOT blindly copy rules from apache. There are much more simple and efficient ways to do it.
    – Alexey Ten
    Jun 1, 2016 at 14:50
  • @AlexeyTen Who said I copied rules from Apache? I haven't used Apache in years. This question is about Nginx's break flag and why it doesn't seem to work.
    – Tobia
    Jun 1, 2016 at 15:23
  • I see it by - in rewrite.
    – Alexey Ten
    Jun 1, 2016 at 16:35
  • @AlexeyTen That - means I don't care what it's rewritten to, I just want to stop processing the rules. Is that my mistake here? Maybe you could post an answer if you think you know how to get break to work.
    – Tobia
    Jun 1, 2016 at 16:49
  • That doesn't work in nginx. I've investigate the problem and writing the answer now
    – Alexey Ten
    Jun 1, 2016 at 16:50

1 Answer 1

3

Just turned on rewrite log and debug log and found out that your problem is in rewriting to -. Unlike Apache, nginx takes it literally and rewrite request URI to string -. Then it cannot find any location block that matches this URI and uses it's magic configuration "" that just happens to have all the server rewrite rules. So new URI goes through all the rewrite rules again.

Here is the log:

2016/06/01 19:52:41 [debug] 7288#7288: *24 http header: "Host: example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http header: "User-Agent: curl/7.47.0"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http header: "Accept: */*"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http header done
2016/06/01 19:52:41 [debug] 7288#7288: *24 event timer del: 4: 1464800021049
2016/06/01 19:52:41 [debug] 7288#7288: *24 generic phase: 0
2016/06/01 19:52:41 [debug] 7288#7288: *24 rewrite phase: 1
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex: "^/css/"
2016/06/01 19:52:41 [notice] 7288#7288: *24 "^/css/" matches "/css/asd", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script copy: "-"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex end
2016/06/01 19:52:41 [notice] 7288#7288: *24 rewritten data: "-", args: "", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 test location: "/"
2016/06/01 19:52:41 [debug] 7288#7288: *24 using configuration ""
2016/06/01 19:52:41 [debug] 7288#7288: *24 http cl:-1 max:1048576
2016/06/01 19:52:41 [debug] 7288#7288: *24 rewrite phase: 3
2016/06/01 19:52:41 [debug] 7288#7288: *24 rewrite phase: 4
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex: "^/css/"
2016/06/01 19:52:41 [notice] 7288#7288: *24 "^/css/" does not match "-", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex: "^/$"
2016/06/01 19:52:41 [notice] 7288#7288: *24 "^/$" does not match "-", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex: "^"
2016/06/01 19:52:41 [notice] 7288#7288: *24 "^" matches "-", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script copy: "/"
2016/06/01 19:52:41 [debug] 7288#7288: *24 http script regex end
2016/06/01 19:52:41 [notice] 7288#7288: *24 rewritten redirect: "/", client: 127.0.0.1, server: example.com, request: "GET /css/asd HTTP/1.1", host: "example.com"

Fix to initial problem could be as simple as:

rewrite ^(/(css|img|js)/.+) $1 break;

or

rewrite ^/(css|img|js)/ $uri break;

So, you should care to what you rewrite your URI.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.