This guide shows you how to configure Nginx so that when someone visits your server’s IP address directly, they are automatically redirected to your domain name. You’ll set up a simple server block that issues a 301 permanent redirect, which also helps with SEO by consolidating your site’s traffic under one canonical address. Before you start, you’ll need root or sudo access to your server, Nginx already installed, and a domain name pointing to your server’s IP.
What You’ll Need
- A Linux server with Nginx installed (Ubuntu, Debian, CentOS, etc.)
- Root or sudo access to edit Nginx configuration files
- A registered domain name that resolves to your server’s public IP address
- Basic familiarity with the command line and text editors (like nano or vim)
Step-by-Step
- Find your server’s IP address. Run
curl ifconfig.meorhostname -Ito see the public IP. You’ll use this in the server block. - Locate your Nginx configuration directory. Common paths are
/etc/nginx/sites-available/(Debian/Ubuntu) or/etc/nginx/conf.d/(CentOS/RHEL). Check your mainnginx.confto see which is included. - Create a new configuration file for the redirect. For example,
/etc/nginx/sites-available/ip-redirect. Use a name that’s easy to recognize. - Edit the file and add the following server block. Replace
yourdomain.comwith your actual domain (includingwwwif you use it):server { listen 80; listen [::]:80; server_name 203.0.113.5; # Replace with your server's IP return 301 http://yourdomain.com$request_uri; }This block listens on port 80 for requests that use the IP as the host. It then issues a 301 redirect to your domain, preserving the original request path.
- Save and exit the editor. In nano:
Ctrl+O,Enter,Ctrl+X. In vim::wq. - Enable the configuration. On Debian/Ubuntu, create a symlink:
sudo ln -s /etc/nginx/sites-available/ip-redirect /etc/nginx/sites-enabled/. On CentOS/RHEL, the file is already active if placed inconf.d. - Test the configuration. Run
sudo nginx -t. You should see a message like “syntax is ok” and “test is successful”. If there are errors, fix them before continuing. - Reload Nginx to apply the changes:
sudo systemctl reload nginx(orsudo service nginx reloadon older systems). - Verify the redirect. Open a browser and visit
http://your-server-ip. You should be taken to your domain. You can also usecurl -I http://your-server-ipfrom the command line; the response should include301 Moved Permanentlyand aLocationheader pointing to your domain.
Common Mistakes
- Using the wrong IP address. Make sure you use the public IP that your domain resolves to, not a private IP like 127.0.0.1 or 192.168.x.x.
- Forgetting the
server_namedirective. If you omit it, Nginx might apply the redirect to all requests, including those for your domain, causing a redirect loop. - Not preserving the request URI. If you use
return 301 http://yourdomain.com;without$request_uri, visitors tohttp://IP/some/pagewill be sent to the homepage instead of the same page on your domain. - Editing the wrong configuration file. Some servers have a default site that may conflict. Ensure your IP redirect block is not being overridden by another server block with a catch-all
server_name _;. - Not testing before reloading. A syntax error can take Nginx down. Always run
nginx -tfirst.
Tips
- Use HTTPS. If your site uses SSL, redirect the IP to the HTTPS version of your domain:
return 301 https://yourdomain.com$request_uri; - Redirect both IP and non-www to www (or vice versa). You can add another server block for the non-www version to enforce a single canonical domain.
- Consider IPv6. If your server has an IPv6 address, add a separate
listen [::]:80;line as shown, and use the IPv6 address in a second server block if needed. - Keep the redirect separate. Putting the IP redirect in its own file makes it easier to remove or modify later without touching your main site config.
- Use a 301 for permanent redirects. This tells search engines to update their index to the new URL. Use 302 only if the redirect is temporary.
Troubleshooting
- Redirect not working. Check that Nginx is running:
sudo systemctl status nginx. Then verify the configuration file is enabled and theserver_namematches your IP exactly (no trailing dots). - Getting a redirect loop. This usually means your domain’s server block is also redirecting. Ensure the IP redirect only applies to requests where the host is the IP, and that your domain block does not redirect back to the IP.
- Seeing the default Nginx page instead. Your IP redirect might be missing or not enabled. Check the
sites-enableddirectory (if you use it) and confirm the symlink exists. - Redirect works but HTTPS breaks. If you have SSL configured, you need to handle port 443 as well. Add a similar server block listening on 443 with the SSL certificates, or redirect to HTTPS from port 80.
- IP still loads the site. Some browsers or CDNs may cache the old response. Try a different browser or use
curl -Ito see the raw headers. Also, clear your browser cache. - Nginx fails to reload. Run
sudo nginx -tto see the exact error. Common issues include missing semicolons or an invalid IP address inserver_name.
Frequently Asked Questions
- Will this redirect affect my email or other services? No, Nginx only handles HTTP/HTTPS traffic. Email (SMTP, POP3, IMAP) uses different ports and is unaffected.
- Can I redirect from an IP to a specific path? Yes, modify the redirect URL:
return 301 http://yourdomain.com/special-page;. But this will send all IP visitors to that page, regardless of the original path. - What if I have multiple domains on the same server? The IP redirect will only apply to requests that use the IP as the host. Your other domains will still work normally.
- Is it possible to redirect both HTTP and HTTPS? Yes, you need separate server blocks for ports 80 and 443. For 443, you’ll need to include the SSL certificates and use
return 301 https://yourdomain.com$request_uri;. - How do I undo the redirect? Simply remove or disable the configuration file (e.g., delete the symlink in
sites-enabled) and reload Nginx. - Will this affect my SEO? A 301 redirect passes most of the link equity to the new URL, so it’s actually beneficial for SEO to consolidate your IP traffic to your domain.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It