تكوين Nginx لـ Sylius

بدأنا مؤخرًا في استخدام منصة Sylius ذات الشعبية المتزايدة لأحد مشاريع التجارة الإلكترونية الخاصة بنا. يعتمد Sylius على Symfony2 ، إطار عمل PHP الرائد الذي عملنا معه على موقع التصوير الفوتوغرافي الخاص بنا . على الرغم من أننا نستخدم WooCommerce للمشاريع الصغيرة ، فإن Sylius توفر حلاً أكثر حداثة وقوة نعتقد أنه بالتأكيد أكثر ملاءمة لمواقع التجارة الإلكترونية الأكثر طموحًا.

كانت المشكلة التي واجهناها في البداية هي جعل Sylius يعمل بشكل صحيح على خادم الويب Nginx + PHP-FPM + Ubuntu 14.04. بدأنابملف تكوين Symfony2 قياسي بالإضافة إلى بعض التحسينات ، لكن لم نتمكن من تشغيل أي صور مصغرة للمنتج. تبين أن LiipImagineBundle يُستخدم لإنشاء صور مصغرة أثناء التنقل ، وكان هذا يتداخل مع مجموعة قواعد انتهاء صلاحية ذاكرة التخزين المؤقت ، مما أدى إلى ظهور 404 ثانية لأي صور مصغرة حاولنا عرضها.

إليك ما كنا نستخدمه بشكل غير صحيح لتعيين تاريخ انتهاء صلاحية لمدة عام واحد في المستقبل للملفات الثابتة:

location ~ \.(js|css|png|jpeg|jpg|gif|ico|swf|flv|pdf|zip)$ { # Set expiry date to 1 year in the future. expires 365d ; }
لغة الكود: Nginx ( nginx )

بينما يعمل هذا عادةً بدون مشكلة ، إلا أنه يعني أيضًا أن أي عنوان URL ينتهي بأحد هذه الامتدادات سيتجاوز ملف app.php تمامًا ، سواء كان ملف الصورة الفعلي موجودًا أم لا. نظرًا لأن الصور المصغرة في Sylius يتم إنشاؤها أثناء التنقل بناءً على طلب URL ، فإن هذا يكسر تمامًا إنشاء الصورة المصغرة.

الإصلاح مباشر للغاية: نحن نحدد فقط تاريخ انتهاء الصلاحية للملفات المطلوبة الموجودة بالفعل. وإلا فإننا نعيد كتابتها في ملف app.php كالمعتاد. هذا هو ملف إعداد Nginx النهائي الذي استخدمناه:

server { server_name example.com; root /var/www/example.com/web; location / { try_files $uri @rewriteapp ; # Redirect to app.php if the requested file does not exist. } # Development rule-set. # This rule should only be placed on your development environment. # In production, don't include this and don't deploy app_dev.php or config.php. location ~ ^/(app_dev|config)\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$ ; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name ; fastcgi_param HTTPS off ; } # Production rule-set. location ~ ^/app\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$ ; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name ; fastcgi_param HTTPS off ; # Prevents URIs that include the front controller. This will 404: # http://domain.tld/app.php/some-path # Remove the internal directive to allow URIs like this. internal; } # Static files rule-set. location ~ \.(js|css|png|jpeg|jpg|gif|ico|swf|flv|pdf|zip)$ { # Set rules only if the file actually exists. if (-f $request_filename ) { # Set expiry date to 1 year in the future. expires 365d ; # Further optimize by not logging access to these files. access_log off ; } # Rewrite to app.php if the requested file does not exist. try_files $uri @rewriteapp ; } # Rewrite rule for PHP files. location @rewriteapp { rewrite ^(.*)$ /app.php/ $1 last ; } error_log /var/log/nginx/example.com_error.log; access_log /var/log/nginx/example.com_access.log; }
لغة الكود: Nginx ( nginx )

هذا هو. يتم الآن إنشاء الصور المصغرة بشكل صحيح أثناء التنقل ، بينما تستخدم الصور الموجودة أيضًا رؤوس انتهاء الصلاحية المناسبة. أخبرنا إذا كان هذا يعمل من أجلك ، أو إذا كنت تستخدم طريقة مختلفة. ترميز سعيد!