You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "rohitkrishna-marneni (via GitHub)" <gi...@apache.org> on 2025/03/06 20:19:56 UTC

[I] help request: How to create a custom-404-redirect plugin? [apisix]

rohitkrishna-marneni opened a new issue, #12023:
URL: https://github.com/apache/apisix/issues/12023

   ### Description
   
   I want to create a plugin where if upstream status is 404, I will be able to specify a custom 404 page and it should go there.
   
   So this is what I have so far.
   
   My apisix configuration for route
   ```yml
   services:
     - name: my-service
       upstream:
         pass_host: pass
         nodes:
           - host: my-api.com
             port: 80
             weight: 1
             priority: 0
         type: roundrobin
         scheme: http
         name: my-api
         hash_on: vars
         timeout:
           send: 10
           read: 60
           connect: 2
       routes:
         - name: my product page route
           hosts:
             - "mysite.com"
           uris:
             - /shop/products/*
           priority: 0
           plugins:
             custom-404-redirect:
               redirect_url: /us/en/footer/terms-conditions.html
   ```
   
   And my plugin code is as shown below
   
   ```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 = 302
           ngx.header["Location"] = conf.redirect_url
           ngx.log(ngx.ERR, "Redirecting to custom 404 page: ", conf.redirect_url)
           ngx.exit(ngx.HTTP_MOVED_TEMPORARILY)
           end
   end
   
   return _M
   ```
   
   But this is not working and simply throwing a 500 status code instead of going to a 404 as expected. Is it not possible to do it via plugin?
   
   ### Environment
   
   - APISIX version (run `apisix version`):
   - Operating system (run `uname -a`): Ubuntu
   - OpenResty / Nginx version (run `openresty -V` or `nginx -V`):
   - etcd version, if relevant (run `curl http://127.0.0.1:9090/v1/server_info`):
   - APISIX Dashboard version, if relevant:
   - Plugin runner version, for issues related to plugin runners:
   - LuaRocks version, for installation issues (run `luarocks --version`):
   


-- 
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: notifications-unsubscribe@apisix.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] help request: How to create a custom-404-redirect plugin? [apisix]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop closed issue #12023: help request: How to create a custom-404-redirect plugin?
URL: https://github.com/apache/apisix/issues/12023


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] help request: How to create a custom-404-redirect plugin? [apisix]

Posted by "rohitkrishna-marneni (via GitHub)" <gi...@apache.org>.
rohitkrishna-marneni commented on issue #12023:
URL: https://github.com/apache/apisix/issues/12023#issuecomment-2770548160

   One other problem with this approach is that 
   - original status is not preserved. Meaning 404 status change to 302. This might be not be good for teams checking for 404 or even in logs you might not get proper 404 metrics.
   - the other issue is that uri also changes in the browser which might not always be expected.


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] help request: How to create a custom-404-redirect plugin? [apisix]

Posted by "juzhiyuan (via GitHub)" <gi...@apache.org>.
juzhiyuan commented on issue #12023:
URL: https://github.com/apache/apisix/issues/12023#issuecomment-2705404802

   duplicated as https://github.com/apache/apisix/issues/12007


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] help request: How to create a custom-404-redirect plugin? [apisix]

Posted by "mikyll (via GitHub)" <gi...@apache.org>.
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] help request: How to create a custom-404-redirect plugin? [apisix]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop commented on issue #12023:
URL: https://github.com/apache/apisix/issues/12023#issuecomment-4010827368

   Duplicate of #12264 


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] help request: How to create a custom-404-redirect plugin? [apisix]

Posted by "rohitkrishna-marneni (via GitHub)" <gi...@apache.org>.
rohitkrishna-marneni commented on issue #12023:
URL: https://github.com/apache/apisix/issues/12023#issuecomment-2733227061

   @mikyll Thanks for the help. Now it seems to be working, however I have a weird issue.
   
   I applied this to a route called "/shop/*". So when I go to a page like "/shop/invalid-product" I get proper 404 page as expected. 
   
   However if I go to a valid page "/shop/valid-product", then this page makes some asset calls(css, js, ajax etc). Lets assume one such call is "/shop/price/...". Lets also assume that this endpoint throws a 404, but because we have a custom plugin its returning a 404 but with custom html body(the 404 url that we gave) and as a result that 404 page is being embedded into my main "/shop/valid-product" page.
   
   Is there a way to apply this to only the main endpoint which I am hitting from the browser? That is if I go "google.com", this should apply only to that endpoint and not for any ajax/css/js(etc) calls that happen on that page?


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] help request: How to create a custom-404-redirect plugin? [apisix]

Posted by "juzhiyuan (via GitHub)" <gi...@apache.org>.
juzhiyuan closed issue #12023: help request: How to create a custom-404-redirect plugin?
URL: https://github.com/apache/apisix/issues/12023


-- 
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: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org