
我不知道這是一個錯誤還是我錯誤地定義了緩存,我有一個 nginx 緩存(反向代理)伺服器,由 2 個用戶使用不同的網域(mydomain1.com
和mydomain2.com
),內容不同,不是別名網域。
我定義了一個緩存,然後在nginx 中定義了兩個網域/虛擬主機並將其設定為使用該緩存,兩個網域從同一來源伺服器取得數據,但它們將不同的虛擬主機傳遞到來源伺服器,以便取得正確的內容。
問題是nginx似乎混合了兩台伺服器上的緩存,訪問一個主頁會顯示另一台伺服器的快取主頁。
我需要為每個虛擬主機定義單獨的快取嗎?
NGINX 不應該也將一個虛擬主機與每個請求關聯起來嗎?它似乎只是關聯來源伺服器、連接埠等。
這是快取主頁的頭部,顯示兩個虛擬主機之間沒有任何區別:
KEY: http://source.example.com:81/
HTTP/1.1 200 OK
Date: Sun, 02 Feb 2020 00:54:33 GMT
Server: Apache/2.4.6 (CentOS) mpm-itk/2.4.7-04 OpenSSL/1.0.2k-fips PHP/5.4.16
X-Powered-By: PHP/5.4.16
Expires: Sun, 02 Feb 2020 02:54:33 GMT
Cache-Control: public, max-age=7200
X-Mod-Pagespeed: 1.13.35.2-0
Vary: Accept-Encoding
我的 NGINX 快取設定:
proxy_cache_path /ramdisk/nginx_cache levels=1:2 keys_zone=nginx_ramdisk_cache:512m max_size=3g
inactive=30d use_temp_path=off;
# defining domain 1
server {
[...]
server_name mydomain1.com;
location / {
proxy_pass http://source.example.com:81;
proxy_set_header Host mydomain1.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache nginx_ramdisk_cache;
}
}
# defining domain 2
server {
[...]
server_name mydomain2.com;
location / {
proxy_pass http://source.example.com:81;
proxy_set_header Host mydomain2.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache nginx_ramdisk_cache;
}
}
在來源伺服器(apache)上,我定義了 2 個虛擬主機,如下所示:
<VirtualHost *:81>
ServerName mydomain1.com
DocumentRoot /var/www/html/mydomain1.com
</VirtualHost>
<VirtualHost *:81>
ServerName mydomain2.com
DocumentRoot /var/www/html/mydomain2.com
</VirtualHost>
答案1
像個怪人一樣回答我自己的問題;
為了讓 nginx 區分兩個 url 使用的相同路徑(例如/
),它需要使用proxy_cache_key
,即預設似乎$scheme$proxy_host$request_uri
,這在我的情況下不起作用,當兩個 nginx 網域從同一來源伺服器獲取資料(透過發送不同的主機頭)時,它們會兩個都最終使用這樣的密鑰:
KEY: http://source.example.com:81
(我理解 $proxy_host = 來源伺服器)
解決方案:指定aproxy_cache_key
包括當前$host
, 不$proxy_host
;
proxy_cache_key $scheme://$host$request_uri;