请问一下如何在 nginx 配置层面做到带参数(且参数正确)时返回页面本身内容,否则返回 404?

2022-09-19 13:53:27 +08:00
 LxnChan

例如访问https://my.website/aaa.html?para=correct时返回aaa.html的内容,否则跳转到bbb.html

目前我在配置文件中写了query_string但是感觉会死循环:

	# Test
	location ~* ^/aaa.html {
	#设置 content type
	default_type text/html ;
	
	# HTTP Status Code 和 内容 
	if ($query_string ~ ".*(?:^|\?|&)para=correct(?:(?:&.*)|$)") { 
		return 200 /aaa.html; 
	}
	return 200  /bbb.html;
    }
1258 次点击
所在节点    NGINX
5 条回复
julyclyde
2022-09-19 14:00:32 +08:00
if ($arg_para=correct)
julyclyde
2022-09-19 14:01:13 +08:00
担心死循环的话,就把逻辑反过来写
只处理错误的情况,return404 ;正常情况就让 nginx 自己管
LxnChan
2022-09-19 14:27:19 +08:00
@julyclyde 那请教一下反过来怎么写呢?“!=”吗
oott123
2022-09-19 15:15:28 +08:00
/t/879586
我还以为我穿越了
julyclyde
2022-09-19 15:54:01 +08:00
@oott123 诶奇怪,你说的这篇也就几天前,但是前几天我没看到这篇帖子

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/881246

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX