示例 nginx.conf,我想從位置指令中移動條件重寫,但不知道如何:
# https://user-agents.net/browsers
map $http_user_agent $outdated {
default 0;
...
}
server {
...
location ~ (not-supported) {
# empty
}
location / {
if ($outdated = 1) {
rewrite ^ /not-supported/index.html break;
}
try_files $uri $uri/ /index.html =404;
}
...
}
uj5u.com熱心網友回復:
您可以在 the或 at背景關系中使用rewrite指令。不同之處在于將處理該指令的請求處理階段(對于最火的情況,對于第二種情況)。盡可能避免使用正則運算式。當您在背景關系中使用指令時,您應該對指令使用標志而不是使用標志來強制重新搜索重寫 URI 的位置。我還建議將其設定為內部以防止直接訪問目錄。所以使用serverlocationrewriteNGX_HTTP_SERVER_REWRITE_PHASENGX_HTTP_REWRITE_PHASErewritelocationlastrewritebreak/not-supported/
location /not-supported/ {
internal;
}
location / {
if ($outdated = 1) {
rewrite ^ /not-supported/index.html last;
}
try_files $uri $uri/ /index.html;
}
或者
if ($outdated = 1) {
rewrite ^ /not-supported/index.html;
}
location /not-supported/ {
internal;
}
location / {
try_files $uri $uri/ /index.html;
}
您可以在您的 web 根目錄下或其他任何地方擁有該non-supported目錄和檔案(對于最后一種情況,您需要為. 確保重寫規則不會干擾背景關系中可能存在的任何其他規則。index.htmllocation /not-supported/ { ... }server
uj5u.com熱心網友回復:
通過以下方式使其作業:
http {
# https://user-agents.net/browsers
map $http_user_agent $outdated {
default 0;
"~Opera" 1; # Opera all
"~MSIE" 1; # MSIE all
"~Trident/[1-7]\." 1; # MSIE all
"~Chrome/(([1-9]{1})|([0-7]{1}[0-9]{1})|(8[0-6]{1}))\." 1; # Chrome 1.* - 86.*
"~YaBrowser/(([1-9]{1})|([1-9]{1}[0-8]{1}))\." 1; # Yandex 1.* - 18.*
"~Firefox/(([1-9]{1})|([0-7]{1}[0-9]{1})|(8[0-3]{1}))\." 1; # Firefox 1.* - 83.*
"~EdgA?/(([1-9]{1})|([0-7]{1}[0-9]{1})|(8[0-6]{1}))\." 1; # Edge/MobileEdge 1.* - 86.*
"~Version/(([1-9]{1})|([1-9]{1}[0-1]{1}))\." 1; # Safari 1.* - 11.*
}
server {
listen 8080 default_server;
root /opt/app-root/src;
if ($uri ~ /not-supported) {
set $outdated 0;
}
if ($outdated = 1) {
rewrite ^ /not-supported/index.html last;
}
location / {
.......
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420973.html
標籤:
