# Setting the status code

**URL:** <https://www.ruby-forum.com/t/setting-the-status-code/236108>\
**Category:** NGINX\
**Created:** [August 6, 2013, 5:02am UTC](https://www.ruby-forum.com/t/setting-the-status-code/236108 "2013-08-06T05:02:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [August 6, 2013, 5:02am UTC](https://www.ruby-forum.com/t/setting-the-status-code/236108/1 "2013-08-06T05:02:29Z")

</div>

Hi,  
I’m wondering how I can set a status code and still deliver a custom web  
page? Specifically I want to use a status code of 403 Forbidden but  
depending on the exact reason I want to display different custom error  
pages for that case.  
When I use the “return 403” directive I can no longer deliver content  
and at most a single custom page can be returned which is defined by the  
error\_page directive.

Since I determine the reason for the denied access in lua a way to do it  
there would also help. I already tried “nginx.status = 403” followed by  
a “nginx.exec(’/reason1’)” but while the right page is display the  
status code returned gets reset to 200.

Any ideas?

Regards,  
Dennis

---

<div class="post-metadata">

**Author:** ![Dennis\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@Dennis\_J](https://www.ruby-forum.com/u/Dennis_J)\
**Post date:** [August 6, 2013, 8:30am UTC](https://www.ruby-forum.com/t/setting-the-status-code/236108/2 "2013-08-06T08:30:27Z")

</div>

let your app handle and deliver error-pages

Posted at Nginx Forum:

> **[Re: Setting the status code](https://forum.nginx.org/read.php?2,241621,241626#msg-241626)**
>
> Hi, I'm wondering how I can set a status code and still deliver a custom web page? Specifically I want to use a status code of 403 Forbidden but depending on the exact reason I want to display different custom error pages for that case. When I use...

---

<div class="post-metadata">

**Author:** ![Dennis\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@Dennis\_J](https://www.ruby-forum.com/u/Dennis_J)\
**Post date:** [August 6, 2013, 1:31pm UTC](https://www.ruby-forum.com/t/setting-the-status-code/236108/3 "2013-08-06T13:31:53Z")

</div>

On 06/08/13 04:02, Dennis J. wrote:

> Since I determine the reason for the denied access in lua a way to do  
> it there would also help. I already tried “nginx.status = 403”  
> followed by a “nginx.exec(‘/reason1’)” but while the right page is  
> display the status code returned gets reset to 200.

Hi  
You can do it in lua.. you need to do it in the header filter stage  
I’m doing something similar but probably not exactly the same  
Hopefully example helps (untested):

```
     set $status_code "";
     location /
     {
         access_by_lua '
             -- your lua script here etc...
             -- if (an error happened) then
                 ngx.var.status_code = "403"
                 ngx.exec("/error/403.html")
             -- end
             ';
     }

     location /error
     {
         root html/error;
         header_filter_by_lua '
             if ngx.var.status_code ~= "" then
                 ngx.status = ngx.var.status_code
             end
         ';
     }

```

---

<div class="post-metadata">

**Author:** ![Dennis\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@Dennis\_J](https://www.ruby-forum.com/u/Dennis_J)\
**Post date:** [August 7, 2013, 12:20am UTC](https://www.ruby-forum.com/t/setting-the-status-code/236108/4 "2013-08-07T00:20:21Z")

</div>

On 06.08.2013 13:31, Richard K. wrote:

> I’m doing something similar but probably not exactly the same  
> – end  
> ';  
> }

That did the trick, thanks!

What I basically wound up doing is:

```
 location /error {
     root /var/www/html;
     header_filter_by_lua '
         ngx.status = 503
     ';
 }

```

Kind of awkward to be forced to use Lua just for this. There should be a  
“status\_code `” directive to make this possible without requiring
the Lua module.`

`
Regards,
Dennis`

---

<div class="post-metadata">

**Author:** ![Dennis\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a183cd/32.png) [@Dennis\_J](https://www.ruby-forum.com/u/Dennis_J)\
**Post date:** [August 6, 2013, 1:03pm UTC](https://www.ruby-forum.com/t/setting-the-status-code/236108/5 "2013-08-06T13:03:15Z")

</div>

On 06.08.2013 08:29, mex wrote:

> let your app handle and deliver error-pages

See basically all I want to do is return a single static html file and  
having to set up php/python/etc. just to serve this file seems like  
overkill to me. This is pretty much the most simple case for a web  
server to handle.

Regards,  
Dennis
