Score:1

Why doesn't the "break" directive in my nginx configuration stop rewrite processing?

fr flag

I have the following nginx configuration:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.php;

    server_name _;

    if ($uri = /foobar) {
        break;
    }

    if ($remote_addr != 1.2.3.4) {
        rewrite ^ https://google.com redirect;
    }

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
}

Whether I got to http://mydomain.tld/ or http://mydomain.tdl/foobar, I get redirected to Google. I thought break was supposed to stop the processing of ngx_http_rewrite_module directives?

Ivan Shatsky avatar
gr flag
It is. But an `$uri` variable get changed by the `try_files` directive. Then a new loop started where `$uri` equals `/index.php`. Since I didn't see your configuration answering your question, I couldn't foresee this, sorry. Try `if ($request_uri = /foobar) { break; }` instead. Anyway, I expect additional problems with the assets.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.