Nginx + VirtualHost + Drupal 7.14 + Clean URLS

mogop's picture

I can't manage Clean Urls to work on Nginx. I try diffrent tutorials over the net but still no result.
/etc/nginx/sites-enabled/sweetdreams

# You may add here your
# server {
#  ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
#listen   80; ## listen for ipv4; this line is default and implied
#listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

  root /var/www/sweetdreams.com;
index index.php;

  # Make site accessible from http://localhost/
  server_name sweetdreams.com www.sweetdreams.com;
   access_log /var/log/nginx/qa.access.log;
       error_log  /var/log/nginx/qa.error.log info;
  
   location / {
       # First attempt to serve request as file, then
     # as directory, then fall back to index.html
       try_files $uri @rewrite;
       # Uncomment to enable naxsi on this location
       # include /etc/nginx/naxsi.rules
   }

# This matters if you use drush
    location = /backup {
                deny all;
    }

  location @rewrite {
    # Some modules enforce no slash (/) at the end of the URL
  # Else this rewrite block wouldn't be needed (GlobalRedirect)
rewrite ^/([^/])/(.)(/?)$ /$1/index.php?q=$2&$args;
  }
 
   location ~* files/styles {
     access_log off;
        expires 30d;
       try_files $uri @rewrite;
   }

location /doc/ {
       alias /usr/share/doc/;
     autoindex on;
      allow 127.0.0.1;
       allow ::1;
     deny all;
  }

# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
  #location /RequestDenied {
#   proxy_pass http://127.0.0.1:8080;   
  #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
#
  #error_page 500 502 503 504 /50x.html;
#location = /50x.html {
    #   root /usr/share/nginx/www;
#}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
  location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
      # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
  
   #   # With php5-cgi alone:
     fastcgi_pass 127.0.0.1:9000;
       # With php5-fpm:
   #   fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
       include fastcgi_params;
    }

