V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
LxnChan
V2EX  ›  NGINX

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

  •  
  •   LxnChan ·
    lxnchan · 2022-09-19 13:53:27 +08:00 · 1218 次点击
    这是一个创建于 556 天前的主题,其中的信息可能已经有所发展或是发生改变。

    例如访问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;
        }
    
    5 条回复    2022-09-19 15:54:01 +08:00
    julyclyde
        1
    julyclyde  
       2022-09-19 14:00:32 +08:00
    if ($arg_para=correct)
    julyclyde
        2
    julyclyde  
       2022-09-19 14:01:13 +08:00
    担心死循环的话,就把逻辑反过来写
    只处理错误的情况,return404 ;正常情况就让 nginx 自己管
    LxnChan
        3
    LxnChan  
    OP
       2022-09-19 14:27:19 +08:00
    @julyclyde 那请教一下反过来怎么写呢?“!=”吗
    oott123
        4
    oott123  
       2022-09-19 15:15:28 +08:00
    /t/879586
    我还以为我穿越了
    julyclyde
        5
    julyclyde  
       2022-09-19 15:54:01 +08:00
    @oott123 诶奇怪,你说的这篇也就几天前,但是前几天我没看到这篇帖子
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4626 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:04 · PVG 18:04 · LAX 03:04 · JFK 06:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.