LINUX.ORG.RU

Не работает expires max nginx

 ,


1

3

Помогите понять почему не работает expires max в nginx. Есть связка nginx+php-fpm+memcache, но почему то не работает кэширование. developers.google.com говорит, что статика не кешируется.

Сам конфиг nginx:

server {
        listen 80;
        listen [::]:80;
        server_name .example.com;
#       return 301 https://example.com$request_uri;
}

server {

    fastcgi_pass_header Last-Modified;

    gzip on;
    gzip_comp_level 3;
    gzip_types application/x-javascript application/javascript text/css;

    pagespeed on;
    pagespeed RewriteLevel OptimizeForBandwidth;
    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    pagespeed LoadFromFile "https://example.com" "/home/www/example.com/www/";
    pagespeed FetchHttps enable;

    pagespeed EnableFilters collapse_whitespace,rewrite_css,rewrite_js,combine_css,combine_javascript,prioritize_critical_css,insert_dns_prefetch;
    pagespeed DisableFilters convert_to_webp_animated,convert_jpeg_to_webp,convert_to_webp_lossless,recompress_webp;
    pagespeed EnableFilters in_place_optimize_for_browser;
    pagespeed InPlaceResourceOptimization on;
    pagespeed EnableFilters extend_cache;
    pagespeed EnableCachePurge on;
    pagespeed ImplicitCacheTtlMs 1209600000;
    pagespeed ImagePreserveURLs on;

    keepalive_timeout   70;

    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_stapling on;
    ssl_prefer_server_ciphers on;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         "HIGH:!RC4:!aNULL:!MD5:!kEDH";

    listen              443 ssl http2;
    server_name .example.com;
    charset off;
    access_log /var/log/nginx/example.com-access.log;
    error_log /var/log/nginx/example.com-error.log;
    set $root_path /home/www/example.com/www;
    root $root_path;

    set $php_sock unix:/var/run/php-fpm/php-fpm.sock;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    client_max_body_size 1024M;
    client_body_buffer_size 10M;
    client_body_timeout 60;
    send_timeout 60;
    index index.php;
    autoindex off;
    error_page 404 /404.php;

        if (!-e $request_filename) {
                rewrite  ^(.*)$  /bitrix/urlrewrite.php last;
        }

        location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
                expires 7d;
        }

        if ($host = 'www0.example.com') {
                return 301 https://example.com$request_uri;
        }

        location @bitrix {
                add_header Cache-Control "private, max-age=1209600";
                fastcgi_pass    $php_sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root/bitrix/urlrewrite.php;
        }

        location ~* /bitrix/admin.+\.php$ {
                pagespeed off;
                try_files       $uri @bitrixadm;
                fastcgi_pass    $php_sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PHP_ADMIN_VALUE "max_execution_time = 1200";
                fastcgi_read_timeout 1200;
                fastcgi_send_timeout 1200;
                include fastcgi_params;
        }

        location @bitrixadm {
                fastcgi_pass    $php_sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root/bitrix/admin/404.php;
        }

        location = /favicon.ico {
                log_not_found off;
                access_log off;
                expires 7d;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        # ht(passwd|access)
        location ~* /\.ht  { deny all; }

        # repositories
        location ~* /\.(svn|hg|git) { deny all; }

        # Player options, disable no-sniff
        location ~* ^/bitrix/components/bitrix/player/mediaplayer/player$ {
          add_header Access-Control-Allow-Origin *;
        }

        location ~* ^.+\.(jpg|jpeg|gif|png|svg|ttf|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar)$ {
                access_log /var/log/nginx/example.com-access.log;
                expires max;
                error_page 404 /404.html;
                try_files $uri /404.html;
        }

        location ~ \.php$ {
                pagespeed off;
                try_files       $uri @bitrix;
                fastcgi_pass    $php_sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

Ты бы хоть скрин показал, чтобы понимать, что не нравится картонной дурилке под названием developers.google.com.

Скрин в студию, может ей не нравится, что не кешируются ресурсы, которые ты подгружаешь со сторонних сайтов, а не со своего.

Twissel ★★★★★
()
Ответ на: комментарий от perrfect

Я вроде бы скрин просил, чтобы увидеть, что конкретно тестилке не по нраву.

Данные сайта можешь затереть на скрине. Судя по конфигу, все ок.

Twissel ★★★★★
()
Ответ на: комментарий от perrfect

На статику для битрикса у меня вот так все работало

        # Static content
        location ~* ^/(upload|bitrix/images|bitrix/tmp) {
          expires 30d;
        }

        location  ~* \.(css|js|gif|png|jpg|jpeg|ico|ogg|ttf|woff|eot|otf)$ {
          error_page 404 /404.html;
          expires 30d;
        }

        location = /404.html {
                access_log off ;
        }

Можно указать expires max будет то же самое, все должно кешироваться nginx'ом.

Twissel ★★★★★
()
Последнее исправление: Twissel (всего исправлений: 2)

до кучи

Вот тебе полный рабочий конфиг

server {
        listen *:80;
        server_name site.shop www.site.shop;
        return 301 https://$host$request_uri;
}

server {
        listen *:443 ssl http2;
	server_name site.shop www.site.shop;
	if ($host = 'www.site.shop' ) {
            return 301 https://$host$request_uri;
        }
	keepalive_timeout 75 75;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:50m;
        ssl_session_timeout 1d;
        ssl_certificate /etc/letsencrypt/live/site.shop/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/site.shop/privkey.pem;
        ssl_dhparam /etc/ssl/dhparam.pem;
        ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
        ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
        add_header Strict-Transport-Security 'max-age=31536000';
        ssl_stapling on;
	resolver 8.8.4.4 8.8.8.8 valid=300s;
        ssl_stapling_verify on;
	charset off;
	gzip on;
	gzip_comp_level 5;
	gzip_types application/x-javascript application/javascript text/css;
	index index.php;

	
    access_log /var/log/nginx/site.shop_ssl-access.log;
    error_log /var/log/nginx/site.shop_ssl-error.log;
	
	ssi on;
	set $root_path /var/www/html;
	root $root_path;
	
	set $php_sock unix:/var/run/php-fpm/php-fpm.sock;
	    
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header Host $host;
	
	client_max_body_size 1024M;
	client_body_buffer_size 4M;
	
	#if ($request_uri ~* "^(.*\/)index\.php$") {
        #return 301 $1;
    #}
	location / {
		try_files       $uri $uri/ @bitrix;
		rewrite ^([^.\?]*[^/])$ $1/ permanent;
}
 
        

        location ~* /upload/.*\.(php|php3|php4|php5|php6|phtml|pl|asp|aspx|cgi|dll|exe|shtm|shtml|fcg|fcgi|fpl|asmx|pht|py|psp|rb|var)$ {
                types {
                        text/plain text/plain php php3 php4 php5 php6 phtml pl asp aspx cgi dll exe ico shtm shtml fcg fcgi fpl asmx pht py psp rb var;
                }
        }

        location ~ \.php$ {
                try_files       $uri @bitrix;
                fastcgi_pass    $php_sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f admin@site.shop";
                include fastcgi_params;
        }
        location @bitrix {
                fastcgi_pass    $php_sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root/bitrix/urlrewrite.php;
                fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f admin@site.shop";
        }
        location ~* /bitrix/admin.+\.php$ {
                try_files       $uri @bitrixadm;
                fastcgi_pass    $php_sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f admin@site.shop";
                include fastcgi_params;
        }
        location @bitrixadm{
                fastcgi_pass    $php_sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root/bitrix/admin/404.php;
                fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f admin@site.shop";
        }

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
        #
        # block this locations for any installation
        #

        # ht(passwd|access)
        location ~* /\.ht  { deny all; }

        # repositories
        location ~* /\.(svn|hg|git) { deny all; }

        # bitrix internal locations
        location ~* ^/bitrix/(modules|local_cache|stack_cache|managed_cache|php_interface) {
          deny all;
        }

        # upload files
        location ~* ^/upload/1c_[^/]+/ { deny all; }

        # use the file system to access files outside the site (cache)
        location ~* /\.\./ { deny all; }
        location ~* ^/bitrix/html_pages/\.config\.php { deny all; }
        location ~* ^/bitrix/html_pages/\.enabled { deny all; }

        # Intenal locations
        location ^~ /upload/support/not_image   { internal; }

        # Cache location: composite and general site
        location ~* @.*\.html$ {
          internal;
          # disable browser cache, php manage file
          expires -1y;
          add_header X-Bitrix-Composite "Nginx (file)";
        }

        # Player options, disable no-sniff
        location ~* ^/bitrix/components/bitrix/player/mediaplayer/player$ {
          add_header Access-Control-Allow-Origin *;
        }

        # Accept access for merged css and js
        location ~* ^/bitrix/cache/(css/.+\.css|js/.+\.js)$ {
          expires 30d;
          error_page 404 /404.html;
        }

        # Disable access for other assets in cache location
        location ~* ^/bitrix/cache              { deny all; }

        # Use nginx to return static content from s3 cloud storage
        # /upload/bx_cloud_upload/<schema>.<backet_name>.<s3_point>.amazonaws.com/<path/to/file>
        location ^~ /upload/bx_cloud_upload/ {
          location ~ ^/upload/bx_cloud_upload/(http[s]?)\.([^/:]+)\.(s3|s3-us-west-1|s3-eu-west-1|s3-ap-southeast-1|s3-ap-northeast-1)\.amazonaws\.com/(.+)$ {
                internal;
                resolver 8.8.8.8;
                proxy_method GET;
                proxy_set_header    X-Real-IP               $remote_addr;
                proxy_set_header    X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header    X-Forwarded-Server      $host;
                #proxy_max_temp_file_size 0;
                proxy_pass $1://$2.$3.amazonaws.com/$4;
          }
          location ~* .*$       { deny all; }
        }
        # Static content
        location ~* ^/(upload|bitrix/images|bitrix/tmp) {
          expires 30d;
        }

        location  ~* \.(css|js|gif|png|jpg|jpeg|ico|ogg|ttf|woff|eot|otf)$ {
          error_page 404 /404.html;
          expires 30d;
        }

        location = /404.html {
                access_log off ;
        }
	
	
	
}
Twissel ★★★★★
()
Ответ на: комментарий от perrfect

если nginx свежее 1.10, то там рядом лежит бинарник nginx-debug и в systemd можно остановить nginx.service и стартануть nginx-debug.service. в дебажных логах будет видно, может у тебя location не отрабатывает.

SevikL ★★★★★
()
Ответ на: комментарий от perrfect

Не за что)) Я этот модуль стараюсь использовать только в случае крайней необходимости, т.к. чаще с ним проблем больше, чем без него.

Пометь тему решенной.

Twissel ★★★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.