location /phpmyadmin {
               root /usr/share/;
               index index.php index.html index.htm;
               location ~ ^/phpmyadmin/(.+.php)$ {
                       try_files $uri =404;
                       root /usr/share/;
                       fastcgi_pass 127.0.0.1:9000;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       include /etc/nginx/fastcgi_params;
               }
               location ~* ^/phpmyadmin/(.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/;
               }
        }
        location /phpMyAdmin {
               rewrite ^/* /phpmyadmin last;
        }

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

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen 8000;
#  listen somename:8080;
# server_name somename alias another.alias;
# root html;
#    index index.html index.htm;
#
# location / {
#      try_files $uri $uri/ /index.html;
# }
#}


# HTTPS server
#
#server {
# listen 443;
#   server_name localhost;
#
#  root html;
#    index index.html index.htm;
#
# ssl on;
#   ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
#   ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1;
#    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
#    ssl_prefer_server_ciphers on;
#
#   location / {
#      try_files $uri $uri/ /index.html;
# }
#}

/etc/hosts
127.0.0.1 localhost
127.0.1.1 Zion
127.0.0.1  sweetdreams.com
127.0.0.1   www.sweetdreams.com


# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

and start/restart nginx gives me error:
Restarting nginx: nginx: [warn] conflicting server name "sweetdreams.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "www.sweetdreams.com" on 0.0.0.0:80, ignored
nginx.

What do I miss? Any help, please.

Comments

had the same problem

elfanjo's picture

try that

location ~* files/generate {
expires max;
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 last;
}

Then if you still can't pass the test force clean urls in settings.php and see what happens

also

elfanjo's picture

also you might be interested in this:
http://groups.drupal.org/node/237483

Thank You! So far by now it

mogop's picture

Thank You!
So far by now it works perfectly!

out of curiosity

elfanjo's picture

just wondering, with your setup + the rewrites do you happen to have image styles working?

yes, they work just fine

mogop's picture

yes, they work just fine

nginx clean urls and image styles problem

geraldvillorente's picture

Hi Guys,

I have the same problem right now. I migrated from Drupal 6 to Drupal 7. In Drupal 6 I am using imagecache which is now a part of Drupal core. I migrated all my presets and I'm having trouble to make it working.

Problems right now:
1. admin/config/search/clean-urls - returns "The clean URL test failed. Clean URLs cannot be enabled."
2. Images are not showing

Environment:
1. NginX
2. Drupal 7

Setup:
We have multiple branches in our dev server and we are using centralized NginX configuration. These branches are composed of Drupal 6 and Drupal 7. Our filesystem is also shared.

did you try

mogop's picture

did you try http://groups.drupal.org/node/237483
I'm too lame with server configuration so I look on the web for help all the time

yeah

elfanjo's picture

I was about to post the same ;)

Our NginX setup

geraldvillorente's picture
#######################################################
###  NginX  BEGIN                                  ####     
#######################################################
pid                   /var/run/nginx.pid;
user                  nginx;
worker_processes      4;
worker_rlimit_nofile  65536;

events {
    worker_connections  4096;
    #use epoll;
}

http {
  geoip_country      /usr/local/share/GeoIP/GeoIP.dat;
  ## MIME types
  include            /etc/nginx/fastcgi.conf;
  include            /etc/nginx/mime.types;
  default_type       application/octet-stream;

  ## Size Limits
  client_body_buffer_size         1k;
  client_header_buffer_size       1k;
  client_max_body_size           50m;
  large_client_header_buffers   3 3k;
  connection_pool_size           256;
  request_pool_size               4k;
  server_names_hash_bucket_size  128;

  ## Timeouts
  client_body_timeout             60;
  client_header_timeout           60;
  keepalive_timeout            75 20;
  send_timeout                    60;

  ## General Options
  ignore_invalid_headers          on;
  #  limit_zone gulag $binary_remote_addr 1m;
  recursive_error_pages           on;
  sendfile                        on;

  ## TCP options 
  tcp_nodelay on;
  tcp_nopush  on;

  ## Compression
  gzip              on;
  gzip_buffers      16 8k;
  gzip_comp_level   6;
  gzip_http_version 1.1;
  gzip_min_length   10;
  gzip_types        text/plain text/css image/png application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon;
  gzip_vary         on;
  gzip_proxied      any;
  gzip_disable      "MSIE [1-6]\.";

  log_format        main '$remote_addr - $remote_user [$time_local] '
                         '"$request" $status $bytes_sent ';

  #client_body_temp_path /var/sites/default/files/cache/nginx/client_body_temp 1 2;
  access_log            /var/log/nginx/domain.v02-access_log main;
  error_log             /var/log/nginx/domain.v02-error_log error;

  server {  
   #   limit_conn   gulag 10;
    listen       80;
    server_name  *.dev.domain.com;
    set $domain $host;
    if ($domain ~ "^(w{3}\.)?(.*?)\.?dev.domain.com") {
      set $domain $2;
    }

    root         /home/httpd/html/$domain.dev.domain.com/;
    index       index.php index.html;

    ## Deny some crawlers
    if ($http_user_agent ~* (HTTrack|HTMLParser|libwww) ) {
         return 444;
    }
    
    ## Required only when using purl, spaces & og for modules: ajax_comments, watcher and fasttoggle
    ## the /og path should be modified to match your default for og/purl URL for organic groups
    location ~* ^/og {
        rewrite ^/og\-(.*)/ajax_comments/(.*)$                  /index.php?q=ajax_comments/$2 last;
        rewrite ^/og\-(.*)/context/ajax-block-view$             /index.php?q=context/ajax-block-view last;
        rewrite ^/og\-(.*)/comment/reply/(.*)\?reload=1$        /index.php?q=comment/reply/$2&reload=1 last;
        rewrite ^/og\-(.*)/node/([0-9]+)/toggle/(.*)$           /index.php?q=node/$2/toggle/$3 last;
        rewrite ^/og\-(.*)/node/([0-9]+)/edit\?(.*)$            /index.php?q=node/$2/edit?$3 last;
        rewrite ^/og\-(.*)/user/([0-9]+)/watcher/toggle/(.*)$   /index.php?q=user/$2/watcher/toggle/$3 last;
        rewrite ^/(.*)$                                         /index.php?q=$1 last;
    }

    location / {
       ## Redirect some language paths
       rewrite ^/zh-hans$ /zh-hans/shanghai;
       rewrite ^/shanghai/chinese$ /zh-hans/shanghai;
       rewrite ^/zh-hant$ /zh-hant/hong-kong;
       rewrite ^/hongkong/chinese$ /zh-hant/hong-kong;
       rewrite ^/ja$ /ja/tokyo;
       rewrite ^/tokyo/jp$ /ja/tokyo;
       rewrite ^/tokyo/japanese$ /ja/tokyo;
       
       try_files $uri @cache;
    }

    location @cache {
       # return 405;
        if ( $request_method !~ ^(GET|HEAD)$ ) {
            return 405;
        }
        if ($http_cookie ~ "(DRUPAL_UID|DOMAINid)") {
            return 405;
        }
        error_page 405 = @drupal;
        add_header Expires "Tue, 24 Jan 1984 08:00:00 GMT";
        add_header Cache-Control "must-revalidate, no-cache, post-check=0, pre-check=0";
        add_header X-Header "Boost APP1.0";
        charset utf-8;
        try_files /sites/default/files/cache/normal/$host${uri}_$args.html /sites/default/files/cache/$host${uri}_$args.html @drupal;
     }

    location @drupal {
        ## Now simplified to reduce rewrites
        rewrite ^/(.*)$  /index.php?q=$1 last;
    }

    location ~* (/\..*|settings\.php$|\.(htaccess|engine|inc|info|install|module|profile|pl|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(Entries.*|Repository|Root|Tag|Template))$ {
        deny all;
    }

    location ~* /sites/default/files/.*\.php$ {
        return 444;
    }

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

    location ~* /themes/.*\.php$ {
        return 444;
    }
      
    location ~ \.php$ {
          add_header X-Header "PHP APP1";
          try_files $uri @drupal;       #check for existence of php file
          fastcgi_pass 127.0.0.1:9000;  #php-fpm listening on port 9000
          fastcgi_index index.php;
    }

    location @uncached {
        access_log  off;
        expires  max; # max if using aggregator, otherwise sane expire time
    }

    location ~* files/imagecache/ {
        access_log         off;
        expires max;
        try_files $uri @drupal;  #imagecache support - now it works
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
        access_log      off;
        expires         30d;
        try_files $uri =404;
    }

    location ~* /crossdomain\.xml$ {
        access_log      off;
        expires         30d;
        try_files $uri =404;
    }

  } # end of server

  # Redirect all to live site
  server {
   #   limit_conn   gulag 10;
    listen       80;
    server_name  *.domain.com;

  }
  
# Include some other config file fomr the conf.d folder
include /etc/nginx/conf.d/git_deploy.conf;
include /etc/nginx/conf.d/git_deploy_mobile.conf;
#include /etc/nginx/conf.d/git_deploy_test.conf;

}

Our NginX setup

geraldvillorente's picture

The above is our nginx setup but this was made for our Drupal 6. Would that be a cause why we cant enable the 'Clean URL" and our problem about showing the images?

Thanks

Clean URL fixed

geraldvillorente's picture

The remaining issue right now is the "Inline Image" not being rendered. For example in Drupla 6 if you try to change the preset in the URL like this from

http://domain.com/sites/default/files/imagecache/inline_image_240x240/2-50-reasons-to-love-hong-kong-wanchai-afp-getty.jpg-_0.jpg

to

http://domain.com/sites/default/files/imagecache/140x140/2-50-reasons-to-love-hong-kong-wanchai-afp-getty.jpg-_0.jpg

It will automatically generate a new image using the preset you used. But in Drupal 7 the above approach is not working.

clean url is not working

bagavmarkiv's picture

You may add here your

server {

...

}

statements for each of your virtual hosts to this file

#

You should look at the following URL's in order to grasp a solid understanding

of Nginx configuration files in order to fully unleash the power of Nginx.

http://wiki.nginx.org/Pitfalls

http://wiki.nginx.org/QuickStart

http://wiki.nginx.org/Configuration

#

Generally, you will want to move this file somewhere, and start with a clean

file but keep this around for reference. Or just disable in sites-enabled.

#

Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.

#

server {
listen 80;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ /index.html;
    # Uncomment to enable naxsi on this location
    # include /etc/nginx/naxsi.rules
}

location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    allow ::1;
    deny all;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
#   proxy_pass http://127.0.0.1:8080;    
#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-cgi alone:
    #fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

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

}

server {
    listen          81;
    server_name     localhost;
    root        /usr/share/phpmyadmin;
    index       index.php index.html index.htm;
    if (!-e $request_filename) {
        rewrite ^/(.+)$ /index.php?url=$1 last;
        break;
    }
    location ~ .php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
    }
    }

another virtual host using mix of IP-, name-, and port-based configuration

#

server {

listen 8000;

listen somename:8080;

server_name somename alias another.alias;

root html;

index index.html index.htm;

#

location / {

try_files $uri $uri/ =404;

}

}

HTTPS server

#

server {

listen 443;

server_name localhost;

#

root html;

index index.html index.htm;

#

ssl on;

ssl_certificate cert.pem;

ssl_certificate_key cert.key;

#

ssl_session_timeout 5m;

#

ssl_protocols SSLv3 TLSv1;

ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;

ssl_prefer_server_ciphers on;

#

location / {

try_files $uri $uri/ =404;

}

}

above is my code .. where i stucked ??

Try my vhost

Afxsoft's picture

I make this file conf https://github.com/Afxsoft/Nginx-Vhost-Drupal and for me it is working

just a thought

elfanjo's picture

hi techwarrior,

can you make socket calls from your dev servers? or can you connect to another server via say telnet?

geraldvillorente's picture

Hi Elfanjo,

Can you point me in the right direction about your post above? Not so techie in system administration field.

have you checked

elfanjo's picture

have you checked http://groups.drupal.org/node/237483 like mogop advised?
there is a lot of information on your issue there, I had the same and tried to describe it as much as possible.

Alternative but not the best way to fix

Suplex's picture

My problem in this case is clean url. When I disable clean url, image style works correctly and the image url becomes 404 not found when clean url is enabled.

Credit to this guy: https://groups.drupal.org/node/237483#comment-780158

My method is changing code in the image module image_style_url function to return un-clean url instead of a clean one.

I already try all of solutions above and it didn't work.

nginx-doc

jebop's picture

You can find a very useful nginx config file for drupal in the package nginx-doc (apt-get install nginx-doc and lookup for /usr/share/doc/nginx-doc/example/drupal.gz on debian).

This config file is the only one that worked for me to solve problems with clean urls, images styles and others. I only needed to add "index index.php" under the line "root /path_to_your_drupal" to resolve 403 issue. Everything else worked out of the box.

Hope it helps.

Nginx

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week