Mod rewrite 如何使用nginx将动态url处理为静态页面?
我想让我的Nginx作为静态页面提供动态url,例如Mod rewrite 如何使用nginx将动态url处理为静态页面?,mod-rewrite,nginx,content-management-system,static-pages,Mod Rewrite,Nginx,Content Management System,Static Pages,我想让我的Nginx作为静态页面提供动态url,例如 given a url "/book?name=ruby_lang&published_at=2014" , the nginx will serve a static file (which is generated automatically ) named as: "book?name=ruby_lang&published_at=2014.html" or: "book-name-eq-ruby_lang-pbl
given a url "/book?name=ruby_lang&published_at=2014" ,
the nginx will serve a static file (which is generated automatically ) named as:
"book?name=ruby_lang&published_at=2014.html" or:
"book-name-eq-ruby_lang-pblished_at-eq-2014.html"
这可能吗
注:
1.没有名为的静态文件:
"book?name=ruby_lang&published_at=2014.html" nor
"book-name-eq-ruby_lang-pblished_at-eq-2014.html"
但是,如果需要,我可以生成它们
2.我无法更改提供给消费者的url。e、 g.我的消费者只能通过以下方式向我发送请求:
"/book?name=ruby_lang&published_at=2014"
但不适用于任何其他URL 如果您可以自己生成HTML文件,那么只需使用nginx的重写模块即可。例如:
rewrite ^/book book-name-eq-$arg_name-published_at-eq-$arg_published_at.html last;
如果需要确保name和published_at有效,可以执行以下操作:
location = /book {
if ($arg_name !~ "^[A-Za-z\d_-]+$") { return 404; }
if ($arg_published_at !~ "^\d{4}$") { return 404; }
rewrite ^/book book-name-eq-$arg_name-published_at-eq-$arg_published_at.html last;
}
这将确保published_at是有效的4位整数,name是有效的标识符英文字母、数字、下划线和连字符
为了确保一本书只能从一个URL访问,如果URL是HTML文件,则应该抛出404。将此添加到上一条规则之前:
location ~ /book-(.*).html {
return 404;
}
如果您可以自己生成HTML文件,那么只需使用nginx的重写模块即可。例如:
rewrite ^/book book-name-eq-$arg_name-published_at-eq-$arg_published_at.html last;
如果需要确保name和published_at有效,可以执行以下操作:
location = /book {
if ($arg_name !~ "^[A-Za-z\d_-]+$") { return 404; }
if ($arg_published_at !~ "^\d{4}$") { return 404; }
rewrite ^/book book-name-eq-$arg_name-published_at-eq-$arg_published_at.html last;
}
这将确保published_at是有效的4位整数,name是有效的标识符英文字母、数字、下划线和连字符
为了确保一本书只能从一个URL访问,如果URL是HTML文件,则应该抛出404。将此添加到上一条规则之前:
location ~ /book-(.*).html {
return 404;
}
好的,多亏@Alon Gubkin的帮助,我终于解决了这个问题,请看:。以下是一些提示: 使用“try_files”而不是“rewrite” 在静态文件名中使用“-”而不是下划线“\u”,否则nginx在将$arg\u参数设置为文件名时会感到困惑。e、 g.使用platform-$arg_platform.json'而不是platform_$arg_platform.json 看一看 这是我的nginx配置代码片段:
server {
listen 100;
charset utf-8;
root /workspace/test_static_files;
index index.html index.htm;
# nginx will first search '/platform-$arg_platform...' file,
# if not found return /defautl.json
location /popup_pages {
try_files /platform-$arg_platform-product-$arg_product.json /default.json;
}
}
我还将我的代码放在github上,以便对此问题感兴趣的人可以查看:
好的,多亏@Alon Gubkin的帮助,我终于解决了这个问题,请参见:。以下是一些提示: 使用“try_files”而不是“rewrite” 在静态文件名中使用“-”而不是下划线“\u”,否则nginx在将$arg\u参数设置为文件名时会感到困惑。e、 g.使用platform-$arg_platform.json'而不是platform_$arg_platform.json 看一看 这是我的nginx配置代码片段:
server {
listen 100;
charset utf-8;
root /workspace/test_static_files;
index index.html index.htm;
# nginx will first search '/platform-$arg_platform...' file,
# if not found return /defautl.json
location /popup_pages {
try_files /platform-$arg_platform-product-$arg_product.json /default.json;
}
}
我还将我的代码放在github上,以便对此问题感兴趣的人可以查看:
你所说的“充当静态”是什么意思?您是否有名为book-name-eq-ruby_lang-pblished_at-eq-2014.html的文件?您的CMS是否支持自定义URL?我添加了注释,请参阅更新。文件是静态的还是按需生成的?您需要支持的是那个特定的URL还是某个URL方案?静态服务是什么意思?您是否有名为book-name-eq-ruby_lang-pblished_at-eq-2014.html的文件?您的CMS是否支持自定义URL?我添加了注释,请参阅更新。文件是静态的还是按需生成的?是你需要支持的特定URL还是某个URL方案?伙计,所有的中文后面都是英文翻译。祝你好运伙计,所有的中文后面都是英文翻译。祝你好运