Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cant get upstream.current_upstream_name to work #39

Open
amgorb opened this issue Dec 29, 2016 · 5 comments
Open

cant get upstream.current_upstream_name to work #39

amgorb opened this issue Dec 29, 2016 · 5 comments

Comments

@amgorb
Copy link

amgorb commented Dec 29, 2016

tried different variants of configuration, no change
config:
$upstream_name used in log_format

server {
.. 
set $upstream_name 'undef';
..
.. 
}
location @siteapi_common {
 ..
  log_by_lua_block {
  ngx.var.upstream_name = "test"
  local upstream = require "ngx.upstream"
  ngx.log(ngx.INFO, "upstream = " .. tostring(upstream.current_upstream_name()))
  local upstream_name = upstream.current_upstream_name()
  ngx.var.upstream_name = upstream_name
  }
..
    proxy_pass $backend_scheme://_bk_api_common;
}

expect to see upstream name (_bk_api_common) in logs but got 'test' string and following in error.log:

2016/12/29 16:13:16 [error] 12547#12547: *20 failed to run log_by_lua*: log_by_lua(api_fallback:36):4: no srv conf for upstream
stack traceback:
        [C]: in function 'current_upstream_name'
        log_by_lua(api_fallback:36):4: in function <log_by_lua(api_fallback:36):1> while logging request, client: ip, server:test, request: "GET /_api/deals/", host: "dev-10--www.testsite"

upstreams are working fine.
What can cause 'no srv conf for upstream' error?

@agentzh
Copy link
Member

agentzh commented Dec 29, 2016

@amgorb Please provide a minimal and standalone example that we can easily run and reproduce the problem on our side. Thank you. You can check out the test suite of this module for examples of "minimal and standalone" test cases.

@agentzh
Copy link
Member

agentzh commented Dec 29, 2016

@amgorb Your example above is incomplete since there lacks definitions for things like _bk_api_common and $backend_scheme. Also it lacks test requests and entry location that trigger the problem. Please do not make us guess. Thanks!

@amgorb
Copy link
Author

amgorb commented Dec 30, 2016

i was able to reproduce problem with config below:
also reproduced with test in #40:

nginx version: nginx/1.11.3
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-stream_geoip_module=dynamic --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --add-module=modules/nginx-http-auth-pam-module --add-module=modules/nginx-dav-ext-module --add-module=modules/echo-nginx-module --add-module=modules/nginx-http-sysguard --add-module=modules/headers-more-nginx-module --add-module=modules/nginx-cache-purge --add-module=modules/nginx-development-kit --add-module=modules/ngx-fancyindex --add-module=modules/nginx-lua --add-module=modules/nginx-upload-progress --add-module=modules/ngx_http_substitutions_filter_module --add-module=modules/encrypted-session-nginx-module --add-module=modules/ngx_http_upstream_ketama_chash --add-module=modules/set-misc-nginx-module --add-module=modules/srcache-nginx-module --add-module=modules/memc-nginx-module --add-module=modules/ngx_dynamic_upstream --add-module=modules/lua-upstream-nginx-module --with-http_v2_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro'
events {
  worker_connections  1024;
}
error_log /var/log/nginx/error.log debug;

