On Fri, Nov 30, 2012 at 10:33:06AM +0800, 任晓磊 wrote:
return 410 ‘$remote_addr 禁止访问云云’;
resource.
Powered by Tengine
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?
This is due to implementation differences in handling internal redirects
and named locations.
When you “return 410” on the server level, “error_page 410 /410” does
an internal redirect to location “/410”. This is processed as a new
request, including running ngx_http_rewrite_module directives specified
on the server level, thus “return 410” fires again. Because
“recursive_error_pages” is off, standard error page is returned.
(If you turn it on, you’ll get 500 instead, due to redirection cycling.)
“error_page 410 @410”, on the other hand, does a redirect straight to a
named location “@410”, thus the processing skips “server rewrites” and
“find configuration” phases of processing, and you get a custom body
for 410 as expected.
You can avoid these differences by moving what you now have on the
server level to “location /”.
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.
Content-Type is controlled by Module ngx_http_core_module and
http://nginx.org/r/types directives, for example:
: server {
: default_type application/octet-stream;
: types {
: text/html html;
: }
:
: error_page 403 @error;
:
: location @error {
: default_type text/plain;
: types {}
:
: return 200 ‘error $status\n’;
: }
: }
: $ echo test > html/test.html
: $ curl -i http://localhost:8000/test.html
: HTTP/1.1 200 OK
: Server: nginx/1.3.9
: Date: Fri, 30 Nov 2012 09:53:35 GMT
: Content-Type: text/html
: Content-Length: 5
: Last-Modified: Fri, 30 Nov 2012 09:53:31 GMT
: Connection: keep-alive
: ETag: “50b8821b-5”
: Accept-Ranges: bytes
:
: test
: $ chmod u-r html/test.html
: $ curl -i http://localhost:8000/test.html
: HTTP/1.1 403 Forbidden
: Server: nginx/1.3.9
: Date: Fri, 30 Nov 2012 09:53:46 GMT
: Content-Type: text/plain
: Content-Length: 10
: Connection: keep-alive
:
: error 403
See also Module ngx_http_ssi_module