大网数据广告活动
服务器运维问答

Ingnoring any and all url parameters to harden security. How?

· 52CE 我爱测

Anyone who puts an NGINX server online and looks at its access.log a week latter will find lots of URL exploits being attempted. Every day, any time. So what if, I was to reject any and all url parameters. What if every query str…

问题原文

原帖链接 · suchislife · CC BY-SA 4.0

Anyone who puts an NGINX server online and looks at its access.log a week latter will find lots of URL exploits being attempted. Every day, any time.

So what if, I was to reject any and all url parameters. What if every query string was masked with an NGINX rewrite which would fail to match anything not defined as a rewrite naturally returning a 404 Not Found?

Or a series of rewrites in order placed inside this always present location block?

location / {
  rewrite "^/api/v1/users/?$" /api/v1/Users.php last;
  rewrite "^/api/v1/users/(all|active|inactive)?$" /api/v1/Users.php?status=$1 last;
  rewrite "^/api/v1/users/(\d+)/?$" /api/v1/Users.php?userId=$1 last;
}

# URL Match examples...

http://localhost/api/v1/users
http://localhost/api/v1/users/

http://localhost/api/v1/users/all
http://localhost/api/v1/users/active
http://localhost/api/v1/users/inactive

http://localhost/api/v1/users/2001
http://localhost/api/v1/users/2002
http://localhost/api/v1/users/2003

Is there a way to direct NGINX to ignore URL parameters? Feel free to let me know if this is a naive question. Perhaps I'm asking the wrong question.

已采纳解答

原帖链接 · digijay · CC BY-SA 4.0

Rather use return instead of rewrite because "rewrite directive can return only code 301 or 302". For this, create a location with a regular expression to catch the specific request uri.

And if you just want to drop all of these requests return a http status 444.

Example:

server {
    # ...
    location ~* ^/api/v1/users/.*$ {
        return  444;
    }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-08-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。