Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
VGSources
Blog

How to Redirect an IP Address to a Domain in Nginx

A practical Nginx guide to redirecting IP-based requests to a canonical domain, with exact server blocks, path preservation, HTTPS certificate caveats, IPv6 support, testing commands, and troubleshooting.
WorldNginx Length9 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To redirect visitors who enter a server’s IP address, point the domain’s A and/or AAAA records at that server, then add an Nginx HTTP server block that catches the IP request and returns a redirect to the canonical domain. Use $request_uri when the original path and query string must survive the redirect:

server {
    listen 80 default_server;
    server_name 203.0.113.10;
    return 301 https://example.com$request_uri;
}

Replace the example address and hostname with your deployment values. Keep the application’s normal server block matched to the domain name. HTTPS needs separate treatment because certificate selection occurs before Nginx receives the HTTP request.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What the redirect does—and what DNS does not do

DNS and Nginx perform different jobs. An A record maps a hostname to an IPv4 address, while an AAAA record maps it to an IPv6 address. Neither record redirects a browser. After DNS resolves the hostname, the browser opens a connection to the address and sends an HTTP request. Nginx examines that request’s host value and can respond with a 301 redirect.

When someone types http://203.0.113.10/game/index.html?level=2, the HTTP Host header contains the literal IP address (and may include a port). Nginx can select a server block whose server_name matches that IP. The response then tells the browser to request the canonical hostname instead.

A permanent redirect is normally appropriate when the IP should never be the public URL. It lets browsers and crawlers update their stored destination. During a temporary migration or test, use a temporary status such as 302 or 307 instead; change to 301 only after the behavior is confirmed.

Prerequisites before editing Nginx

  • The domain’s A record points to the server’s public IPv4 address.
  • If the site is reachable over IPv6, the AAAA record points to the intended public IPv6 address and Nginx listens on IPv6.
  • You have administrative access to the Nginx configuration and permission to reload the service.
  • The canonical application server block already uses the domain name and serves the application correctly.
  • A certificate is installed for the domain if the destination uses HTTPS.

DNS changes can take time to appear according to the records’ existing caching policies. Test against the address and hostname that clients actually resolve, and confirm that no load balancer, firewall, or second web server is answering the IP before Nginx.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right redirect server block

Use an exact IP match when you know which address should redirect. Use a default catch-all when every otherwise-unmatched host on that listener should be sent away from the server.

Use case Server match Destination behavior Important consequence
One known IPv4 address server_name 203.0.113.10; Redirect to the domain, usually with $request_uri Other unmatched hostnames may be handled by another server block
One known IPv6 address An IPv6 literal in server_name Redirect requests arriving with that IPv6 host value Test IPv6 independently; IPv4 success does not prove IPv6 is configured
All unknown hosts on port 80 listen 80 default_server; and server_name _; Redirect every request that did not match another server block Do not let this block accidentally replace the domain’s intended HTTP server
Only the home page is valid Exact or catch-all match return 301 https://example.com; The original path and query string are discarded

Configure an IP-to-domain redirect

1. Confirm the addresses and canonical hostname

Write down the public IPv4 address, any public IPv6 address, and the exact hostname that should appear in the browser. Decide whether the destination is HTTP or HTTPS. If the canonical URL is HTTPS, use that scheme in the redirect rather than relying on a second hop.

2. Add a dedicated HTTP block

Place a small server block in the location used by your distribution for enabled Nginx sites. This example matches an IPv4 request and preserves the complete original URI:

server {
    listen 80;
    server_name 203.0.113.10;

    return 301 https://example.com$request_uri;
}

If this is the only HTTP server or must win when no other hostname matches, mark the listener as the default:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
server {
    listen 80 default_server;
    server_name 203.0.113.10;

    return 301 https://example.com$request_uri;
}

Do not put the application’s proxying, root, or rewrite rules in this block. Its purpose is to answer the wrong host immediately. Keep those application directives in the server block whose server_name is the real domain.

3. Decide whether to preserve the path

$request_uri contains the original path and arguments. A request for /save/slot-3?cloud=1 therefore becomes https://example.com/save/slot-3?cloud=1. This is the normal choice when equivalent routes exist on the domain.

For an IP that should always land on the home page, omit the variable:

server {
    listen 80 default_server;
    server_name _;

    return 301 https://example.com;
}

Use the home-page form only when reusing arbitrary paths would be confusing, unsafe, or incompatible with the application. A catch-all can receive requests for misspelled hostnames, scanners, and unrelated domains, so preserving every incoming URI is not always desirable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

4. Add IPv6 handling when an AAAA record exists

An IPv6 client can bypass an IPv4-only redirect if Nginx is not listening on the IPv6 socket. Add an IPv6 listener to the redirect block when the server has an IPv6 address:

server {
    listen 80;
    listen [::]:80;
    server_name 203.0.113.10 2001:db8::10;

    return 301 https://example.com$request_uri;
}

IPv6 literals in HTTP host headers are represented with brackets when a port is present. Nginx’s normal server-name matching handles the address form used by the request; verify the result with an actual IPv6 request rather than assuming the IPv4 block covers it.

5. Check the configuration and reload

  1. Run nginx -t. Continue only if Nginx reports that the syntax test is successful.
  2. Reload Nginx through the service manager used by the host, such as the system’s normal reload command. A reload applies the new configuration without needlessly terminating existing connections.
  3. If the syntax test fails, read the file and line number in the error. Common causes are a missing semicolon, a duplicate default listener, an invalid certificate path in another block, or an IPv6 listener unsupported by the host.

