mikyll commented on issue #12023: URL: https://github.com/apache/apisix/issues/12023#issuecomment-2730538551
I slightly changed your plugin, by removing `ngx.exit(ngx.HTTP_MOVED_TEMPORARILY)`. I tested it with the example below, using external service [httpbin.org](https://httpbin.org/), and it seems to be working. Can you have a look and provide a feedback? ## Example ### File Structure ```lang-none . ├── conf/ │ ├── apisix.yaml │ └── config.yaml ├── plugins/ │ └── custom-404-redirect.lua └── compose.yaml ``` ### File Sources File `conf/config.yaml`: ```yaml apisix: extra_lua_path: "/usr/local/apisix/apisix/plugins/custom/?.lua" deployment: role: data_plane role_data_plane: config_provider: yaml plugins: - custom-404-redirect #END ``` File `conf/apisix.yaml`: ```yaml upstreams: - id: external_httpbin nodes: "httpbin.org": 1 scheme: https routes: - id: 1 uri: /anything upstream_id: external_httpbin - id: 2 uris: - /shop/products/* upstream_id: external_httpbin plugins: custom-404-redirect: redirect_url: /anything #END ``` File `plugins/custom-404-redirect.lua`: ```lua local core = require("apisix.core") local ngx = ngx local plugin_name = "custom-404-redirect" local schema = { type = "object", properties = { redirect_url = { type = "string", minLength = 1 } }, required = { "redirect_url" } } local _M = { version = 0.1, priority = 1010, -- set the plugin priority, higher means earlier execution name = plugin_name, schema = schema } function _M.check_schema(conf) return core.schema.check(schema, conf) end function _M.header_filter(conf, ctx) if ngx.status == 404 then ngx.status = ngx.HTTP_MOVED_TEMPORARILY ngx.header["Location"] = conf.redirect_url core.log.warn("Redirecting to custom 404 page: " .. conf.redirect_url) -- ngx.exit(ngx.HTTP_MOVED_TEMPORARILY) -- Comment/remove this line end end File compose.yaml: services: apisix: container_name: apisix-test-redirect image: apache/apisix:latest restart: no stdin_open: true tty: true environment: - APISIX_STAND_ALONE=true volumes: - ./conf/config.yaml:/usr/local/apisix/conf/config.yaml - ./conf/apisix.yaml:/usr/local/apisix/conf/apisix.yaml - ./plugins:/usr/local/apisix/apisix/plugins/custom/apisix/plugins ports: - "9080:9080/tcp" - "9443:9443/tcp" ``` ## Testing ### Setup Run the following: ```bash docker compose up ``` ### Testing the Route Run the following: ```bash curl -sL localhost:9080/shop/products/test -i ``` Result: ```bash HTTP/1.1 302 Moved Temporarily Content-Type: text/html Content-Length: 233 Connection: keep-alive Date: Mon, 17 Mar 2025 16:58:54 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true Server: APISIX/3.11.0 Location: /anything HTTP/1.1 200 OK Content-Type: application/json Content-Length: 388 Connection: keep-alive Date: Mon, 17 Mar 2025 16:58:55 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true Server: APISIX/3.11.0 { "args": {}, "data": "", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Host": "localhost", "User-Agent": "curl/8.5.0", "X-Amzn-Trace-Id": "Root=1-67d854ce-0853fc4a1447023a613f0439", "X-Forwarded-Host": "localhost" }, "json": null, "method": "GET", "origin": "172.X.X.X, 89.X.X.X", "url": "https://localhost/anything" } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
