Forum: NGINX custom error pages for retun directive

2974d09ac2541e892966b762aad84943?d=identicon&s=25 kay (Guest)
on 2014-08-26 10:49
(Received via mailing list)
It is not possible to set custom error page. For example
/usr/share/nginx/404.html contains "test":

server {
error_page 404 /404.html;

if ($request_method = "GET")
return 404;
}

location / {
proxy_pass http://localhost:8080;
}

location /404.html {
/usr/share/nginx/404.html;
}
}


curl -v 127.0.0.1
* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7
NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 127.0.0.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Server: nginx
< Date: Tue, 26 Aug 2014 08:45:26 GMT
< Content-Type: text/html
< Content-Length: 162
< Connection: keep-alive
< Keep-Alive: timeout=20
<
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

But if I'll make rewrite rule:

server {
error_page 404 /404.html;

if ($request_method = "GET")
rewrite ^ /404.html last;
}

location / {
proxy_pass http://localhost:8080;
}

location /404.html {
/usr/share/nginx/404.html;
}
}

I'll get:

curl -v 127.0.0.1
* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7
NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: 127.0.0.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Tue, 26 Aug 2014 08:46:25 GMT
< Content-Type: text/html
< Content-Length: 5
< Last-Modified: Tue, 26 Aug 2014 08:42:47 GMT
< Connection: keep-alive
< Keep-Alive: timeout=20
< ETag: "53fc4887-5"
< Accept-Ranges: bytes
<
test
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

But I'll get 200 OK.

Is it possible to get cutsom 404 error page with 404 error code using
"return 404" directive?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,252825,252825#msg-252825
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2014-08-26 14:00
(Received via mailing list)
Hello!

On Tue, Aug 26, 2014 at 04:48:41AM -0400, kay wrote:

> location / {
> proxy_pass http://localhost:8080;
> }
>
> location /404.html {
> /usr/share/nginx/404.html;
> }
> }

[...]

> Is it possible to get cutsom 404 error page with 404 error code using
> "return 404" directive?

The request processing with the above configuration is as follows:

- if (...) matches, 404 returned;

- error_page redirects the request to /404.html;

- if (...) again matches, 404 returned;

- recursive_error_pages is off, so builtin 404 page is returned.

As you can see, the problem is that "if (...)" specified at
server level matches again after the redirect.  To fix things,
move the if into the "location /", it will prevent if from
matching requests to /404.html:

    location / {
        if ($request_method = "GET") {
            return 404;
        }

        proxy_pass http://localhost:8080;
    }

    location = /404.html {
        # static file
    }

--
Maxim Dounin
http://nginx.org/
2974d09ac2541e892966b762aad84943?d=identicon&s=25 kay (Guest)
on 2014-08-26 14:42
(Received via mailing list)
How can "if in location" influence productivity?

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,252825,252844#msg-252844
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2014-08-26 15:53
(Received via mailing list)
Hello!

On Tue, Aug 26, 2014 at 08:41:31AM -0400, kay wrote:

> How can "if in location" influence productivity?

There is no difference from performance point of view.

--
Maxim Dounin
http://nginx.org/
2974d09ac2541e892966b762aad84943?d=identicon&s=25 kay (Guest)
on 2014-08-27 05:02
(Received via mailing list)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.