Score:0

nginx: Location block for all URIs without extension

cn flag

I know how to process URIs that match a certain pattern.

Example

The following configuration will add an Cache-Control http-header to all files ending with .css or .js:

location ~* \.(css|js)$ {
  add_header Cache-Control "public, max-age=31536000, immutable" always;
}

Question

How can I process all URIs without extension?

Something like www.domain.tld/my-article.

(I use nginx as an reverse-proxy and I add the extension .html in the .htaccess.)

Richard Smith avatar
jp flag
So the final path element does not contain a `.`, for example: `location ~ /[^./]+$`
Score:2
us flag

The usual way is:

location / {
    # Everything else
}

location ~* \.(?:css|js)$ {
    add_header Cache-Control "public, max-age=31536000, immutable" always;
}

nginx location directive documentation explains in detail how nginx evaluates different location blocks.

Sr. Schneider avatar
cn flag
Is in the example above `\.(?:css|js)$` the same as `\.(css|js)$`?
us flag
The `?:` tells regular expression engine not to capture the part to variable. This makes the expression a bit faster.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.