While CubeCart’s default configuration targets Apache with mod_rewrite, it works well with Nginx too. You just need to add the equivalent rewrite rules to your Nginx server block.

Basic Nginx Configuration

Add the following to your site’s Nginx server block (typically in /etc/nginx/sites-available/yourdomain.conf):

server {
    listen 80;
    listen 443 ssl;
    server_name www.yourdomain.com yourdomain.com;
    root /path/to/cubecart;
    index index.php;

    # SSL (adjust paths to your certificate)
    ssl_certificate /etc/ssl/certs/yourdomain.crt;
    ssl_certificate_key /etc/ssl/private/yourdomain.key;

    # Force HTTPS
    if ($scheme = http) {
        return 301 https://$host$request_uri;
    }

    # CubeCart SEO URL rewriting
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # PHP processing
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Deny access to sensitive files
    location ~ /\.ht {
        deny all;
    }

    location ~ /(includes|classes|cache|files/cache)/ {
        deny all;
    }

    # Static file caching
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # Gzip compression
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript image/svg+xml;
    gzip_min_length 1000;
}

Key Differences from Apache

Feature Apache Nginx
URL rewriting .htaccess with mod_rewrite try_files in server block
PHP processing mod_php or PHP-FPM PHP-FPM (required)
Config location .htaccess (per-directory) Server block (centralised)
Access control .htaccess directives location blocks

Important Notes

  • Nginx does not read .htaccess files. All rules must go in the server block configuration.
  • After editing Nginx config, test with nginx -t and reload with sudo systemctl reload nginx.
  • Ensure PHP-FPM is installed and the socket path matches your configuration.
  • The includes/, classes/, and cache directories should be denied to prevent direct access.

PHP-FPM Configuration

Ensure your PHP-FPM pool has adequate resources:

  • pm.max_children: At least 5 (increase for busier stores)
  • php_admin_value[memory_limit]: 256M
  • php_admin_value[max_execution_time]: 120

Troubleshooting

  • 404 errors on pages: Ensure the try_files directive is correct
  • Blank pages: Check PHP-FPM logs (/var/log/php-fpm/)
  • Permission errors: Nginx user (usually www-data) must have read access to CubeCart files