1

I tried this: How do I enable clean URLs with Nginx?

And I tried this: https://www.digitalocean.com/community/questions/how-do-i-enable-clean-urls-with-nginx-for-drupal

So, I tried these:

 location / {
    index index.php;
    # This is cool because no php is touched for static content
    try_files $uri $uri/ @rewrite;
    expires max;
  }

  location @rewrite {
    # 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;
  }

  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/var/run/php-fpm.sock; # fastcgi_pass unix:/tmp/php5-fpm.sock;
 }

And I tried these:

server {
     listen       80;
     server_name  example.org;

     location / {
         root   /path/to/drupal;
         index  index.php;
         error_page 404 = @drupal;
     }

     location @drupal {
         rewrite ^(.*)$ /index.php?q=$1 last;
     }
 }

And clean URL's don't work. The site runs at ip address in a subdir 127.0.0.1/dev

It has its own config at /etc/nginx/sites-available/dev and symlinked to /etc/nginx/sites-enabled/dev

This is my current sites-available/default config:

 root /var/www;

 # Add index.php to the list if you are using PHP
 index index.html index.htm index.php;

 server_name _;

 location / {
 # First attempt to serve request as file, then
 # as directory, then fall back to displaying a 404.
 # try_files $uri $uri/ =404;
 try_files $uri $uri/ @rewrite;
 expires max;
 }

 location @rewrite {
   # 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;
 }

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 location ~ \.php$ {
 include snippets/fastcgi-php.conf;

 # With php7.0-cgi alone:
 # fastcgi_pass 127.0.0.1:9000;
 # With php7.0-fpm:

 fastcgi_pass unix:/run/php/php7.0-fpm.sock;

 }

 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 location ~ /\.ht {
  deny all;
 }
}

This is my current sites-available/dev config:

server {

        server_name dev;
        root /var/www/dev; ## <-- Your only path reference.

        # Enable compression, this will help if you have for instance advagg module
        # by serving Gzip versions of the files.
        gzip_static on;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        # This matters if you use drush prior to 5.x
        # After 5.x backups are stored outside the Drupal install.
        #location = /backup {
        #        deny all;
        #}

        # Very rarely should these ever be accessed outside of your lan
        location ~* \.(txt|log)$ {
                allow 192.168.0.0/16;
                deny all;
        }

        location ~ \..*/.*\.php$ {
                return 403;
        }

        # No no for private
        location ~ ^/sites/.*/private/ {
                return 403;
        }

        # Block access to "hidden" files and directories whose names begin with a
        # period. This includes directories used by version control systems such
        # as Subversion or Git to store control files.
        location ~ (^|/)\. {
                return 403;
        }

  location / {
    index index.php;
    # This is cool because no php is touched for static content
    try_files $uri $uri/ @rewrite;
    expires max;
  }

  location @rewrite {
    # Some modules enforce no slash (/) at the end of the URL
    # Else this rewrite block wouldn&#39;t be needed (GlobalRedirect)
    rewrite ^/(.*)$ /index.php?q=$1;
  }

  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
 }

        # Fighting with Styles? This little gem is amazing.
        # This is for D6
        #location ~ ^/sites/.*/files/imagecache/ {
        # This is for D7 and D8
        location ~ ^/sites/.*/files/styles/ {
                try_files $uri @rewrite;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }

}

I restart nginx server by running sudo service nginx restart

2
  • what message do you get in your clean URLs config page? admin/config/search/clean-urls Commented Feb 9, 2017 at 5:06
  • that test failed, default message Commented Feb 9, 2017 at 7:57

1 Answer 1

1

Your question is about Drupal 7 right ? Try replacing

# For Drupal <= 6
location / {
    index index.php;
    try_files $uri $uri/ @rewrite;
    expires max;
  }

by

# For Drupal >= 7
location / {
    try_files $uri /index.php?$query_string; 
}

Then reload nginx.

This is a full example working vhost fir Drupal 7 :

server {

    listen 1.2.3.4:8000;
    server_name example.com www.example.com
    root /var/www/example.com /web;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    #Drupal private folder (see https://github.com/perusio/drupal-with-nginx#private-file-handling)
    location ^~ /sites/default/protected {
      internal;
    }

    # Very rarely should these ever be accessed outside of your lan
    location ~* \.(txt|log)$ {
        allow 192.168.0.0/16;
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    # Allow "Well-Known URIs" as per RFC 5785
    location ~* ^/.well-known/ {
        allow all;
    }

    # Block access to "hidden" files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/)\. {
        return 403;
    }

    location / {
        try_files $uri /index.php?$query_string; 
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }

    # Don't allow direct access to PHP files in the vendor directory.
    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }

    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        include fastcgi_params;
        # Block httpoxy attacks. See https://httpoxy.org/.
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_read_timeout 180;
        # PHP 7 socket location (change it to fit your config)
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    }

    # Fighting with Styles? This little gem is amazing.
    location ~ ^/sites/.*/files/styles/ { 
        try_files $uri @rewrite;
    }

    # Handle private files through Drupal. Private file's path can come
    # with a language prefix.
    location ~ ^(/[a-z\-]+)?/system/files/ { 
        try_files $uri /index.php?$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}
5
  • Why is 'listen' set to '1.2.3.4:8000' what's that? If I enter my server IP nginx won't restart. also what's '/web' part? Anyway I tried your config, but it breaks site now. CSS files won't load for example. Commented Feb 10, 2017 at 13:56
  • You have to change both IP and port accordingly to your config. By default port 80. I'm behind Varnish, this is why I wrote port 8000 in my example, sorry if it confused you.
    – Kojo
    Commented Feb 10, 2017 at 14:00
  • I don't access the website via domain. I enter it via IP address e.g. 127.0.0.1/dev and if I do that css files won't load and site loads all text in white. so I have to comment out the listen part to fix it. E.g. "listen 127.0.0.1:80;" (IP edited of course) and don't you need semi-colon at the end? It's not in your code. And clean urls still dont pass the test. Commented Feb 10, 2017 at 14:01
  • This is a bit out of the scope of your question :) I can't fully setup your remote install, try to put these parameters in the server part at the beginning : listen YOUR_SERVER_IP:80;, then below listen 80;, then below if it was working server_name dev;, and below root /var/www/dev; , then the rest on sample nginx vhost.
    – Kojo
    Commented Feb 10, 2017 at 14:13
  • awesome, works for drupal 7. Finally removed that annoying ?q! Thanks for sharing Commented Oct 21, 2018 at 21:16

Your Answer

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

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