我在一个IP地址上运行Proxmox服务器,根据请求的主机将HTTP请求分发到容器。
我在Proxmox端使用nginx来侦听HTTP请求,并且在我的不同server块中使用proxy_pass指令来根据server_name分派请求。
我的容器在Ubuntu上运行,也运行一个nginx实例。
我在一个完全静态的特定网站上caching有麻烦:在文件更新之后,nginx会继续为我提供陈旧的内容,直到我:
proxy_cache off此服务器的proxy_cache off并重新加载configuration 以下是我的configuration的详细信息:
在服务器(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/*; }
/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;
/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; } }
在容器(my-domain.local)上:
nginx.conf 🙁一切都在主configuration文件里面 – 很快完成了)
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; } }
我已经阅读了许多博客文章和答案,然后才解决发布我自己的问题…大多数的答案我可以看到build议设置sendfile off; 但是这对我不起作用 。 我已经尝试了很多其他的东西,双重检查我的设置,一切似乎都很好。
所以我想知道我是否不期待nginx的caching做一些不该做的事情?
基本上,我认为,如果我的容器中的一个静态文件被更新,我的反向代理中的caching将失效,我的浏览器将得到该文件的新版本,当它请求它…
但是我现在有这种感觉,我误解了很多东西。
所有的事情,我现在想知道服务器上的nginx如何知道容器中的文件已经改变? 我看到了一个指令proxy_header_pass (或类似的),我应该用这个来让容器中的nginx实例以某种方式通知Proxmox中的更新文件?
这个期望只是一个梦,或者我可以用我当前的架构上的nginx来实现吗?
你想做什么是不可能的。 当代理对URL进行caching时,直到caching过期才会对此URL的后端进行进一步查询。 代理没有办法知道该文件已在后端更新。
一些像Varnish的高级caching能够通过头文件或PURGE请求处理失效请求。 例如,当用户编辑mediawiki页面时,对POST请求的响应包含一个标题,该标题使文章的caching项无效。 但是,只有在使用URL调用修改资源时,此过程才有效:更改后端的静态文件不会通知代理caching。
可以通过lua模块在nginx上实现这种types的失效: http : //syshero.org/post/68479556365/nginx-passive-cache-invalidation 。
您可以通过使用独立caching(例如memcached或redis)来实现此function。 nginx有模块来处理它们。 你将需要一些关于caching键的约定(例如,从用户cookie和url构造键)。 在这种情况下,您可以控制后端的caching项目,包括无效和更新。