Custom error pages for retun directive

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
<

404 Not Found

404 Not Found


nginx * 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:

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 D.
http://nginx.org/

How can “if in location” influence productivity?

Posted at Nginx Forum:

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 D.
http://nginx.org/

Thanks!

Posted at Nginx Forum: