ScaleScaleScaleScale

Tips / Nginx


Nginx configuration for Prestashop

PrestaShop is an open source & free e-commerce CMS. It has been one of the most popular e-commerce solutions since years. It is written in PHP and uses MySQL as default database engine, and today I will teach you how to configure Prestashop rewrite rules for Nginx.

At this point I assume you already installed Nginx and running with php-fpm configured to run as socket, and that you have a line like this in your nginx.conf file:

include "/etc/nginx/conf.d/*.conf";

or

include "/usr/local/conf.d/*.conf";

Then, we will create the virtual host configuration for PrestaShop:

Move to your Nginx directory and create a new file /etc/nginx/conf.d/yoursite.com.conf

pico -w conf.d/yoursite.com.conf

Note: remember to change yoursite.com for your real site name, and tweak this setup as you need.

Then paste this inside:

### yoursite.com

server {
access_log off;
error_log  logs/yoursite.com-error_log warn;

listen 80;
        server_name  yoursite.com www.yoursite.com;
        root /var/www/yoursite.com;
        index index.php index.html;

       # PrestaShop rewrite rules
rewrite ^/([a-z0-9]+)-([a-z0-9]+)(-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*).jpg$ /img/p/$1-$2$3.jpg last;
rewrite ^/([0-9]+)-([0-9]+)/([_a-zA-Z0-9-]*).jpg$ /img/p/$1-$2.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)/([_a-zA-Z0-9-]*).jpg$ /img/c/$1$2.jpg last;
rewrite "^/lang-([a-z]{2})/([a-zA-Z0-9-]*)/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$" /product.php?id_product=$3&isolang=$1$5 last;
rewrite "^/lang-([a-z]{2})/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$" /product.php?id_product=$2&isolang=$1$4 last;
rewrite "^/lang-([a-z]{2})/([0-9]+)-([a-zA-Z0-9-]*)(.*)$" /category.php?id_category=$2&isolang=$1 last;
rewrite ^/([a-zA-Z0-9-]*)/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?id_product=$2$4 last;
rewrite ^/([0-9]+)-([a-zA-Z0-9-]*).html(.*)$ /product.php?id_product=$1$3 last;
rewrite ^/([0-9]+)-([a-zA-Z0-9-]*)(.*)$ /category.php?id_category=$1 last;
rewrite ^/content/([0-9]+)-([a-zA-Z0-9-]*)(.*)$ /cms.php?id_cms=$1 last;
rewrite ^/([0-9]+)__([a-zA-Z0-9-]*)(.*)$ /supplier.php?id_supplier=$1$3 last;
rewrite ^/([0-9]+)_([a-zA-Z0-9-]*)(.*)$ /manufacturer.php?id_manufacturer=$1$3 last;
rewrite "^/lang-([a-z]{2})/(.*)$" /$2?isolang=$1 last;

# static file cache configuration
location ~* .(gif)$ {
expires 2592000s;
}
location ~* .(jpeg|jpg)$ {
expires 2592000s;
}
location ~* .(png)$ {
expires 2592000s;
}
location ~* .(css)$ {
expires 604800s;
}
location ~* .(js)$ {
expires 604800s;
}
location ~* .(js)$ {
expires 604800s;
}
location ~* .(ico)$ {
expires 31536000s;
}

# php-fpm configuration
location ~ .php$ {
root /var/www/yoursite.com;
try_files $uri =404;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}

}

Reload services to apply this changes:

service nginx reload

Popular search terms:

  • yellowzhq
  • 9YRJ
  • acrossa1i
  • adjective7el
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Javier

    Hello,

    Not work in Prestashop 1.6.0.9

    Is there any update of this post?

    Thanks very much!

    Best regards