search
HomeBackend DevelopmentPHP TutorialPHP uses HTTP caching protocol principle analysis and application analysis in Apache environment_PHP tutorial
PHP uses HTTP caching protocol principle analysis and application analysis in Apache environment_PHP tutorialJul 21, 2016 pm 03:40 PM
apachehttpphpDownanalyzeuseprotocolprincipleapplicationenvironmentcacheparsestaticpage

There is also Etag for static pages.

1. Let’s look at the first situation: apache static page

The static page sent by apache to the client generally contains Last-Modified and Etag. These two tags The value comes from the modification time and inode of the static file.

The following is an interception of the header returned by apache to the client

XML/HTML code

Copy code The code is as follows:

Last-Modified: Fri, 26 Jan 2007 01:53:34 GMT
ETag: "3f9f640-318-cb9f8380"

The reason why search engines like static files Because of these two logos, you can determine whether the file has been updated

2. Dynamic pages such as PHP

Since PHP is dynamically generated, its content cannot The last modification date is determined based on the time of the PHP program, so by default PHP does not include any cache control when returning to the client. If you want to make good use of the cache, you must understand the cache mechanism and manage the interaction to reduce b and s, reduce bandwidth traffic, and reduce the load on the server. The burden...the benefits are many.

3. The specific meaning of cache control

First explain the meaning of these tags that I have tested and understood

Cache-Control: Specify The caching mechanism followed by requests and responses. Setting Cache-Control in a request message or response message does not modify the caching process during the processing of another message. The caching instructions during the request include no-cache, no-store, max-age, max-stale, min-fresh, only-if-cached, and the instructions in the response message include public, private, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, max-age.

The meaning of the instructions in each message is as follows:

Public indicates that the response can be cached by any cache area.

Private indicates that all or part of the response message for a single user cannot be processed by the shared cache. This allows the server to only describe a partial response from a user that is not valid for other users' requests.

no-cache indicates that the request or response message cannot be cached.

no-store is used to prevent important information from being released unintentionally. Sending it in the request message will cause both the request and response messages to use caching.

max-age indicates that the client can receive responses with a lifetime no longer than the specified time in seconds.

min-fresh indicates that the client can receive responses with a response time less than the current time plus the specified time.

max-stale indicates that the client can receive response messages beyond the timeout period. If you specify a value for max-stale messages, the client can receive response messages that exceed the specified value of the timeout period.

php usage:

Use header() before output, (if you use ob_start(), you can place the header anywhere in the program)

PHP code
Copy code The code is as follows:

header('Cache-Control: max-age=8');

max-age=8 means the maximum lifetime is 8 seconds. If it exceeds 8 seconds, the browser must go to the server to read it again. This time starts from the time the user reads the page, and Expires is an absolute time.

Expires: The absolute time for cache expiration. If it passes the specified time point, the browser will not recognize the cache and will go to the server to request the latest one again.

Last-Modified: The last modification time of the document. Its wonderful use is: 1

If it is a static file, the client will send the time in its cache, and apache will compare it. If it is found that there is no modification, a header will be returned directly. The status code is 304 and the number of bytes is very small. (The advanced version will also add a comparison Etag to determine whether the file has changed)

2 PHP dynamic file:

The client sends the comparison time, and PHP will determine whether to modify it. If the modification time is the same, only 1024 bytes will be returned. As for why 1024 is returned, it is unknown. If the file generated by your PHP is very large, it will only Returns 1024, so it saves bandwidth. The client will automatically display it from the cache file based on the modification time sent by the server.

Note: Cache-Control and Expires can also work without the Last-Modified header, but each request must return the real number of file bytes, not 1024

4. HOW?

You don’t need to worry about static pages. If you want to better control the caching of static pages, apache has several modules that can be well controlled, which will not be discussed here

php page:

There are two types here:

1. Pages that do not change frequently, similar to news releases. The characteristics of this type of page: there will be several times after the first release. Changes will basically not be modified over time. The control strategy should be: 1. Send Last-Modified for the first release, set max-age for 1 day, update Last-Modified after modification, and max-age time will be normal with the number of modifications. This seems more cumbersome, and you have to record the number of modifications. You can also predict the next possible modification time and use Expires to specify the approximate time to expire

PHP code
Copy code The code is as follows:

//header('Cache-Control: max-age=86400');//Cache for one day
header('Expires: Mon, 29 Jan 2007 08:56:01 GMT');/ /Specify expiration time
header('Last-Modified: '.gmdate('D, d M Y 01:01:01',$time).'GMT');//Greenwich time, $time is the time when the file is added Timestamp

2 Frequently changed pages

are similar to bbs and forum programs. This kind of page updates faster. The main function of the cache is to prevent users from frequently refreshing the list, causing the server to The burden on the database must not only ensure the timeliness of updates, but also ensure that the cache can be utilized

This is generally controlled by Cache-Control, and max-age can be flexibly controlled according to the frequency of postings in the forum.

PHP code
Copy code The code is as follows:

header('Cache-Control: max-age =60');//Cache for one minute
header('Last-Modified: '.gmdate('D, d M Y 01:01:01',$time).'GMT');//Greenwich time, $time is the last updated timestamp of the post

Five extra

1 The difference between refresh, go to, force refresh

Browser There are refresh and go buttons on the browser. Some browsers support using ctrl+F5 to force refresh the page. What is the difference between them?

