Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Adam Miedema
Adam Miedema

Posted on • Edited on • Originally published atcleavr.io

     

How to skip NGINX cache server wide

InCleavr.io, you can enable NGINX cache or FastCGI (for PHP-apps) for any site as well as configure cache settings for each site.

configure site nginx cache

The great thing about caching your sites is that your server can serve a page, or elements within a page, directly from a saved cache store as opposed to having to re-generate the whole page, or element, every time a request is made to view it.

This greatly increases the performance of your sites for your visitors since the server is able serve content quicker. However, you don't necessarily want everything to be cached and served to visitors directly from the cache store. This is especially true for more dynamic content, personalized content, and cookies.

If you find yourself adding the same cache settings for all of your sites, then you may consider configuring cache settings server-wide to avoid repetitive work and maintenance.

Here is how you can make that happen!

Step 1: SSH into your server

Cleavr.io doesn't have a server-wide NGINX Cache config section currently. But, Cleavr.io offers the tools to help get this done easily and securely.

The first step is to SSH into your server. If you have never SSH'd into your server before,then check out this guide to learn how to SSH into your Cleavr-managed servers.

Once you have SSH'd into your server, using the terminal, navigate to the NGINXconf.d directory.

cd /etc/nginx/conf.d
Enter fullscreen modeExit fullscreen mode

Step 2: Add cache config files

NGINX will automatically pick up and files in theconf.d directory that end with.conf. So, all we need to do is add a new file using the.conf extension.

You can add multiple config files and give them a descriptive name so you know what the config file is for. In fact, doing it this way may help you keep things more organized.

Let's add a couple of examples.

First, let's add a new file to exclude a path from being cached.

vim exclude-path-name-from-cache.conf
Enter fullscreen modeExit fullscreen mode

This will create a new file and then allow you to edit the file via vim. New to vim?Check out this guide on how to get started using vim.

For the contents of the configuration file, add the following:

if ($request_uri ~* "(/enter-path-here/)") {    set $skip_cache 1;}
Enter fullscreen modeExit fullscreen mode

Swapenter-path-here with the path that you wish to exclude caching for.

Save the file.

Second, let's see how we can skip a cookie from being cached.

vim exclude-cookie-from-cache.conf
Enter fullscreen modeExit fullscreen mode

Then, we can add something like the following to the new file:

if ($http_cookie ~* "enter-cookie-name") {    set $skip_cache 1;}
Enter fullscreen modeExit fullscreen mode

Swapenter-cookie-name with the name of the cookie you wish to exlude from cache.

Save the file.

Step 3: Restart NGINX

We're done with the terminal, so we can close that and go back toCleavr.io.

Locate the server that the cache configs were created on, go to the Services section, and then restart NGINX.

restart nginx in cleavr

That's it! NGINX will see the config files and will now refer to the new cache configs for all server caching operations.


I hope you find this guide useful! Please check out our other guides and blog posts in theCleavr Slice.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Keep moving forward.
  • Joined

More fromAdam Miedema

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp