• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / nginx server tutorials (installation, configuration, performance tuning, security) / nginx add last modified header (remove, php wordpress)

nginx add last modified header (remove, php wordpress)

Table of Contents

Toggle
  • last modified header
  • add header Last-Modified
  • Disable Last modified header in nginx
  • Cloudflare Nginx Last modified header
  • Last modified header plugins for WordPress
  • for wordpress add this to theme function code

last modified header

 

Syntax: add_header name value [always];
Default: —
Context: http, server, location, if in location

Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). Parameter value can contain variables.

There could be several add_header directives. These directives are inherited from the previous configuration level if and only if there are no add_header directives defined on the current level.

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.

 

add header Last-Modified

 

Nginx adds last modified date for files cached html files in WordPress cached pages automatically rather than published date, it adds cached or stored date in SSD or disk.

for wordpress we need to add this plugin as of now working ..  Last-Modified and If-Modified-Since Headers

Nginx Module ngx_http_headers_module

 

if_modifeid_since exact;

exact: Returns 304 Not Modified if the date and time specified in the HTTP header are an exact match with the actual requested file modification date. If the file modification date is earlier or later, the file is served normally (200 OK response).

Disable Last modified header in nginx

by default nginx adds last modified header for static files css,js images and html which are stored in disk. not from mysql for this we need to tweak php.

if_modified_since off;

Removing Last-Modified header:

add_header Last-Modified “”;

 

 

Cloudflare Nginx Last modified header

When both Last-Modified and Etag headers are absent from the origin server response, Smart Edge Revalidation will use the time the object was cached on Cloudflare’s edge as the Last-Modified header value.

 

Problem: cache ttl sets for 2 hours the page last modified time always within 2 hours, google may ignore the last modified header or updated genuine pages.

 

Change alter the last modified date

 

add_header Last-Modified $date_gmt;
if_modified_since off;
etag off;

As for the last line, if you really want to hide a true last-modified date, then you must hide the ETag header too since it leaks timestamps.

add_header Last-Modified $date_gmt;   (the present date prints)

ssi_last_modified on

Allows preserving the “Last-Modified” header field from the original response during SSI processing to facilitate response caching.

Add headers Last modified exact date with PHP

 

<?php
//get the last-modified-date of this very file
$lastModified=filemtime(__FILE__);
//get a unique hash of this file (etag)
$etagFile = md5_file(__FILE__);
//get the HTTP_IF_MODIFIED_SINCE header if set
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
//get the HTTP_IF_NONE_MATCH header if set (etag: unique file hash)
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);

//set last-modified header
header(“Last-Modified: “.gmdate(“D, d M Y H:i:s”, $lastModified).” GMT”);
//set etag-header
//header(“Etag: $etagFile”);
header(“ETag: \”$etagFile\””);
//make sure caching is turned on
header(‘Cache-Control: private, must-revalidate, proxy-revalidate, max-age=3600’);

//check if page has changed. If not, send 304 and exit
if (@strtotime($_SERVER[‘HTTP_IF_MODIFIED_SINCE’])==$lastModified || $etagHeader == $etagFile)
{
header(“HTTP/1.1 304 Not Modified”);
header(“Vary: Accept-Encoding”);
exit;
}
?>

 

Last modified header plugins for WordPress

 

LH Add Headers

 

Check HTTP Headers in linux terminal

 

curl -I https://sarkariresultz.in/iti-jobs/

 

root@ubuntu-s-2vcpu-4gb-intel-blr1-01:~# curl -I https://sarkariresultz.in/iti-jobs/
HTTP/2 200
date: Sun, 19 Mar 2023 10:04:52 GMT
content-type: text/html
last-modified: Sat, 18 Mar 2023 06:26:48 GMT  (ETAG on)
vary: Accept-Encoding, Cookie
cache-control: no-cache, no-store, must-revalidate
x-rocket-nginx-serving-static: HIT
cf-cache-status: DYNAMIC

strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
server: cloudflare
cf-ray: 7aa4e4c7a9949bd7-FRA
alt-svc: h3=”:443″; ma=86400, h3-29=”:443″; ma=86400

 

 

for wordpress add this to theme function code

This worked for me on all posts (Not pages)– added into theme functions.php file:

wordpress variables

add_action(‘wp’, ‘last_if_modified_headers’ );

function last_if_modified_headers() {
global $post;
if(isset($post) && is_single()){
$LastModified_unix = strtotime($post->post_modified);
$LastModified = gmdate(“D, d M Y H:i:s \G\M\T”, $LastModified_unix);
$IfModifiedSince = false;

if (isset($_ENV[‘HTTP_IF_MODIFIED_SINCE’])) {
$IfModifiedSince = strtotime(substr($_ENV[‘HTTP_IF_MODIFIED_SINCE’], 5));
}
if (isset($_SERVER[‘HTTP_IF_MODIFIED_SINCE’])) {
$IfModifiedSince = strtotime(substr($_SERVER[‘HTTP_IF_MODIFIED_SINCE’], 5));
}

if ($IfModifiedSince && $IfModifiedSince >= $LastModified_unix) {
header($_SERVER[‘SERVER_PROTOCOL’] . ‘ 304 Not Modified’);
exit;
}

header(‘Last-Modified: ‘. $LastModified);
}
}

 

