openresty / openresty/lua-nginx-module

Non-GET requests are always 405 for non 2xx responses

Open
#1,610 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

I'm using OpenResty nginx version: openresty/1.15.8.1.

Here is my sample nginx block:


# Assume there is a logger globally available

server {
    # Generic html error pages
    error_page 401 @json401;

    # html error pages
    location @json401 {
        rewrite_by_lua 'return 1';
        try_files /errors/401.json =401;
        default_type application/json;
        internal;
    }

    rewrite_by_lua_block {
        logger.info('in rewrite/auth block');
        local auth = require 'myApp.auth'
        
        if not auth.isAuthorized() then
            logger.info('not authorized, 401ing')
            return ngx.exit(401)
        end
    }

    location = /test {
        content_by_lua_block {
            logger.info('in content (/test) block');
            ngx.status = 200
            ngx.say('test-page')
            ngx.exit(200)
        }
    }
}

Now when I do a curl localhost:80/test I get the expected behavior; I get a response 403 with the custom JSON response.

However, when I do a curl localhost:80/test -X POST (or any other non-GET methods), I get back:

< HTTP/1.1 405 Not Allowed
< Server: openresty
< Date: Thu, 17 Oct 2019 19:01:38 GMT
< Content-Type: text/html; charset=utf8
< Content-Length: 154
< Connection: close
< Cache-Control: no-cache
<
<html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>openresty</center>
</body>
</html>

Why is that? I know POST requests cannot serve static pages, which is why I am returning an application/json file.

If I change the error_page location block to ngx.say('{"test":true}') then it does work - but I want to serve the content from a file.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the sample nginx server block on OpenResty 1.15.8.1, comparing GET and POST requests that trigger error_page 401 and try_files. Trace nginx's method handling for the named error location; done means non-GET requests return the custom application/json file instead of the default 405 HTML response.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, nginx
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.