Magento – Nginx working configuration for 1.7.XX

ce-1.7.0.2configurationnginxoverridesurl

I have this store running on Apache, Magento 1.7.0.2

Then I decided to clone the application on a test server, running Nginx.

I first tried mine, naive rewrite:

rewrite ^(.*index\.php)\/(.*) $1?$2 last;

This worked, although not in the entire store. Recent viewed product pages didn't. They resulted in 404 by nginx.

Then I followed the steps outlined in: http://www.howtoforge.com/running-magento-1.6.0.0-on-nginx-lemp-on-debian-squeeze-ubuntu-11.04

My server configuration was updated. Then, nothing worked, and the 404 error was from Magento in every link (instead of nginx 404).

Can you please confirm that the above link configuration is good ? Notice that I just cloned the files from production server to the test server (didn't install Magento). It will be really nice if re-install isn't necessary.

I already updated web/unsecure/base_url and web/secure/base_url on the database.

Best Answer

I've posted a working sample config below. Included is a set of rewrite rules that allows us to serve up static html pages from the "static" directory. If we're advertising a product like http://yourdomain.com/super-popular.html that we expect many thousands of concurrent hits to, we may opt to save the html of super-popular.html in the static directory to skip php entirely. Nginx absolutely crushes Apache in serving up these pages in particular.

We've experienced rock solid performance with the config below.

server {
    listen 80 default ;
    server_name www.yourdomain.com;
    root /var/www/production/web;

    ## allow for html source of super high volume product pages to be put in "static" directory and served without php
    if ($http_host ~ "^(.*)yourdomain.com"){
        set $rule_0 1;
    }
    if ($uri ~ "^(.*)$"){
        set $rule_0 2$rule_0;
    }
    if ($http_referer !~* ".*yourdomain.com"){
        set $rule_0 3$rule_0;
    }
    if (-f $document_root/static$request_uri){
        set $rule_0 4$rule_0;
    }
    if ($rule_0 = "4321"){
        rewrite ^/.*$ /static/$request_uri last;
    }

    ## Images and static content is treated different
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
            access_log        off;
            expires           30d;
            root /var/www/production/web/;
    }

    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 /nginx-config/       { deny all; }

    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_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_read_timeout 120;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE yourdomain_store;
        fastcgi_param  MAGE_RUN_TYPE store;

        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }

  access_log /var/log/nginx/yourdomain.com-access_log combined;
}