How to Configure Apache Nginx: A Step-by-Step Reverse Proxy Setup

Written by

in

TL;DR: “Apache Nginx” usually means running Nginx as a front-facing reverse proxy that forwards requests to an Apache backend. Install both, point Nginx’s proxy_pass at Apache’s listening port, then restart both services.

Step 1: Install Both Servers

On Debian/Ubuntu, run sudo apt install nginx apache2. On RHEL-based systems, use dnf install nginx httpd. Nginx will grab port 80 by default, so plan to move Apache off it.

If you want to dig deeper, check out our guide on Top 10 Ergonomic Office Chairs for Back Pain Relief.

Step 2: Move Apache to a Backend Port

Edit /etc/apache2/ports.conf (or /etc/httpd/conf/httpd.conf) and change Listen 80 to Listen 8080. Update any <VirtualHost *:80> blocks to match. Restart Apache with sudo systemctl restart apache2.

Step 3: Create the Nginx Proxy Block

Add a server block in /etc/nginx/sites-available/ (or conf.d) containing:

server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }

Step 4: Test and Reload

Run sudo nginx -t to validate syntax, then sudo systemctl reload nginx. Visit your domain; Nginx should serve the response Apache generates.

Tips

Enable mod_remoteip in Apache so logs show real client IPs. Add caching or gzip in Nginx for static assets to offload Apache. Keep both servers patched, and use TLS termination at Nginx for simpler certificate management.

FAQ

Q: Why put Nginx in front of Apache?
A: Nginx handles static files and many concurrent connections efficiently, while Apache keeps its rich module ecosystem for dynamic content.

Q: Do I need to change Apache’s port?
A: Yes, unless Nginx listens elsewhere. Both cannot bind the same port on the same interface.

Q: How do I verify the proxy is working?
A: Check Nginx access logs for 200 responses and confirm Apache’s logs show requests originating from 127.0.0.1.

Related Articles

Comments

One response to “How to Configure Apache Nginx: A Step-by-Step Reverse Proxy Setup”

  1. […] If you want to dig deeper, check out our guide on How to Configure Apache Nginx: A Step-by-Step Reverse Proxy . […]

Leave a Reply

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