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;
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
add a comment |
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
add a comment |
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
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
nginx reverse-proxy cache
edited Apr 5 '18 at 18:39
Chris Stryczynski
asked Apr 5 '18 at 16:51
Chris StryczynskiChris Stryczynski
1631213
1631213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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