proxy cache не кэширует http 1.0

commeta nginx-forum на forum.nginx.org
Вт Окт 29 23:04:07 UTC 2019


server {
	server_name mysite.ru www.mysite.ru;
	charset UTF-8;
	index index.php index.html;
	disable_symlinks if_not_owner from=$root_path;
	include /etc/nginx/vhosts-includes/*.conf;
	include /etc/nginx/vhosts-resources/mysite.ru/*.conf;
	access_log /var/www/httpd-logs/mysite.ru.access.log;
	error_log /var/www/httpd-logs/mysite.ru.error.log notice;
	set $root_path /var/www/www-root/data/www/mysite.ru;
	root $root_path;
	location / {
		location ~ [^/]\.ph(p\d*|tml)$ {
			try_files /does_not_exists @fallback;
		}
		location ~*
^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf|woff|woff2|ico|ttf)$
{
			try_files $uri $uri/ @fallback;
		}
		location / {
			try_files /does_not_exists @fallback;
		}
	}
	location @fallback {
		proxy_pass http://127.0.0.1:8080;
		proxy_redirect http://127.0.0.1:8080 /;
		proxy_set_header Host $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_set_header X-Forwarded-Port $server_port;
		access_log off;
	}
	return 301 https://$host:443$request_uri;
	gzip on;
	gzip_comp_level 9;
	gzip_disable "msie6";
	gzip_types text/plain text/css application/json application/x-javascript
text/xml application/xml application/xml+rss text/javascript
application/javascript;
	listen 19.19.19.19:80;
}
server {

	# show cache status and any skip cache reason
	add_header Bullet-Proxy-Cache $upstream_cache_status;
	add_header Cache-BYPASS-Reason $skip_reason;

	# define nginx variables
	set $skip_cache 1;
	set $skip_reason "";

	if ($request_uri ~* "/|/*.html|/*.txt|/*.xml") {
		set $skip_cache 0;
		set $skip_reason HTML; 
	}

	# security for bypass so localhost can empty cache
	if ($remote_addr ~ "^(127.0.0.1|19.19.19.19)$") {
		set $bypass $http_secret_header;
	}

	# skip caching cookies
	if ($http_cookie ~* "modx_admin" ) {
		set $skip_cache 1;
		set $skip_reason Cookie; 
	}

	# Don't cache URIs containing the following segments
	if ($request_uri ~* "/manager/|/*.php") {
		set $skip_cache 1;
		set $skip_reason URI; 
	}
			
	# skip caching POST
	if ($request_method = POST) {
		set $skip_cache 1;
		set $skip_reason POST; 
	}


	server_name mysite.ru www.mysite.ru;
	ssl_certificate "/var/www/httpd-cert/www-root/mysite.ru_le3.crtca";
	ssl_certificate_key "/var/www/httpd-cert/www-root/mysite.ru_le3.key";
	ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
	ssl_prefer_server_ciphers on;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
	charset UTF-8;
	index index.php index.html;
	disable_symlinks if_not_owner from=$root_path;
	include /etc/nginx/vhosts-includes/*.conf;
	include /etc/nginx/vhosts-resources/mysite.ru/*.conf;
	access_log /var/www/httpd-logs/mysite.ru.access.log;
	error_log /var/www/httpd-logs/mysite.ru.error.log notice;
	set $root_path /var/www/www-root/data/www/mysite.ru;
	root $root_path;
	location / {
		location ~ [^/]\.ph(p\d*|tml)$ {
			try_files /does_not_exists @fallback;
		}
		location ~*
^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf|woff|woff2|ico|ttf)$
{
			try_files $uri $uri/ @fallback;
			expires 30d;
		}
		location / {
			try_files /does_not_exists @fallback;
		}

	}
	location @fallback {
		proxy_cache mysite.ru;
		proxy_cache_revalidate on;
		proxy_ignore_headers Expires Cache-Control X-Accel-Expires;


		#proxy_hide_header "Set-Cookie";
		#expires -1;
		#add_header Last-Modified $sent_http_Expires;


		proxy_cache_use_stale error timeout invalid_header updating http_500
http_502 http_503 http_504;
		proxy_cache_bypass $skip_cache;
		proxy_no_cache $skip_cache;
		proxy_cache_valid 301 302 0;
		proxy_cache_valid 200 24h;
		proxy_cache_valid 404 502 503 1m;


		# mobile users
		set $is_mobile 0;
		if ($http_user_agent ~* '(iPhone|iPod|mobile|Android|2.0\
MMP|240x320|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|hiptop|IEMobile)')
{
			set $is_mobile 1;
		}
		
 set $is_gzip 0;
		if ($http_accept_encoding ~* 'gzip') {
			set $is_gzip 1;
		}

		proxy_cache_key
$request_method|$http_if_modified_since|$http_if_none_match|$host|$request_uri|$is_args|$is_mobile|$is_gzip;



		proxy_pass http://127.0.0.1:8080;
		proxy_redirect http://127.0.0.1:8080 /;
		proxy_set_header Host $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
		proxy_set_header X-Forwarded-Port $server_port;
		access_log off;
	}
	gzip on;
	gzip_comp_level 9;
	gzip_disable "msie6";
	gzip_types text/plain text/css application/json application/x-javascript
text/xml application/xml application/xml+rss text/javascript
application/javascript;
	listen 19.19.19.19:443 ssl http2;
}

этот конфиг кэширует прокси запросы к apache, при отдаче сообщает в
заголовках ответа, если есть в кэше то HIT а если нету то MISS.

Проблема в том что при запросах из браузера (http 2 по логам) запросы
кэшируются, первый MISS последующие HIT.

А когда запрос идет через curl (http 1.0 по логам) то все время MISS.
Подскажите что я делаю не так? Как сделать чтобы HTTP 1.0 тоже сохранялся в
кэше?

Posted at Nginx Forum: https://forum.nginx.org/read.php?21,286046,286046#msg-286046



Подробная информация о списке рассылки nginx-ru