I have this store running on Apache, Magento

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-

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.

  • 1
    Stick to what you know and can support. Nginx isn't going to make your store any faster - and if you're tripping up at this stage; just picture how panicked you'll be when your production store is falling down with random errors. Some reading, sonassi.com/knowledge-base/magento-kb/mythbusting/… Feb 15, 2013 at 14:38
  • @sonassi The test server is hosting other apps, specially Redmine. So I took the nginx way for configuring it already. It would be nice to make it work for Magento too. The production server will keep running Apache.
    – Niloct
    Feb 15, 2013 at 14:46
  • How can I check the URL that Magento tried to find when giving 404 error ?
    – Niloct
    Feb 15, 2013 at 15:59
  • @Niloct Check your access logs, they should give you the requests that were causing the 404. Feb 18, 2013 at 9:26
  • @RichardCleverley The access.log from nginx shows a GET in root directory, nothing else. I managed to trace the problem.
    – Niloct
    Feb 28, 2013 at 2:39

1 Answer 1


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_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;
  • 2
    I traced the problem. I commented the MAGE_RUN_CODE, letting Magento put the default value "" in index.php, and then the code worked... Sorry for the delay, so many problems being solved but I traced this one like that. Thank you.
    – Niloct
    Feb 28, 2013 at 2:42

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.