use this plugin  (how to create a WordPress plugin)

last-modified-and-if-modified-since-headers

 

copy the following text to the header.php file:

header(“Last-Modified: ” . date(‘r’,strtotime($post->post_modified)));

header("Last-Modified: " . date('r',strtotime($post->post_modified)));

 

However, this code applies for posts and pages only. It’s useless for the main page, archives, recent comments, and taxonomy.

<?php
$LastModified_unix = 1294844676;
$Last Modified = gmdate(«D, d M Y H:i:s \G\M\T», $LastModified_unix);
$IfModifiedSince = false;
if (isset($_ENV[‘HTTP_IF_MODIFIED_SINCE’]))
  $IfModifiedSince = strtotime(substr($_ENV[‘HTTP_IF_MODIFIED_SINCE’], 5));
if (isset($_SERVER[‘HTTP_IF_MODIFIED_SINCE’]))
  $IfModifiedSince = strtotime(substr($_SERVER[‘HTTP_IF_MODIFIED_SINCE’], 5));
If ($IfModifiedSince && $IfModifiedSince >= &LastModified_unix) {
  header ($_SERVER[‘SERVER_PROTOCOL’] . ‘ 304 Not Modified’);
  exit;
}
header(‘Last-Modified: ‘ . $LastModified);
?>


global $wpdb;

$wp_last_modified_date = $wpdb->get_var(“SELECT GREATEST(post_modified_gmt, post_date_gmt) d FROM $wpdb->posts WHERE post_status = ‘publish’ ORDER BY d DESC LIMIT 1”);
$wp_last_modified_date = max($wp_last_modified_date, get_lastcommentmodified(‘GMT’));

$last_modified = mysql2date(‘D, d M Y H:i:s’, $wp_last_modified_date, 0) . ‘ GMT’;

 

//set last-modified header
header( “Last-Modified: “.$last_modified);

Primary Sidebar

nginx server tutorials (installation, configuration, performance tuning, security)

  • Letsencrypt SSL Installation on apache/Nginx ubuntu / debian wordpress
  • fix error 520 522 524 on cloudflare wordpress godaddy nginx etc
  • nginx fastcgi cache wordpress how to configure
  • install LEMP Stack on 22.04 LTS Nginx MySQL PHP fpm #wordpress #digital ocean
  • Apache vs nginx (connection handling, modules, memory usage)
  • Pagespeed module install, configure, monitor, errors ft nginx &apache
  • nginx errors (504,502, 413, unable to start, syntax errors)
  • nginx conf explained best config file performance tuning tips nginx.conf location errors tutorial
  • use nginx as reverse proxy and load balancer for apache wordpress
  • nginx rewrite rules with examples 301 redirection
  • nginx modules list (enable, disable, upgrade, install dynamic module)
  • php fpm pool manager configuration settings based on server spike high cpu wordpress
  • php fpm restart nginx ubuntu enable status page, monitor etc
  • what is TTFB & how to Reduce it (server response time) Google pagespeed
  • letsencrypt install configure on ubuntu / debian nginx
  • Top 10 tips to improve nginx server security
  • nginx performance tuning connections, buffers file descriptors
  • enable brotli compression nginx brotli vs gzip
  • nginx installation on ubuntu 20.04 LTS
  • monitor nginx request with nginx status amplify datadog new relic
  • SSL faster reduce TLS hand shake improve https performance
  • nginx rate limiting explained by location time specific url
  • datadog nginx integration installation process
  • newrelic nginx integration process and errors fix and metrics
  • php fpm seems busy fixed warning and max children reached to handle max connections / requests
  • Php fpm configuration for 1000 concurrent connections server busy max children reached
  • php fpm ondemand vs dynamic vs Static (the dynamic pool problem)
  • nginx upstream response is buffered to a temporary file
  • php fpm install ubuntu 20.04 nginx
  • install phpmyadmin ubuntu nginx 22.04
  • upgrade php fpm ubuntu nginx 7.4 to 8.2
  • nginx add last modified header (remove, php wordpress)
  • php fpm dynamic pool manager settings
  • nginx fastcgi cache purge
  • nginx open file limit connections ulimits sysctl
  • php fpm high cpu usage WordPress 4 solutions nay work for you
  • nginx buffer size for wordpress
  • Cloudflare error code 524 nginx a timeout error occured
  • server configuration for 1000 concurrent users

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy