I
I
Ivan Novikov2017-01-17 18:06:44
Nginx
Ivan Novikov, 2017-01-17 18:06:44

Nginx: code 503 in config actually turns out to be 302. Why?

server {
listen $ip:$port;
server_name example.com;

location / {
    root   /usr/share/nginx/html;
    index index.html index.htm;
    error_page 503 http://example.com/maintrance.html;
    return 503;
}

location = /maintrance.html {
return 200;
}

Intuitively, I expect that along with maintrance.html, the status code 503 will arrive, but 302 arrives (which is nowhere in the config and, by default, it seems to have nowhere to squeeze through, all codes are written explicitly) and then 200 arrives. Well, this is understandable.
Why 302, what I don't know and where am I the fool? Maybe that's how it should be?
Solution with expected behavior:
server {
listen $ip:$port;
server_name example.com;

location / {
    root   /usr/share/nginx/html;
    index index.html index.htm;
    error_page 503 /maintrance.html;
    return 503;
}
# Без этого локейшна в такой конфигурации сервер отдаёт не ту страницу для 503:
location /maintrance.html {
return 200;
}

}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry MiksIr, 2017-01-17
@stranger777

Since your URL in error_page is specified with http and domain, an external redirect is implied. Hence 302. To get what you want, write
Also, if you want to get 200 in the response, it is not necessary to describe a separate location, you can solve the same error_page, it will be like this
And then location = /maintrance.html can be thrown out .
UPD: Yes, I did. Since error_page makes an internal redirect, if there is no separate location /maintrance.html - the server again falls into location / and an endless cycle of redirects is obtained. So in this case location /maintrance.html is needed.
Along the way, I want to focus on another method, more popular, especially if we do not need to issue 503.

location / {
    root   /usr/share/nginx/html;
    index index.html index.htm;
    try_files /maintrance.html $uri $uri/ =404;
}

try_files iterates over the specified arguments until it encounters an existing one on disk. Those. in this configuration, it is enough to create /usr/share/nginx/html/maintrance.html - it starts to be issued, if it is deleted, normal requests start working (parameter $uri and $uri/).

A
Andrey Nikolaev, 2017-01-17
@gromdron

Maybe it's internal? As it were, 503 is Service Unaviable, and instead of returning to /maintrance.html you send it.
And he already gives 200.
Try removing:

location = /maintrance.html {
return 200;
}

Will there be a similar result?

I
Ivan Novikov, 2017-01-17
@stranger777

Solution with expected behavior:

server {
listen $ip:$port;
server_name example.com;

location / {
    root   /usr/share/nginx/html;
    index index.html index.htm;
    error_page 503 /maintrance.html;
    return 503;
}
# Без этого локейшна в такой конфигурации сервер отдаёт не ту страницу для 503:
location /maintrance.html {
return 200;
}

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question