about error_page and named location

Maxim Dounin mdounin at mdounin.ru
Fri Nov 30 07:51:52 UTC 2012


On Fri, Nov 30, 2012 at 10:33:06AM +0800, 任晓磊 wrote:

> For rejecting some unfriendly access, I use 410 status code for them. The
> config is below:
> error_page 410 /410;
> if (xxx) {
>  return 410;
> }
> location /410 {
>  more_set_headers "Content-Type: text/html;charset=utf8;";
> return 410 '$remote_addr 禁止访问云云';
> }
> When I access it for testing purpose, I got
> <html>
> <head><title>410 Gone</title></head>
> <body bgcolor="white">
> <h1>410 Gone</h1>
> <p>The requested resource is no longer available on this server and there
> is no forwarding address. Please remove all references to this
> resource.<hr/>Powered by Tengine
> </body>
> </html>
> If I use error_page 410 @410; and location @410{...} , it works correctly,
> and serves a page with my own message.
> So,
> 1st, What's the differences between norma location and named location, in
> this context?

Named location is looked up directly, without executing 
server-level rewrite module directives again (which in your above 
config will return another 410 error, resulting in default error 
being returned).

> 2nd, Are there better ways to serve a html page to unfriendly access, and
> use nginx's variables in the html? I tried return 410 '$remote_addr
> rejected' ,but it gave a application/octet-stream response, browser would
> download it instead of display the message in browser window. So I have to
> use more headers module to set Content-Type.

Use text extension to be matched by mime.types, or set 
default_type correctly for the location in question 

Maxim Dounin

More information about the nginx mailing list