Oli*_*nce 7 nginx cache reverse-proxy
我在单个 IP 地址上运行 Proxmox 服务器,它将根据请求的主机向容器发送 HTTP 请求。
我在 Proxmox 端使用 nginx 来监听 HTTP 请求,我proxy_pass
在不同server
块中使用指令根据server_name
.
我的容器在 Ubuntu 上运行,同时也在运行一个 nginx 实例。
我在完全静态的特定网站上缓存时遇到问题:在文件更新后,nginx 继续为我提供陈旧的内容,直到我:
proxy_cache off
为此服务器设置并重新加载配置这是我的配置的详细信息:
在服务器上(proxmox):
/etc/nginx/nginx.conf:
user www-data;
worker_processes 8;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
use epoll;
}
http {
##
# Basic Settings
##
sendfile on;
#tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_body_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 1 1K;
ignore_invalid_headers on;
client_body_timeout 5;
client_header_timeout 5;
keepalive_timeout 5 5;
send_timeout 5;
server_name_in_redirect off;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
# gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
limit_conn_zone $binary_remote_addr zone=gulag:1m;
limit_conn gulag 50;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)
/etc/nginx/conf.d/proxy.conf:
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_hide_header X-Powered-By;
proxy_intercept_errors on;
proxy_buffering on;
proxy_cache_key "$scheme://$host$request_uri";
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m inactive=7d max_size=700m;
Run Code Online (Sandbox Code Playgroud)
/etc/nginx/sites-available/my-domain.conf:
server {
listen 80;
server_name .my-domain.com;
access_log off;
location / {
proxy_pass http://my-domain.local:80/;
proxy_cache cache;
proxy_cache_valid 12h;
expires 30d;
proxy_cache_use_stale error timeout invalid_header updating;
}
}
Run Code Online (Sandbox Code Playgroud)
在容器 (my-domain.local) 上:
nginx.conf:( 一切都在主配置文件中——它已经完成了……)
user www-data;
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip off;
server {
listen 80;
server_name .my-domain.com;
root /var/www;
access_log logs/host.access.log;
}
}
Run Code Online (Sandbox Code Playgroud)
在决定发布我自己的问题之前,我已经阅读了许多博客文章和答案......我能看到的大多数答案都建议设置,sendfile off;
但这对我不起作用。我尝试了很多其他的东西,仔细检查了我的设置,一切似乎都很好。
所以我想知道我是否不希望 nginx 的缓存做一些它不应该做的事情......?
基本上,我认为如果我的容器中的一个静态文件被更新,我的反向代理中的缓存将失效,我的浏览器将在它请求时获得该文件的新版本......
但我现在有一种情绪,我误解了很多事情。
在所有事情中,我现在想知道服务器上的 nginx 如何知道容器中的文件已更改?我已经看到了一个指令proxy_header_pass
(或类似的东西),我应该使用它让容器中的 nginx 实例以某种方式通知 Proxmox 中的一个关于更新文件的信息吗?
这种期望只是一个梦想,还是可以在我当前的架构上使用 nginx 来实现?
小智 5
你想做的事情是不可能的。当一个 URL 被代理缓存时,在缓存过期之前不会再向后端查询这个 URL。代理无法知道文件已在后端更新。
一些高级缓存(如 Varnish)能够通过标头或 PURGE 请求处理失效需求。例如,当用户编辑 mediawiki 页面时,对 POST 请求的响应包含使文章的缓存条目无效的标头。但是此过程仅在使用 URL 调用修改资源时才有效:更改后端的静态文件不会通知代理缓存。
可以通过 lua 模块在 nginx 上实现这种类型的失效:http : //syshero.org/post/68479556365/nginx-passive-cache-invalidation。