nginx代理只允许使用指定方法POST
需求:
nginx反向代理,其中有一个接口,为了安全考虑,只允许使用POST方法请求,其他方法返回405
代码:
set $notlogin 0; if ($request_uri ~* "login") { set $notlogin '${notlogin}1'; } if ($request_method !~ ^(POST)$ ) { set $notlogin '${notlogin}2'; } if ($notlogin = '012') { return 405; }
整体代码:
location /test/ { #if ($request_method !~ ^(GET|POST)$) { # return 405; #} set $notlogin 0; if ($request_uri ~* "login") { set $notlogin '${notlogin}1'; } if ($request_method !~ ^(POST)$ ) { set $notlogin '${notlogin}2'; } if ($notlogin = '012') { return 405; } proxy_pass http://testserver/; proxy_buffers 16 1024k; proxy_busy_buffers_size 2048k; proxy_temp_file_write_size 2048k; client_max_body_size 4000m; proxy_set_header Host $host:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }