openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.33k stars 2.03k forks source link

ngx.say not work #2310

Closed transtone closed 6 months ago

transtone commented 6 months ago
  location /api/ {

    set $userId '';
    set $companyCode '';

    access_by_lua_block {
      local cjson = require "cjson"
      local response = {}
      response['code'] = 401
      response['msg'] = "请登录!"
      response['result'] = ngx.var.userId
      ngx.status = 401
      ngx.say(cjson.encode(response))
    }

    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Credentials' false always;
    add_header 'Access-Control-Allow-Methods' 'OPTIONS,GET,PUT,POST,PATCH,DELETE' always;
    add_header 'Access-Control-Allow-Headers' 'User-Agent,Tenant-Code,Content-Type,Token,Timestamp,Referer,Origin,Connection' always;
    if ($request_method = 'OPTIONS') {
      return 204;
    }
    proxy_hide_header Access-Control-Allow-Origin;

    proxy_pass http://127.0.0.1:18080/;
    proxy_redirect default;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout 3600;

    proxy_set_header User-Id $userId;
    proxy_set_header Company-Code $companyCode;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

openresty/openresty:1.25.3.1-centos

I want it return 401, but it proxied to 127.0.0.1:18080. what can I do?

==============

must add ngx.eof() after ngx.say('say something')