Skip to main content

Restricting Access and GeoIP Routing

Learn how to restrict access to parts of your WordPress site using IP addresses, HTTP Basic Authentication, or country codes (X-Seravo-Geo-Country-Code), and understand Seravo's automatic rate limiting.

WordPress provides robust tools for user management and content protection. We recommend using WordPress's native features as your primary method of access control—such as password-protecting individual pages, assigning user roles, or enabling two-factor authentication (2FA).

If you require server-level restrictions or country-based routing, you can use the methods outlined below in Seravo's environment.

Restricting Access by IP Address

If your site hosts static files or directories (such as internal documents) that should only be accessible to visitors from a specific network, you can restrict access based on IP addresses.

Create a new configuration file at /data/wordpress/nginx/ip-restrict.conf:

location ^~ /restricted-section/ {
allow 1.2.3.4;
allow 5.6.7.8;
deny all;
}

Apply the configuration by restarting nginx:

wp-restart-nginx

Warning: Never apply IP restrictions across the entire site (location /). Blocking site-wide access breaks Seravo's automated monitoring and prevents maintenance operations. If you only want to restrict access to the admin dashboard, see Restrict wp-admin Access by IP Address.

Keep in mind that IP addresses can change (especially on mobile networks), and IP-based restrictions do not create an individual user audit trail.

Country-Based Access Control and Redirects (GeoIP)

Seravo's server environment adds an X-Seravo-Geo-Country-Code header to incoming HTTP requests containing the visitor's two-letter ISO country code based on their IP address (e.g., FI, SE, US). You can use this header to route visitors to localized site versions or block traffic from specific countries.

Option 1: Country-Based Redirects or Blocking via nginx (Recommended)

Handling redirects at the server level is the fastest approach and does not load PHP or interfere with caching. Add your rules to a file such as /data/wordpress/nginx/geo-redirect.conf:

# Redirect visitors from Finland to example.fi
if ($http_x_seravo_geo_country_code = "FI") {
return 302 https://example.fi$request_uri;
}

# Redirect visitors from Sweden to example.se
if ($http_x_seravo_geo_country_code = "SE") {
return 302 https://example.se$request_uri;
}

# Block traffic from a specific country entirely (403 Forbidden)
if ($http_x_seravo_geo_country_code = "XX") {
return 403;
}

Apply the changes by running wp-restart-nginx.

Tip: Use temporary 302 redirects rather than permanent 301 redirects for GeoIP routing so browsers do not permanently cache the redirect on traveling users' devices.

Option 2: Country-Based Redirects in PHP

If you need more complex conditions (such as checking cookies or user login status), you can read the header in PHP via $_SERVER['HTTP_X_SERAVO_GEO_COUNTRY_CODE']:

<?php
if ( isset( $_SERVER['HTTP_X_SERAVO_GEO_COUNTRY_CODE'] ) ) {
$country = strtoupper( $_SERVER['HTTP_X_SERAVO_GEO_COUNTRY_CODE'] );

switch ( $country ) {
case 'FI':
header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' );
header( 'Location: https://example.fi', true, 302 );
exit;

case 'SE':
header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' );
header( 'Location: https://example.se', true, 302 );
exit;
}
}

Note on Caching: When performing GeoIP redirects in PHP, always send Cache-Control: no-store, no-cache headers before the Location header. Otherwise, the HTTP cache may store the redirect and route visitors from other countries to the wrong destination.

HTTP Basic Authentication

HTTP Basic Authentication works well as a simple layer of protection for static files and directories. If you need to protect actual WordPress pages or posts, use WordPress's built-in password protection or a dedicated access-control plugin instead.

First, create a password file using the htpasswd utility. Only use the -c flag when creating the file for the first user (using it again will overwrite the existing file):

# Create a new file and add the first user
htpasswd -bc /data/wordpress/nginx/.htpasswd user1 password1

# Add a second user to the same file (omit the -c flag)
htpasswd -b /data/wordpress/nginx/.htpasswd user2 password2

Next, enable authentication for the target directory path by creating /data/wordpress/nginx/htauth.conf:

location ^~ /restricted-section/ {
auth_basic "Restricted Area";
auth_basic_user_file /data/wordpress/nginx/.htpasswd;
}

Apply the configuration with wp-restart-nginx.

Warning: Never enable HTTP Basic Authentication across the entire site (location /), as it blocks Seravo's automated site monitoring. If you want to hide a site under development from public view, see Is My Ordered Site Immediately Public and Hiding a Site Under Construction.

Restricting Dynamic WordPress Pages with PHP

Because WordPress pretty permalinks are routed through index.php, server-level directory rules do not protect individual WordPress pages or custom post types.

To restrict a specific page or section to logged-in users in your theme's functions.php file or a custom plugin, use the template_redirect hook. Be sure to call nocache_headers() before redirecting so Seravo's HTTP cache does not store the redirect response:

<?php
add_action( 'template_redirect', function () {
// Do not restrict CLI commands or background cron jobs
if ( wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}

// Require login for the /intranet/ page
if ( is_page( 'intranet' ) && ! is_user_logged_in() ) {
nocache_headers();
auth_redirect(); // Redirects to login and returns the user afterward
}
} );

If you want to restrict access to a specific user role in addition to requiring login, replace ! is_user_logged_in() with a capability check such as ! current_user_can( 'edit_posts' ) and redirect unauthorized users to the homepage using wp_safe_redirect( home_url() ); exit;.

Seravo's Automatic Rate Limiting

To ensure security and server stability, Seravo's environment enforces automatic request rate limiting (PHP flood protection). These thresholds are tuned so they never interfere with normal site usage.

Rate limiting only triggers if all of the following conditions are met simultaneously:

  1. The site receives more than 200 HTTP requests per minute.

  2. The requests hit dynamic WordPress/PHP processes (rather than cached pages).

  3. All requests originate from the same IP address.

When this threshold is exceeded, the server returns a 429 Too Many Requests status code and logs a notice in /data/log/nginx-error.log. This limit does not apply to static assets (images, CSS, JS) or pages served from the HTTP cache.

Common Issues and Troubleshooting

  • The site returns 429 Too Many Requests during an integration or plugin task: An external API call, bulk image import, or cache preloader is making over 200 PHP requests per minute from a single IP address. Inspect /data/log/nginx-access.log to identify the source and URLs, and reduce the request rate.

  • A WordPress page is not protected by an nginx Basic Auth rule: WordPress pretty permalinks are routed through index.php, so directory-level nginx rules do not apply to virtual WordPress URLs. Use WordPress's built-in password protection or a membership/access-control plugin for dynamic pages.

  • Changes to nginx settings do not take effect: Always run wp-restart-nginx after modifying configuration files, and clear the cache with wp-purge-cache if necessary.

  • GeoIP routing sends all visitors to the same country version: If you use PHP-based GeoIP redirects without a Cache-Control: no-store header, the HTTP cache stores the redirect served to the first visitor. Add cache-control headers to your PHP code or move the rule to nginx.

See Also

Properly configured access restrictions safeguard sensitive areas of your site and route international visitors to the right language version without unnecessary overhead. If you need assistance setting up nginx rules or investigating 429 rate limits, our support team is happy to help at [email protected].

Did this answer your question?