Monday, February 16, 2026

Run apache and nginx on same server

AI Overview

Common Configuration Methods
 
You have several options to run both servers simultaneously:
  • Different Ports: This is the simplest method. Configure one server (e.g., Apache) to listen on a non-standard port like 8080 or 81, while the other (e.g., Nginx) uses the default 80 (HTTP) and 443 (HTTPS) ports. Users would access the second server by appending the port number to the URL (e.g., http://yourdomain.com:8080).
  • Different IP Addresses: If your server has multiple IP addresses, you can bind each web server to a unique IP address on the same port (80/443).
  • Nginx as a Reverse Proxy (Recommended): This leverages the strengths of both servers. Nginx (known for speed and efficiency in static content and high concurrency) listens on the public ports 80 and 443. It serves static files directly and proxies requests for dynamic content (like PHP applications that rely on .htaccess files) to Apache, which is configured to listen on an internal, loopback IP and port (e.g., 127.0.0.1:8080). This setup improves overall performance and resource usage.
Steps for a Reverse Proxy Setup (Nginx in front of Apache)
 
A typical configuration involves these general steps (specific file paths may vary by operating system):
  1. Configure Apache to use a different port: Edit the Apache configuration files (ports.conf and the default virtual host file) to change the listening ports from 80/443 to an internal port like 8080.
  2. Configure Nginx as a reverse proxy: Set up Nginx to listen on the public ports 80/443. For dynamic requests, add proxy directives within the Nginx virtual host configuration to forward traffic to the Apache port (e.g., proxy_pass http://localhost:8080;).
  3. Install mod_rpaf (or mod_remoteip) on Apache: This module ensures Apache logs the client's real IP address instead of Nginx's localhost IP, which is useful for statistics and authentication.
  4. Restart services: After configuration changes, restart both Nginx and Apache to apply the new settings.
This combined approach allows you to benefit from Nginx's performance for static content while still using Apache for applications that require its specific features.

No comments:

Blog Archive

Followers