Nginx-Always return 403 status code

http-status-codehttp-status-code-403nginx

I'm trying to get Nginx to return a simple webpage, but always with an http status code of 403. For some reason, I just can't get it to work. I have two files in /var/www/html/:

  • index.html
  • photo.jpg

index.html displays some text along with photo.jpg

Here is my Nginx config:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                return 403;
         }

        error_page 404 /index.html;
        error_page 403 /index.html;

        location = /index.html {
                 allow all;
       }

        location = /photo.jpg {
                allow all;
        }
}

With this config as-is, both index.html and photo.jpg are displayed with a status of 200. If I remove the location blocks for those two files, neither are displayed, but they have a status of 403.
My question is: How can I display the html file, along with the image, but just return a status of 403? I feel like I'm missing something simple.

Best Answer

Your location / {}, containing a return 403; matches first. You should move it below two others.

Happy leg shooting !