When configuring Nginx to redirect from an IP address to a domain name, a common pitfall is creating an infinite redirect loop. This occurs when the server configuration doesn't properly distinguish between requests coming via IP versus domain name.
The problematic configuration looks like this:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name isitmaintained.com;
...
}
server {
listen 178.62.136.230:80;
server_name 178.62.136.230;
add_header X-Frame-Options "SAMEORIGIN";
return 301 $scheme://isitmaintained.com$request_uri;
}
The issue stems from how Nginx processes the server_name
directive and the default server behavior. When a request comes in, Nginx:
- First matches the IP/port combination
- Then looks for the best
server_name
match - If no match found, uses the default server
Here's the corrected configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 http://isitmaintained.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name isitmaintained.com;
# Your actual website configuration here
...
}
The fixed version includes these crucial changes:
- Uses
_
as a catch-all server name for IP requests - Explicitly marks the IP listener as
default_server
- Separates the domain configuration completely
- Removes the explicit IP binding that was causing conflicts
After making changes, always test with:
sudo nginx -t
sudo systemctl reload nginx
Then verify using curl:
curl -I http://178.62.136.230
curl -I http://isitmaintained.com
For production environments, consider adding:
- HTTPS redirects
- HSTS headers
- Proper caching headers for redirects
- Monitoring for redirect chains
When attempting to redirect traffic from an IP address to a domain name in Nginx, many developers encounter redirect loops. Here's a typical problematic configuration:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name isitmaintained.com;
# Other configurations...
}
server {
listen 178.62.136.230:80;
server_name 178.62.136.230;
add_header X-Frame-Options "SAMEORIGIN";
return 301 $scheme://isitmaintained.com$request_uri;
}
The fundamental issue lies in how Nginx processes server blocks. When a request comes to the IP address, Nginx first matches the IP-based server block, then redirects to the domain. However, if the domain request somehow gets routed back to the IP address (common in shared hosting or certain DNS setups), the cycle repeats indefinitely.
Here's the corrected configuration that prevents the redirect loop:
server {
listen 80;
listen [::]:80;
server_name 178.62.136.230;
return 301 http://isitmaintained.com$request_uri;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name isitmaintained.com www.isitmaintained.com;
# Your actual website configuration here
root /var/www/isitmaintained;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
- Explicitly separated IP and domain configurations
- Used default_server only for the domain configuration
- Added proper root and index directives for the domain server
- Included www subdomain for completeness
After making changes, always test with:
sudo nginx -t
sudo systemctl restart nginx
Then verify using curl:
curl -I http://178.62.136.230
Should return a 301 with the correct Location header pointing to your domain.
For HTTPS redirection, you'll need to modify the approach:
server {
listen 443 ssl;
server_name 178.62.136.230;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
return 301 https://isitmaintained.com$request_uri;
}
Remember to handle HTTP to HTTPS redirection separately if needed.