Opened 3 months ago

Closed 3 months ago

#1836 closed defect (duplicate)

limit_req and proxy_pass

Reported by: vvershkov@… Owned by:
Priority: minor Milestone:
Component: nginx-core Version: 1.12.x
Keywords: Cc:
uname -a: Linux app15.0.6-1.el7.elrepo.x86_64 #1 SMP Wed Apr 3 05:57:04 EDT 2019 x86_64 x86_64 x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

Description

I have location like:

location ~ (.*)\.(png|svg|jpg|jpeg|js|ico|html|apk|txt|xml|gz) {
    proxy_set_header        Host $http_host;
    proxy_pass              http://web_static/root/production/$1.$2;
    error_page              404 403 = @node_static_root;
  }

and limit config like:

map ":$http_user_agent:$http_x_cookie1:$http_x_cookie2:" $limit_headers {
  default                   0;
  ~::                       1;
}

map $request $limit_url {
  default                   0;
  ~/some/url1               1;
  ~/some/url2               1;
  ~/some/url3               1;
}

geo $limit_ip {
  default                   1;
  someip1/24                0;
  someip2/24                0;
}
map "$limit_headers$limit_ip$limit_url" $limit_key {
  default    "";
  111         $binary_remote_addr;
}

limit_req_zone $limit_key zone=limit_req_by_ip:30m rate=10r/m;
limit_req_log_level error;
limit_req_status 403;

and a limit_req that I can add to http, server or location:

limit_req zone=limit_req_by_ip burst=30 nodelay;

It works like "if user don't have user_agent or cookie1 or cookie2 and he is not in white IP list and he is requesting blacklisted URL - limit his rps by IP"

The problem is when I add limit_req to server or to "location /", which affects static location - proxy_pass doesn't work. I don't get any requests on upstreams: I am getting 301 redirect to http://web_static/root/production/. instead. If I remove dot in this proxy pass ($1$2 - like somepicpng instead of somepic.png) or remove limit_req from this location - everything works just fine.

I guess that there is something with mapping $request or something else...

Change History (1)

comment:1 Changed 3 months ago by mdounin

  • Resolution set to duplicate
  • Status changed from new to closed

The problem is that you are using $1 and $2 in the proxy_pass URI, and these are expanded from the most recent regular expression being evaluated. Without limit_req, it is from the location ~ ... in your configuration. But with limit_req it is from one of the maps. To fix this, avoid using numbered captures - use named captures like (?<foo>.*) instead.

Closing this as a duplicate of #1285.

Note: See TracTickets for help on using tickets.