如何在nginx中通过查询字符串重定向js?
这是我的html如何在nginx中通过查询字符串重定向js?,nginx,Nginx,这是我的html 我的申请 文件夹中有两个js文件,一个用于生产(app.min.js),另一个用于开发(app.debug.js) 我想要的是通过查询字符串切换js文件。就像: http://example.com/ -> use app.min.js http://example.com/?debug=1 -> use app.dev.js 我尝试使用以下配置,但它不起作用 server { server_name self-hosted.com; listen
我的申请
文件夹中有两个js文件,一个用于生产(app.min.js
),另一个用于开发(app.debug.js
)
我想要的是通过查询字符串切换js文件。就像:
http://example.com/ -> use app.min.js
http://example.com/?debug=1 -> use app.dev.js
我尝试使用以下配置,但它不起作用
server {
server_name self-hosted.com;
listen 80;
root /path/to/my/project;
proxy_buffering off;
location / {
if ($query_string ~ debug=1) {
rewrite ^/app.min.js$ /app.dev.js? permanent;
}
try_files $uri $uri/ =404;
}
}
查询字符串仅出现在HTML文件的初始请求中。JS文件的后续请求包含在
script
标记的src
属性的值中
您可以使用两个HTML文件,每个文件的src
属性值不同,或者在交付HTML文件时使用更改src
属性值
例如:
root /path/to/my/project;
location = / {
if ($arg_debug) { rewrite ^ /debug.html last; }
index index.html;
}
location = /debug.html {
internal;
try_files /index.html =404;
sub_filter '<script src="app.min.js"></script>' '<script src="app.dev.js"></script>';
}
location / {
try_files $uri $uri/ =404;
}
root/path/to/my/project;
位置=/{
if($arg_debug){rewrite^/debug.html last;}
index.html;
}
location=/debug.html{
内部的;
try_files/index.html=404;
子过滤器“”;
}
地点/{
try_files$uri$uri/=404;
}
根据和@Richard Smith的提示,我找到了解决方案
server {
server_name self-hosted.com;
listen 80;
root /path/to/my/project;
proxy_buffering off;
location / {
error_page 418 = @debug;
if ($query_string ~ debug=1) { return 418; }
try_files $uri $uri/ =404;
}
location @debug {
sub_filter "app.min" "app.dev";
try_files /index.html =500;
}
}
我认为你做不到。查询字符串是初始请求(对于html文件)的一部分,
/app.min.js
URI是后续请求,它将不包括查询字符串。@RichardSmith您的意思是我需要使用另一个html文件吗?@RichardSmith谢谢您的提示<代码>子过滤器是一个不错的选择。