# Custom 404 pages with auth\_request

**URL:** <https://community.nginx.org/t/custom-404-pages-with-auth-request/8895>\
**Category:** NGINX\
**Tags:** web-server\
**Created:** [January 25, 2026, 11:33pm UTC](https://community.nginx.org/t/custom-404-pages-with-auth-request/8895 "2026-01-25T23:33:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xlf](https://avatars.discourse-cdn.com/v4/letter/x/e5b9ba/32.png) [@xlf](https://community.nginx.org/u/xlf)\
**Post date:** [January 25, 2026, 11:33pm UTC](https://community.nginx.org/t/custom-404-pages-with-auth-request/8895/1 "2026-01-25T23:33:40Z")

</div>

I am using auth\_request to serve files in /protected to logged in users and if it doesn’t exist try /public. Logged out users should just try /public. I have the custom 404 page as /404 which should also use /protected/404.html or /public/404.html.

The custom 404 page is shown for pages that don’t exist when the user is logged in. But it shows the default nginx 404 page when the user is logged out. How can I always show the custom one?

```
http {
  server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;

    location /auth {
      internal;
      # Assuming you have a backend service that checks authentication and returns 200 if authenticated, and 401 or other error codes if not
      proxy_pass http://your-auth-service;
      proxy_pass_request_body off;
      proxy_set_header Content-Length 0;
      proxy_set_header X-Original-URI $request_uri;
    }

    location / {
      # Perform authentication check
      auth_request /auth;
      error_page 401 = @error401;

      # If authenticated, first try to serve files from the protected directory. Finally, try the public directory as a fallback
      try_files /protected$uri /public$uri =404;
      error_page 404 /404;
    }

    location @error401 {
      internal;
      try_files /public$uri @unauth_404;
      error_page 404 /404;
    }

    location @unauth_404 {
      internal;
      try_files /public$uri =404;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://drob5swvtjqxg.cloudfront.net/user_avatar/community.nginx.org/system/32/19_2.png) [@system](https://community.nginx.org/u/system)\
**Post date:** [February 24, 2026, 11:34pm UTC](https://community.nginx.org/t/custom-404-pages-with-auth-request/8895/2 "2026-02-24T23:34:03Z")

</div>

Closed on February 24, 2026, 11:34pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
