Nginx Magento vhost Configuration

From Brian Nelson Ramblings
Jump to: navigation, search
Install Nginx - PHP+FPM - Percona MySQL

Nginx - Magento vhost Configuration

The web is all about speed, the faster your site the better your search results. One way to increase your sites speed is to use Nginx, Nginx is light weight and powerful.

Working for a company that specializes in Magneto web hosting, that primarily uses Apache, I thought I would setup a site using Nginx. To check the if that makes a large difference.

After installing Nginx and getting the server aspects setup, you will want to configure your vhost file with the default .htaccess rules that come with Mangento.

Here is the configurations from Nginx - http://wiki.nginx.org/Magento

They have two separate configurations for Magento versions.

This config works for version 1.7 - and above

server {
 root	   /home/magento/web/;
 index	   index.php;
 server_name	magento.example.com;
 location / {
   index index.html index.php;
   try_files $uri $uri/ @handler;
   expires 30d;
 }
 location ~ ^/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
 location /var/export/ { internal; }
 location /. { return 404; }
 location @handler { rewrite / /index.php; }
 location ~* .php/ { rewrite ^(.*.php)/ $1 last; }
 location ~* .php$ {
   if (!-e $request_filename) { rewrite / /index.php last; }
   expires off;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param MAGE_RUN_CODE default;
   fastcgi_param MAGE_RUN_TYPE store;
   include fastcgi_params;
 }
}

This config currently seems to work for the <1.4 versions

server {
 server_name example.com;
 root /var/www/vhost/example.com/htdocs;
 access_log /var/log/nginx/example.com.access.log main;
 index index.php;

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

 # set a nice expire for assets
 location ~* "^.+\.(jpe?g|gif|css|png|js|ico|pdf|zip|tar|t?gz|mp3|wav|swf)$" {
   expires    max;
   add_header Cache-Control public;
 }

 # the downloader has its own index.php that needs to be used
 location ~* ^(/downloader|/js|/404|/report)(.*) {
   include fastcgi_params;
   fastcgi_index index.php;
   fastcgi_param  SCRIPT_FILENAME  $document_root$1/index.php$1;
   fastcgi_read_timeout 600;
   fastcgi_pass  127.0.0.1:9000;
 }

 location ~* \.php {
   include fastcgi_params;
   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
   fastcgi_read_timeout 600;
   fastcgi_pass  127.0.0.1:9000;
 }

}

Directly From Magento

https://www.magentocommerce.com/wiki/1_-_installation_and_configuration/configuring_nginx_for_magento

I have since also found this version from Magento. Use all others at your own risk!!

   server {
       listen 80;
       server_name DOMAIN.com;
       rewrite / $scheme://www.$host$request_uri permanent; ## Forcibly prepend a www
   }
    
   server {
       listen 80 default;
   ## SSL directives might go here
       server_name www.DOMAIN.com *.DOMAIN.com; ## Domain is here twice so server_name_in_redirect will favour the www
       root /var/www/vhosts/DOMAIN.com;
    
       location / {
           index index.html index.php; ## Allow a static html file to be shown first
           try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
           expires 30d; ## Assume all files are cachable
       }
    
       ## These locations would be hidden by .htaccess normally
       location ^~ /app/                { deny all; }
       location ^~ /includes/           { deny all; }
       location ^~ /lib/                { deny all; }
       location ^~ /media/downloadable/ { deny all; }
       location ^~ /pkginfo/            { deny all; }
       location ^~ /report/config.xml   { deny all; }
       location ^~ /var/                { deny all; }
    
       location /var/export/ { ## Allow admins only to view export folder
           auth_basic           "Restricted"; ## Message shown in login window
           auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
           autoindex            on;
       }
    
       location  /. { ## Disable .htaccess and other hidden files
           return 404;
       }
    
       location @handler { ## Magento uses a common front handler
           rewrite / /index.php;
       }
    
       location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
           rewrite ^(.*.php)/ $1 last;
       }
    
       location ~ .php$ { ## Execute PHP scripts
           if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
    
           expires        off; ## Do not cache dynamic content
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_param  HTTPS $fastcgi_https;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
           fastcgi_param  MAGE_RUN_TYPE store;
           include        fastcgi_params; ## See /etc/nginx/fastcgi_params
       }
   }

Additional Nginx Setups