#652·Nginx

nginx seemingly ignoring If-None-Match in the presence of If-Modified-Since in violation of specs

Author: vlofgrenCreated Apr 25, 2025Updated Sep 30, 2026
Labelsbug

Environment

I'm coming at this from the angle of a search engine operator, and have seen the behavior in the wild at scale, though I've reproduced locally for this issue on Arch linux (but also seen on a Debian server I operate, and many others):

nginx version: nginx/1.28.0 Linux blackbox 6.12.23-1-lts #1 SMP PREEMPT_DYNAMIC Thu, 10 Apr 2025 13:28:36 +0000 x86_64 GNU/Linux

Description

I've observed nginx servers in general do not seem to deal well with If-Modified-Since, specifically, it lets If-Modified-Since override the behavior of If-None-Match, which violates the HTTP1.1 specs.

The specifications say

A recipient MUST ignore If-Modified-Since if the request contains an If-None-Match header field; the condition in If-None-Match is considered to be a more accurate replacement for the condition in If-Modified-Since, and the two are only combined for the sake of interoperating with older intermediaries that might not implement If-None-Match.

Let's trigger this incorrect behavior:

$ curl -v localhost
...
< HTTP/1.1 200 OK
< Server: nginx/1.28.0
< Date: Fri, 25 Apr 2025 11:44:41 GMT
< Content-Type: text/html
< Content-Length: 615
< Last-Modified: Wed, 23 Apr 2025 19:23:44 GMT
< Connection: keep-alive
< ETag: "68093e40-267"
< Accept-Ranges: bytes

Pass the ETag as If-None-Match and get a 304 -- so far so good.

$ curl -v localhost -H "If-None-Match: \"68093e40-267\"
< HTTP/1.1 304 Not Modified

Rewind the Date header a bit to get a date after Last-Modified but before now. ... and we get a 200. This is technically fine -- a server isn't obliged to understand this header, though I'd somewhat expect it to given we're just serving a static file:

$ curl -v localhost -H "If-Modified-Since: Fri, 25 Apr 2025 10:44:41 GMT"
< HTTP/1.1 200 OK

It's when we combine the two, we get behavior violating HTTP/1.1 specs, ignoring If-None-Match in seeming favor of If-Modified-Since:

$ curl -v localhost -H "If-None-Match: \"68093e40-267\"" -H "If-Modified-Since: Fri, 25 Apr 2025 10:44:41 GMT"
< HTTP/1.1 200 OK

nginx configuration


worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

    }
}