J Del
J Del

Reputation: 881

Nginx - show a specific page if IP is not whitelisted

I want to have a whitelist of IPs, and if your IP is not on the list then it serves comingsoon.html to you.

How can I achieve this?

At the moment I have the whitelist set up, but I don't know how to serve a specific page to IPs that aren't on the whitelist

This is what I have at the moment for my server block:

server {
        listen 80;

        root /var/www/public;
        index index.php;

        server_name example.dev 192.168.33.10;

        error_page 404 /404.php;

        location / {
        allow 10.0.1.26;
        deny all;
        }

        location ~ \.php$ {
        try_files $uri =404;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        }

        location ~ /\.ht {
        deny all;
        }
}

Upvotes: 2

Views: 1005

Answers (1)

user325117
user325117

Reputation:

You can use the error_page directive for 403 Forbidden:

error_page 403 /comingsoon.html;

If you also want to change the response code to 200 OK:

error_page 403 =200 /comingsoon.html;

Reference: error_page

Upvotes: 3

Related Questions