http {
    map $host $backend_http {
      default 'http';
    }

    upstream some_upstream {
        server 127.0.0.1:80;
    }

    upstream some_other_upstream {
        server 127.0.0.1:81;
    }


server {
    log_by_lua_block {
        local upstream = require "ngx.upstream"
        ngx.log(ngx.INFO, "upstream = " .. tostring(upstream.current_upstream_name()))
    }
    location /test {
        proxy_pass http://some_upstream/back;
    }
    location /test2 {
        proxy_pass $backend_http://some_upstream/back;
    }
    location /back {
        echo ok;
    }
}
}
cat error.log
2016/12/30 14:29:33 [notice] 28885#28885: using the "epoll" event method
2016/12/30 14:29:33 [notice] 28885#28885: nginx/1.11.3
2016/12/30 14:29:33 [notice] 28885#28885: built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
2016/12/30 14:29:33 [notice] 28885#28885: OS: Linux 4.8.6-x86_64-linode78
2016/12/30 14:29:33 [notice] 28885#28885: getrlimit(RLIMIT_NOFILE): 1024:65536
2016/12/30 14:29:33 [notice] 28886#28886: start worker processes
2016/12/30 14:29:33 [notice] 28886#28886: start worker process 28887
2016/12/30 14:29:35 [info] 28887#28887: *3 [lua] log_by_lua(nginx.conf:24):3: upstream = nil while logging request, client: 127.0.0.1, server: , request: "GET /back HTTP/1.0", host: "some_upstream"
2016/12/30 14:29:35 [error] 28887#28887: *1 failed to run log_by_lua*: log_by_lua(nginx.conf:24):3: no srv conf for upstream
stack traceback:
       	[C]: in function 'current_upstream_name'
       	log_by_lua(nginx.conf:24):3: in function <log_by_lua(nginx.conf:24):1> while logging request, client: 127.0.0.1, server: , request: "GET /test2 HTTP/1.1", upstream: "http://127.0.0.1:80/back", host: "localhost"
2016/12/30 14:29:35 [info] 28887#28887: *1 client 127.0.0.1 closed keepalive connection

@agentzh
Copy link
Member

agentzh commented Dec 30, 2016

@amgorb You know that your log_by_lua_block directive will also get inherited by your location /back which does not have any upstream facilities (like proxy_pass) configured?

Your error message is for that /back request if you look at the last part of this message:

[lua] log_by_lua(nginx.conf:24):3: upstream = nil while logging request, client: 127.0.0.1, server: , request: "GET /back HTTP/1.0", host: "some_upstream"

@mfrederico
Copy link

mfrederico commented Jun 29, 2018

I am using php-fpm and I am also hitting this same issue .. I am not sure exactly WHEN current_upstream_name is going to be available for use? I would assume that body_filter is AFTER everything has processed, and theoretically the upstream should be initialized by this point?

Perhaps there is a different upstream for php-fpm?

I am getting:
"no srv conf for upstream" in my debug log

2018/06/29 12:19:46 [debug] 23347#0: *6 pipe write downstream flush in
2018/06/29 12:19:46 [debug] 23347#0: *6 http output filter "/index.php?"
2018/06/29 12:19:46 [debug] 23347#0: *6 http copy filter: "/index.php?"
2018/06/29 12:19:46 [debug] 23347#0: *6 lua body filter for user lua code, uri "/index.php"
2018/06/29 12:19:46 [error] 23347#0: *6 failed to run body_filter_by_lua*: body_filter_by_lua:6: no srv conf for upstream
stack traceback:
	[C]: in function 'current_upstream_name'
	body_filter_by_lua:6: in function <body_filter_by_lua:1> while sending to client, client: 127.0.0.1, server: _, request: "GET /dashboard/login HTTP/1.1", upstream: "fastcgi://127.0.0.1:9071", host: "getmotivated.localhost"
2018/06/29 12:19:46 [debug] 23347#0: *6 http copy filter: -1 "/index.php?"
2018/06/29 12:19:46 [debug] 23347#0: *6 event timer: 18, old: 1530296446622, new: 1530296446631
2018/06/29 12:19:46 [debug] 23347#0: *6 http upstream exit: 0000000000000000
2018/06/29 12:19:46 [debug] 23347#0: *6 finalize http upstream request: 0
2018/06/29 12:19:46 [debug] 23347#0: *6 finalize http fastcgi request
2018/06/29 12:19:46 [debug] 23347#0: *6 free rr peer 1 0
2018/06/29 12:19:46 [debug] 23347#0: *6 close http upstream connection: 18

I am doing the following in my nginx config at the bottom of my server block after everything:

    body_filter_by_lua_block {
        local cjson          = require 'cjson';
        local upstream       = require "ngx.upstream"

        local us = upstream.get_upstreams()
        local replacestr = "<head><!-- Upstreams: "..cjson.encode(upstream.current_upstream_name()).." -->"
        ngx.arg[1] = string.gsub(ngx.arg[1],"<head>", replacestr);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants