How can I force to cache requests that have “Cache-Control: no-cache” in nginx when using a reverse proxy and it responds with 404?I want static content cached, but browsers keep requesting itHTTP Caching Server that supports POSTNginx cache reverse proxy: how to keep app server alive during the 5 second window when cache expires?Cache Control Headers with IIS 7.5Nginx: using X-Accel-Expires with Cache-ControlControl files fetched from upstream when using nginx proxy cacheFastCGI cache is always a MISSIs this an nginx proxy_cache bug?How can I ensure that Nginx will never cache certain error statuses?Nginx web-cache tweeking

Did Milano or Benatar approve or comment on their namesake MCU ships?

Are there downsides to using std::string as a buffer?

SQL counting distinct over partition

Pre-1972 sci-fi short story or novel: alien(?) tunnel where people try new moves and get destroyed if they're not the correct ones

Prime Sieve and brute force

System.StringException: Unexpected end of expression

How to return a security deposit to a tenant

What is the actual quality of machine translations?

Second (easy access) account in case my bank screws up

How can I tell the difference between unmarked sugar and stevia?

Is using haveibeenpwned to validate password strength rational?

What can I, as a user, do about offensive reviews in App Store?

How to handle self harm scars on the arm in work environment?

What makes an item an artifact?

Someone whose aspirations exceed abilities or means

1980s live-action movie where individually-coloured nations on clouds fight

Generate a Graeco-Latin square

Find the limit of a multiplying term function when n tends to infinity.

How can I get an unreasonable manager to approve time off?

Difference between > and >> when used with a named pipe

Is it legal for a bar bouncer to conficaste a fake ID

Grover algorithm for a database search: where is the quantum advantage?

Determining fair price for profitable mobile app business

Should an arbiter claim draw at a K+R vs K+R endgame?



How can I force to cache requests that have “Cache-Control: no-cache” in nginx when using a reverse proxy and it responds with 404?


I want static content cached, but browsers keep requesting itHTTP Caching Server that supports POSTNginx cache reverse proxy: how to keep app server alive during the 5 second window when cache expires?Cache Control Headers with IIS 7.5Nginx: using X-Accel-Expires with Cache-ControlControl files fetched from upstream when using nginx proxy cacheFastCGI cache is always a MISSIs this an nginx proxy_cache bug?How can I ensure that Nginx will never cache certain error statuses?Nginx web-cache tweeking






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















The upstream server is sending a 404 with a header of Cache-Control: no-cache.



Due to this, nginx is not caching the request. How can I force it to cache the request?




This caching is only required on the upstream repsponses with a 404 status code.



It can keep the original header and pass this on to the client, but I don't want it sending a 'fresh' request to the page.



I also don't want to remove the header entirely as some of the requests would have a appropriate 'cache control' header set to expire in for example 24h. Hence I can't use proxy_ignore_headers.










share|improve this question






























    0















    The upstream server is sending a 404 with a header of Cache-Control: no-cache.



    Due to this, nginx is not caching the request. How can I force it to cache the request?




    This caching is only required on the upstream repsponses with a 404 status code.



    It can keep the original header and pass this on to the client, but I don't want it sending a 'fresh' request to the page.



    I also don't want to remove the header entirely as some of the requests would have a appropriate 'cache control' header set to expire in for example 24h. Hence I can't use proxy_ignore_headers.










    share|improve this question


























      0












      0








      0








      The upstream server is sending a 404 with a header of Cache-Control: no-cache.



      Due to this, nginx is not caching the request. How can I force it to cache the request?




      This caching is only required on the upstream repsponses with a 404 status code.



      It can keep the original header and pass this on to the client, but I don't want it sending a 'fresh' request to the page.



      I also don't want to remove the header entirely as some of the requests would have a appropriate 'cache control' header set to expire in for example 24h. Hence I can't use proxy_ignore_headers.










      share|improve this question
















      The upstream server is sending a 404 with a header of Cache-Control: no-cache.



      Due to this, nginx is not caching the request. How can I force it to cache the request?




      This caching is only required on the upstream repsponses with a 404 status code.



      It can keep the original header and pass this on to the client, but I don't want it sending a 'fresh' request to the page.



      I also don't want to remove the header entirely as some of the requests would have a appropriate 'cache control' header set to expire in for example 24h. Hence I can't use proxy_ignore_headers.







      nginx reverse-proxy cache






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 5 '18 at 18:39







      Chris Stryczynski

















      asked Apr 5 '18 at 16:51









      Chris StryczynskiChris Stryczynski

      1631213




      1631213




















          1 Answer
          1






          active

          oldest

          votes


















          0














          I've been lucky in that I can do this based on the response code (404) and by intercepting the error:



          #user www-data;
          #worker_processes 4;
          #pid /run/nginx.pid;
          #
          #events
          # worker_connections 768;
          # # multi_accept on;
          #
          user www-data;
          worker_processes 4;

          error_log /var/log/nginx/error.log warn;
          pid /var/run/nginx.pid;


          events
          worker_connections 1024;



          http
          include /etc/nginx/mime.types;
          default_type application/octet-stream;

          log_format main '$remote_addr - $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for"';

          access_log /var/log/nginx/access.log main;
          #access_log /dev/null;
          #error_log /dev/null;

          sendfile off;
          #tcp_nopush on;

          keepalive_timeout 65;

          #gzip on;
          proxy_cache_path /tmp/nginx levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;

          server
          listen 80 default_server;
          listen [::]:80 default_server;
          server_name _;
          return 301 https://$host$request_uri;


          server
          listen 443 default_server ssl;
          listen [::]:443 default_server ssl;

          server_name localhost;

          ssl on;
          ssl_certificate /opt/bitnami/nginxssl/nginxssl.crt;
          ssl_certificate_key /opt/bitnami/nginxssl/selfbuild.key;
          recursive_error_pages on;

          location /
          proxy_pass https://localhost:9443/;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;
          # Let the Set-Cookie header through.

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;
          error_page 404 /404.html;
          proxy_intercept_errors on;



          location /404.html

          proxy_pass https://localhost:9443/404.html;

          proxy_hide_header Set-Cookie;
          proxy_hide_header Cache-Control;
          proxy_hide_header Expires;
          proxy_hide_header Pragma;
          proxy_hide_header X-Accel-Expires;
          proxy_ignore_headers "Set-Cookie" "Cache-Control" "Expires" "X-Accel-Expires";

          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;









          share|improve this answer























          • I did actually find this answer somewhere else - but I can't remember where and my history crazy long right now haha.

            – Chris Stryczynski
            Apr 5 '18 at 18:40











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "2"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f906214%2fhow-can-i-force-to-cache-requests-that-have-cache-control-no-cache-in-nginx-w%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I've been lucky in that I can do this based on the response code (404) and by intercepting the error:



          #user www-data;
          #worker_processes 4;
          #pid /run/nginx.pid;
          #
          #events
          # worker_connections 768;
          # # multi_accept on;
          #
          user www-data;
          worker_processes 4;

          error_log /var/log/nginx/error.log warn;
          pid /var/run/nginx.pid;


          events
          worker_connections 1024;



          http
          include /etc/nginx/mime.types;
          default_type application/octet-stream;

          log_format main '$remote_addr - $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for"';

          access_log /var/log/nginx/access.log main;
          #access_log /dev/null;
          #error_log /dev/null;

          sendfile off;
          #tcp_nopush on;

          keepalive_timeout 65;

          #gzip on;
          proxy_cache_path /tmp/nginx levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;

          server
          listen 80 default_server;
          listen [::]:80 default_server;
          server_name _;
          return 301 https://$host$request_uri;


          server
          listen 443 default_server ssl;
          listen [::]:443 default_server ssl;

          server_name localhost;

          ssl on;
          ssl_certificate /opt/bitnami/nginxssl/nginxssl.crt;
          ssl_certificate_key /opt/bitnami/nginxssl/selfbuild.key;
          recursive_error_pages on;

          location /
          proxy_pass https://localhost:9443/;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;
          # Let the Set-Cookie header through.

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;
          error_page 404 /404.html;
          proxy_intercept_errors on;



          location /404.html

          proxy_pass https://localhost:9443/404.html;

          proxy_hide_header Set-Cookie;
          proxy_hide_header Cache-Control;
          proxy_hide_header Expires;
          proxy_hide_header Pragma;
          proxy_hide_header X-Accel-Expires;
          proxy_ignore_headers "Set-Cookie" "Cache-Control" "Expires" "X-Accel-Expires";

          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;









          share|improve this answer























          • I did actually find this answer somewhere else - but I can't remember where and my history crazy long right now haha.

            – Chris Stryczynski
            Apr 5 '18 at 18:40















          0














          I've been lucky in that I can do this based on the response code (404) and by intercepting the error:



          #user www-data;
          #worker_processes 4;
          #pid /run/nginx.pid;
          #
          #events
          # worker_connections 768;
          # # multi_accept on;
          #
          user www-data;
          worker_processes 4;

          error_log /var/log/nginx/error.log warn;
          pid /var/run/nginx.pid;


          events
          worker_connections 1024;



          http
          include /etc/nginx/mime.types;
          default_type application/octet-stream;

          log_format main '$remote_addr - $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for"';

          access_log /var/log/nginx/access.log main;
          #access_log /dev/null;
          #error_log /dev/null;

          sendfile off;
          #tcp_nopush on;

          keepalive_timeout 65;

          #gzip on;
          proxy_cache_path /tmp/nginx levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;

          server
          listen 80 default_server;
          listen [::]:80 default_server;
          server_name _;
          return 301 https://$host$request_uri;


          server
          listen 443 default_server ssl;
          listen [::]:443 default_server ssl;

          server_name localhost;

          ssl on;
          ssl_certificate /opt/bitnami/nginxssl/nginxssl.crt;
          ssl_certificate_key /opt/bitnami/nginxssl/selfbuild.key;
          recursive_error_pages on;

          location /
          proxy_pass https://localhost:9443/;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;
          # Let the Set-Cookie header through.

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;
          error_page 404 /404.html;
          proxy_intercept_errors on;



          location /404.html

          proxy_pass https://localhost:9443/404.html;

          proxy_hide_header Set-Cookie;
          proxy_hide_header Cache-Control;
          proxy_hide_header Expires;
          proxy_hide_header Pragma;
          proxy_hide_header X-Accel-Expires;
          proxy_ignore_headers "Set-Cookie" "Cache-Control" "Expires" "X-Accel-Expires";

          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;









          share|improve this answer























          • I did actually find this answer somewhere else - but I can't remember where and my history crazy long right now haha.

            – Chris Stryczynski
            Apr 5 '18 at 18:40













          0












          0








          0







          I've been lucky in that I can do this based on the response code (404) and by intercepting the error:



          #user www-data;
          #worker_processes 4;
          #pid /run/nginx.pid;
          #
          #events
          # worker_connections 768;
          # # multi_accept on;
          #
          user www-data;
          worker_processes 4;

          error_log /var/log/nginx/error.log warn;
          pid /var/run/nginx.pid;


          events
          worker_connections 1024;



          http
          include /etc/nginx/mime.types;
          default_type application/octet-stream;

          log_format main '$remote_addr - $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for"';

          access_log /var/log/nginx/access.log main;
          #access_log /dev/null;
          #error_log /dev/null;

          sendfile off;
          #tcp_nopush on;

          keepalive_timeout 65;

          #gzip on;
          proxy_cache_path /tmp/nginx levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;

          server
          listen 80 default_server;
          listen [::]:80 default_server;
          server_name _;
          return 301 https://$host$request_uri;


          server
          listen 443 default_server ssl;
          listen [::]:443 default_server ssl;

          server_name localhost;

          ssl on;
          ssl_certificate /opt/bitnami/nginxssl/nginxssl.crt;
          ssl_certificate_key /opt/bitnami/nginxssl/selfbuild.key;
          recursive_error_pages on;

          location /
          proxy_pass https://localhost:9443/;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;
          # Let the Set-Cookie header through.

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;
          error_page 404 /404.html;
          proxy_intercept_errors on;



          location /404.html

          proxy_pass https://localhost:9443/404.html;

          proxy_hide_header Set-Cookie;
          proxy_hide_header Cache-Control;
          proxy_hide_header Expires;
          proxy_hide_header Pragma;
          proxy_hide_header X-Accel-Expires;
          proxy_ignore_headers "Set-Cookie" "Cache-Control" "Expires" "X-Accel-Expires";

          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;









          share|improve this answer













          I've been lucky in that I can do this based on the response code (404) and by intercepting the error:



          #user www-data;
          #worker_processes 4;
          #pid /run/nginx.pid;
          #
          #events
          # worker_connections 768;
          # # multi_accept on;
          #
          user www-data;
          worker_processes 4;

          error_log /var/log/nginx/error.log warn;
          pid /var/run/nginx.pid;


          events
          worker_connections 1024;



          http
          include /etc/nginx/mime.types;
          default_type application/octet-stream;

          log_format main '$remote_addr - $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for"';

          access_log /var/log/nginx/access.log main;
          #access_log /dev/null;
          #error_log /dev/null;

          sendfile off;
          #tcp_nopush on;

          keepalive_timeout 65;

          #gzip on;
          proxy_cache_path /tmp/nginx levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;

          server
          listen 80 default_server;
          listen [::]:80 default_server;
          server_name _;
          return 301 https://$host$request_uri;


          server
          listen 443 default_server ssl;
          listen [::]:443 default_server ssl;

          server_name localhost;

          ssl on;
          ssl_certificate /opt/bitnami/nginxssl/nginxssl.crt;
          ssl_certificate_key /opt/bitnami/nginxssl/selfbuild.key;
          recursive_error_pages on;

          location /
          proxy_pass https://localhost:9443/;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;
          # Let the Set-Cookie header through.

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;
          error_page 404 /404.html;
          proxy_intercept_errors on;



          location /404.html

          proxy_pass https://localhost:9443/404.html;

          proxy_hide_header Set-Cookie;
          proxy_hide_header Cache-Control;
          proxy_hide_header Expires;
          proxy_hide_header Pragma;
          proxy_hide_header X-Accel-Expires;
          proxy_ignore_headers "Set-Cookie" "Cache-Control" "Expires" "X-Accel-Expires";

          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Proto $scheme;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          proxy_cache STATIC;

          add_header X-Proxy-Cache $upstream_cache_status;
          proxy_cache_valid 404 60m;










          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 5 '18 at 18:38









          Chris StryczynskiChris Stryczynski

          1631213




          1631213












          • I did actually find this answer somewhere else - but I can't remember where and my history crazy long right now haha.

            – Chris Stryczynski
            Apr 5 '18 at 18:40

















          • I did actually find this answer somewhere else - but I can't remember where and my history crazy long right now haha.

            – Chris Stryczynski
            Apr 5 '18 at 18:40
















          I did actually find this answer somewhere else - but I can't remember where and my history crazy long right now haha.

          – Chris Stryczynski
          Apr 5 '18 at 18:40





          I did actually find this answer somewhere else - but I can't remember where and my history crazy long right now haha.

          – Chris Stryczynski
          Apr 5 '18 at 18:40

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Server Fault!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f906214%2fhow-can-i-force-to-cache-requests-that-have-cache-control-no-cache-in-nginx-w%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

          Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

          What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company