[HOW-TO] Config Nginx to work with Drupal 6

By BXTra |
Yesterday, I've tried to setup Nginx as a Reverse Proxy for Apache - Worker MPM base webserver. I found that it doesn't help my server performance as I expected. So, I decided to try to set up Nginx to be a Webserver to see if Nginx can give me a better performance or not. I found a sample set up for Drupal website on Nginx web and it looks like it's pretty easy to set it up. So, I tried it. Just copy the code below into your configuration. ( This is per vhost and it's related to my [HOW-TO] Use Nginx as a reverse proxy for DirectAdmin ) Let's say the domain name is domainadmin.com and IP address is Start by create a configuration file :
nano -w /usr/local/nginx/vhosts/domainadmin.com.conf
Copy the code below into configuration :
server {
        server_name domainadmin.com alias www.domainadmin.com;
        root /home/admin/public_html; ## <-- Your only path reference.
        access_log  /var/log/nginx_domainadmin.com_access.log  main;
        error_log  /var/log/nginx_domainadmin.com_error.log debug;

        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
        location = /backup {
                deny all;

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

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

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

        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$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
                fastcgi_pass unix:/fcgi/admin/public_html/admin.sock;

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

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

Then, create a file fastcgi_params as below :

nano -w /usr/local/nginx/vhosts/fastcgi_params

Copy and paste code below :

### fastcgi parameters.
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

Save it, restart Nginx and it should work properly now. Now, to talk about performance, I just ran it for almost a day but I saw something different. Look like the server load is lower than when I used Apache Worker MPM. It seems like Nginx works well together with PHP-FPM. I will keep monitor it and do some tuning and check it again.

Tested with below software :
1. CentOS 5.4 / 5.5 - 64 bits
2. DirectAdmin 1.37 - With Custombuild 1.2
3. Nginx 0.8.5
4. PHP 5.2.17 / 5.3.5
5. PHP-FPM in PHP 5.3.5
6. APC 3.1.7

source :
- http://wiki.nginx.org/Drupal
- https://github.com/perusio/drupal-with-nginx

Add new comment

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.