Configure HTTPS correctly

HTTP redirection cannot repair every direct-IP HTTPS request. TLS negotiation happens before the browser sends the HTTP request containing the Host header. Nginx must choose a certificate during that earlier handshake. When a browser visits https://203.0.113.10, the certificate for the domain usually does not match the literal IP, so the browser can show a certificate warning before Nginx has an opportunity to return a redirect.

For normal visitors, redirecting the HTTP IP URL to the HTTPS domain is sufficient. The browser reaches the domain during the redirect, sends the domain through SNI, and Nginx can select a certificate that covers that domain. If users must type an HTTPS IP URL without a warning, the certificate would need to cover that IP address, which is a separate certificate and deployment requirement; a domain certificate alone does not do so.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The canonical HTTPS server should explicitly name the domain and reference its certificate:

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # Application root, proxy_pass, or other site directives go here.
}

Do not assume that adding an HTTP redirect block creates an HTTPS virtual host. Port 443 requires its own listener, certificate, and application configuration.

return versus rewrite

For one host-wide redirect, return is the clearer directive. It immediately sends the selected status and URL from the server or location context:

return 301 https://example.com$request_uri;

A regex-based rewrite is useful when the destination depends on a pattern in the URI, but it adds matching rules that are unnecessary for a fixed host redirect. Nginx’s rewrite guidance recommends a separate redirect server and a direct return for this simple case. Avoid placing a broad rewrite inside the application’s canonical server block; doing so can create loops or redirect already-correct requests.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How server selection affects the result

Nginx first selects the listening address and port, then compares the request’s host value against the server_name values on that listener. A request using the literal IP can therefore select the IP-named block. If no name matches, Nginx uses the block marked default_server; if none is marked, it uses the first server declared for that address and port.

The documented catch-all pattern is:

server {
    listen 80 default_server;
    server_name _;
    return 301 https://example.com;
}

The underscore is a conventional value that does not represent a special wildcard by itself. The important behavior comes from the default listener and the absence of a more specific match. If your domain also has an HTTP server block, make sure that block is selected for the domain before the catch-all handles unknown hosts.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Verify the redirect without guessing

  1. Request the IP over HTTP with a path and query string, for example http://203.0.113.10/path?x=1.
  2. Inspect the response’s Location header. It should be https://example.com/path?x=1 when path preservation is intended.
  3. Request the canonical HTTPS URL directly and confirm that it serves the intended application instead of redirecting back to the IP or repeating the same redirect.
  4. If the server has an AAAA record, perform the same test over IPv6. A successful IPv4 response does not validate the IPv6 route.
  5. Check the browser address bar after following the redirect. The visible URL should contain the domain, not the numeric address.

For command-line inspection, a header-only request is useful because it exposes the status and destination without downloading the page:

curl -I 'http://203.0.113.10/path?x=1'

Look for a permanent redirect status and the expected Location value. If a proxy or CDN sits in front of Nginx, run a second test through the public hostname and compare it with a request that reaches the origin directly; an upstream device may be generating the response.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshooting common failures

The IP serves the application instead of redirecting

  • Confirm the request reaches the Nginx instance whose configuration you edited.
  • Check which block is default for port 80. Another file may declare default_server first or may be listening on a different address.
  • Ensure the IP in server_name is the public address clients use, not a private interface address.
  • Search for another web server, load balancer, container ingress, or firewall performing NAT before Nginx.

The path disappears or query parameters change

Inspect the return target. A target ending at the hostname, such as https://example.com, intentionally sends every request to the home page. Append $request_uri for path and argument preservation. Do not append an extra question mark or manually rebuild the query string; that can create malformed or duplicated arguments.

The redirect loops

  • Check that the canonical HTTPS server does not redirect the domain back to the IP.
  • Review proxy or load-balancer headers if TLS terminates upstream. An application that does not recognize the original scheme may redirect an already-secure request to HTTP, causing a loop with another rule.
  • Test the final domain URL independently. It must serve content without passing through the IP redirect block.

The browser reports a certificate error for the IP

This is expected for a direct HTTPS IP request when the certificate covers only the domain. Test the HTTP IP URL, which can redirect before TLS, or visit the canonical HTTPS hostname. If direct-IP HTTPS is a hard requirement, obtain and configure a certificate whose names include the IP and understand that this is distinct from the ordinary domain certificate.

Only some users are redirected

Compare IPv4 and IPv6 resolution. A domain with an AAAA record may send some clients to an IPv6 listener that lacks the redirect. Also check cached permanent redirects in browsers and intermediate caches while testing changes; use a temporary status during trials if you need to avoid making an early redirect sticky.

Operational checklist

  • DNS A and AAAA records point to the intended server.
  • The application’s canonical server block matches the domain, not the numeric address.
  • The IP redirect block listens on the correct port and address family.
  • return 301 https://example.com$request_uri; is used when routes and query strings should be retained.
  • The home-page form without $request_uri is used only when discarding the original URI is deliberate.
  • The HTTPS domain block has a certificate and key that cover the hostname.
  • nginx -t succeeds before every reload.
  • HTTP IP, HTTPS domain, path/query preservation, and IPv6 have each been tested.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.