Go to: When the user clicks on the link, it will go to it. It completely uses the caching mechanism. If there is Last-Modified, it will not communicate with the server. You can use the packet capture tool to see that the sent byte is 0byte. If the cache expires , then it will perform the F5 refresh action.

Refresh (F5): This refresh is also determined based on whether the cache has Last-Modified. If so, it will be transferred to 304 or 1024 (php). If there is no last update time, then go to the server to read it and return the true value. Document size

Forced refresh: completely abandon the cache mechanism, go to the server to read the latest document, and send the header to the server as follows

XML/HTML code
Copy code The code is as follows:

Cache-Control: no-cache

2 Debugging Tools

View browser and A better tool for server interaction is httpwatch pro, the current version 4.1, supports ie7

There are other proxy packet capture tools that can analyze, http debugging. I haven’t used it before, but there is also a tcp packet capture tool, the network that comes with 2000. There are also tcp packet capture tools. The network monitor that comes with 2000 is not specifically for http, but it is difficult to use.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321283.htmlTechArticleThere is also Etag for static pages. 1. Let’s look at the first situation: apache static page. The static page sent by apache to the client generally contains Last-Modified and Etag. The values ​​of these two tags come from...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
图文详解apache2.4+php8.0的安装配置方法图文详解apache2.4+php8.0的安装配置方法Dec 06, 2022 pm 04:53 PM

本文给大家介绍如何安装apache2.4,以及如何配置php8.0,文中附有图文详细步骤,下面就带大家一起看看怎么安装配置apache2.4+php8.0吧~

Linux apache怎么限制并发连接和下载速度Linux apache怎么限制并发连接和下载速度May 12, 2023 am 10:49 AM

mod_limitipconn,这个是apache的一个非官方模块,根据同一个来源ip进行并发连接控制,bw_mod,它可以根据来源ip进行带宽限制,它们都是apache的第三方模块。1.下载:wgetwget2.安装#tar-zxvfmod_limitipconn-0.22.tar.gz#cdmod_limitipconn-0.22#vimakefile修改:apxs=“/usr/local/apache2/bin/apxs”#这里是自己apache的apxs路径,加载模块或者#/usr/lo

apache版本怎么查看?apache版本怎么查看?Jun 14, 2019 pm 02:40 PM

查看​apache版本的步骤:1、进入cmd命令窗口;2、使用cd命令切换到Apache的bin目录下,语法“cd bin目录路径”;3、执行“httpd -v”命令来查询版本信息,在输出结果中即可查看apache版本号。

超细!Ubuntu20.04安装Apache+PHP8环境超细!Ubuntu20.04安装Apache+PHP8环境Mar 21, 2023 pm 03:26 PM

本篇文章给大家带来了关于PHP的相关知识,其中主要跟大家分享在Ubuntu20.04 LTS环境下安装Apache的全过程,并且针对其中可能出现的一些坑也会提供解决方案,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

nginx,tomcat,apache的区别是什么nginx,tomcat,apache的区别是什么May 15, 2023 pm 01:40 PM

1.Nginx和tomcat的区别nginx常用做静态内容服务和代理服务器,直接外来请求转发给后面的应用服务器(tomcat,Django等),tomcat更多用来做一个应用容器,让javawebapp泡在里面的东西。严格意义上来讲,Apache和nginx应该叫做HTTPServer,而tomcat是一个ApplicationServer是一个Servlet/JSO应用的容器。客户端通过HTTPServer访问服务器上存储的资源(HTML文件,图片文件等),HTTPServer是中只是把服务器

php站用iis乱码而apache没事怎么解决php站用iis乱码而apache没事怎么解决Mar 23, 2023 pm 02:48 PM

​在使用 PHP 进行网站开发时,你可能会遇到字符编码问题。特别是在使用不同的 Web 服务器时,会发现 IIS 和 Apache 处理字符编码的方法不同。当你使用 IIS 时,可能会发现在使用 UTF-8 编码时出现了乱码现象;而在使用 Apache 时,一切正常,没有出现任何问题。这种情况应该怎么解决呢?

如何在 RHEL 9/8 上设置高可用性 Apache(HTTP)集群如何在 RHEL 9/8 上设置高可用性 Apache(HTTP)集群Jun 09, 2023 pm 06:20 PM

Pacemaker是适用于类Linux操作系统的高可用性集群软件。Pacemaker被称为“集群资源管理器”,它通过在集群节点之间进行资源故障转移来提供集群资源的最大可用性。Pacemaker使用Corosync进行集群组件之间的心跳和内部通信,Corosync还负责集群中的投票选举(Quorum)。先决条件在我们开始之前,请确保你拥有以下内容:两台RHEL9/8服务器RedHat订阅或本地配置的仓库通过SSH访问两台服务器root或sudo权限互联网连接实验室详情:服务器1:node1.exa

【总结】apache调用不起来php5.6的原因和解决方法【总结】apache调用不起来php5.6的原因和解决方法Mar 28, 2023 pm 03:00 PM

PHP5.6和Apache是Web开发中最常用的工具之一。然而,有时候,当我们尝试调用PHP5.6时,会遇到一些问题,例如无法调用PHP文件或页面等等。在本文中,我将探讨为什么您可能无法调用PHP5.6以及如何解决这个问题。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software