Nginx: Fix for redirect loop on D8 sites

By John Locke on April 17, 2017

On one of our D8 sites, after upgrading to 8.3.0, we had a "too many redirects" loop going on on the dev server. For quite some time, we've had the site path repeated with "?q=".

Tracked this down to a trick with the rewrite blocks we had in our Nginx virtual host config. Our config was built to support both Drupal 6 and Drupal 7 with different rewrite blocks. The essence:

        location / {
                # This is cool because no php is touched for static content
                try_files $uri @rewrite7 @rewrite6;
        }
 
        location @rewrite7 {
                # You have 2 options here
                # For D7 and above:
                # Clean URLs are handled in drupal_environment_initialize().
                rewrite ^ /index.php;
        }
        location @rewrite6 {
                # For Drupal 6 and bwlow:
                # Some modules enforce no slash (/) at the end of the URL
                # Else this rewrite block wouldn't be needed (GlobalRedirect)
                rewrite ^/(.*)$ /index.php?q=$1;

... The problem was, the "try_files" section was skipping over the @rewrite7 block and going straight to @rewrite6. And in this case it was leading to a redirect loop that appended another q= parameter until the server killed the request.

It turns out that try_files only checks for an existence of a file pattern, and redirects to the last item in its list only if a file does not exist in the earlier patterns. So providing two named locations does not work -- it will follow the second one because the first is not evaluated, it is simply checked literally.

Simple fix -- remove the @rewrite6 section. Drupal 6 doesn't work on PHP7 anyway.

Topic

Comments

This seems to be some sort of Kalabox issue I'm running into that's causing the ?q= to be output on every page, it just wasn't causing an infinite redirect loop until the new version of redirect was installed today. I suppose I'll roll back redirect until the issue in Kalabox is figured out. Thanks for your assistance.

Add new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <blockquote cite> <cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h1> <h2 id> <h3 id> <h4 id> <h5 id> <p> <br> <img src alt height width>
  • Lines and paragraphs break automatically.

Drupal Canvas — Block HTML (locked)

  • Allowed HTML tags: <strong> <em> <u> <a href> <p> <br> <ul> <ol> <li>

Drupal Canvas — Inline HTML (locked)

  • Allowed HTML tags: <strong> <em> <u> <a href>

More Like This

Laptop with coffee and pills
🕑Apr 07, 2020 🖋John Locke 💬2

Aggregate fields in Drupal 8 views

Views module has long been the killer feature of Drupal, making it easy for a site builder or skilled administrator to essentially create complex SQL queries through a web interface, without knowing SQL.

Rock climb
🕑Aug 05, 2017 🖋John Locke 💬2

Getting the group into the URL with Purl

The corners of Drupal 8 that aren't there are quickly dwindling, but there are still some that need to get worked out. While upgrading our internal issue tracker, we hit a new one -- getting a group context set via a URL alias, and generally keeping posts within a group.