file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

When you encounter a failed to open stream error specifically when calling an endpoint on your own server from within that same server, it is almost never a PHP configuration issue or a firewall blocking outbound traffic. Standard connectivity checks, like pinging Google or fetching external APIs, working while internal calls fail points directly to how the local web server handles loopback traffic versus external requests.

The scenario described involves moving a site to a new domain and IP address using Plesk, resulting in multiple SSL certificates and potentially multiple IP addresses on a single machine. This setup often triggers a specific behavior known as Localhost Resolution Ambiguity. When your application attempts to fetch https://mydomain.com from the server itself, PHP's file_get_contents (or the underlying cURL stack) resolves the hostname. If the system DNS configuration returns multiple IP addresses for that domain, or if the web server binds to multiple interfaces with different virtual hosts active, the local request might resolve to a specific IP that serves a default welcome page rather than your application's virtual host.

This explains why accessing / from the server itself yields the Plesk "this is a new domain" page instead of your actual site content. The web server (Apache or Nginx) is likely responding with a generic catch-all configuration for one of the IPs, while external requests correctly resolve to the IP associated with your specific virtual host definition. This mismatch causes the application to receive a 404 Not Found because it expects the script logic but receives a static error page or a redirect that doesn't match the expected response.

Why Same-Server Requests Fail

The core issue lies in how operating systems resolve hostnames. When you run file_get_contents('https://mydomain.com'), the system performs a DNS lookup. If your /etc/hosts file (on Linux) or Windows hosts file contains an entry for mydomain.com pointing to 127.0.0.1 or localhost, but your web server is not configured to serve that domain on the loopback interface, you get unexpected results.

In environments with multiple IP addresses and SSL certificates, the local resolution might default to the first available IP in the DNS record, which could be an external IP managed by a load balancer or a separate management interface that serves a default Plesk landing page. External requests bypass this ambiguity because they resolve to the public IP explicitly configured for your domain's virtual host.

To verify if this is the culprit, check how your system resolves the hostname locally:

# Linux example
getent hosts mydomain.com

# Or use nslookup from within the server
nslookup mydomain.com

If these commands return an IP address that is not the one serving your actual application (e.g., they return the management IP or a generic Plesk host), you will encounter this 404 error. The local web server receives the request on a different interface than it expects for that virtual host, triggering a fallback response.

Solutions and Best Practices

The most robust solution is to stop resolving your domain name via DNS inside your application when making internal requests. Instead of using the hostname, resolve the specific IP address associated with your application's virtual host or use localhost if the server binds correctly to 127.0.0.1.

However, relying on IP addresses can be brittle if you have multiple IPs. A cleaner approach within PHP is to disable DNS lookups for local requests by explicitly setting the host to localhost and ensuring your virtual host configuration includes a server block or location directive that listens on the loopback interface:

// Prefer localhost for internal calls to avoid ambiguous resolution
$url = 'http://localhost/path/to/script';
$html = file_get_contents($url);

Alternatively, you can configure your web server to handle local requests consistently. In Apache, ensure there is no generic <VirtualHost *:80> or <VirtualHost *:443> that catches traffic intended for your specific domain. You should have a dedicated virtual host block for mydomain.com that listens on all interfaces (*) or specifically on the IP assigned to your application logic.

If you are using Laravel, which often handles HTTP clients via Illuminate\Support\Facades\Http, ensure you aren't inadvertently resolving the domain within the framework's internal resolver when making local requests. Disabling DNS lookups globally for specific contexts or forcing the use of http://127.0.0.1 can mitigate these resolution issues effectively.

Final Considerations

This issue is a classic example of why applications should not rely on hostname resolution for internal communication when multiple network interfaces or IP addresses are involved. The discrepancy between how external clients see your site and how the local OS resolves it creates a fragile dependency on DNS records that may point to different IPs than expected. By forcing the use of localhost or resolving the correct external IP explicitly, you bypass the ambiguity inherent in multi-IP server environments.

Our network