我想以這種方式處理 nginx 中的請求,所以當爬蟲來獲取請求的頁面時,我需要通過請求的 URL 發送靜態版本的單頁應用程式,否則動態生成index.html。靜態頁面和動態頁面位于不同的檔案夾中(必需),因此我$src在這種情況下使用變數。
我的配置:
server {
listen 443 ssl http2;
server_name www.example.com;
set $src "/www";
set $crawler 0;
if ($http_user_agent ~* "examplebot|anotherbot|crawlerbot") {
set $crawler 1;
set $src "/www-static";
}
root "/var${src}";
location ~* ^/(en|us|uk)/products/ {
if ($crawler = 1) {
rewrite ^/(\w\w)/products/(\w )/(\w ) /pages/$1/$2/$3.html break;
}
}
location / {
try_files $uri /index.html;
}
}
使用上面的配置,當我訪問我的網站時,我會收到404錯誤,即使是通過普通的用戶代理(不是爬蟲的)。
爬蟲請求網址:https://www.example.com/en/products/dairy/cheese
靜態頁面位置:/var/www-static/pages/en/dairy/cheese.html
我的期望是,當爬蟲請求 eg 時https://www.example.com/en/products/dairy/cheese,它應該獲取靜態頁面,而當普通用戶請求任何頁面時,他應該獲取位于根檔案夾中的index.html 。
我知道ifnginx 中的陳述句不是世界上最好的東西,但是有沒有什么解決方案可以實作我想要的,不管有沒有iforrewrite陳述句,謝謝?
我想指出,在沒有第一個塊的情況下“重定向”到具有$src變數的所需檔案夾沒有問題。location所以,我認為問題與$src變數及其邏輯無關。
uj5u.com熱心網友回復:
向您發送請求的邏輯index.html是try_files陳述句。要為塊處理的 URL 復制該行為location ~* ^/(en|us|uk)/products/,您還需要try_files向該塊添加一條陳述句。
例如:
location ~* ^/(en|us|uk)/products/ {
if ($crawler = 1) { ... }
try_files $uri /index.html;
}
location / {
try_files $uri /index.html;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/515784.html
