🌐 Configuring Nginx as a Forward Proxy
🚀 What is a Forward Proxy?
A forward proxy is an intermediary server that sits between client requests and the internet. It helps with:
- Anonymity 🕵️♂️
- Security 🔐
- Traffic control 🚦
🛠️ Forward Proxy Configuration
📌 Basic Forward Proxy (Port 8080)
server {
listen 8080;
server_name test.asifahmadkhan.com;
location / {
proxy_pass $scheme://$host$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
resolver 8.8.8.8; # 🌍 Using Google's public DNS
proxy_connect_timeout 5s;
proxy_read_timeout 10s;
proxy_send_timeout 10s;
}
# ✅ Enable CONNECT method for HTTPS proxying
location /connect {
proxy_pass https://$host;
proxy_connect;
proxy_set_header Host $host;
}
}
📝 Explanation:
🔹 proxy_pass $scheme://$host$request_uri; → Forwards requests dynamically based on the request's scheme (HTTP/HTTPS) and host.
🔹 resolver 8.8.8.8; → Uses Google's DNS resolver to handle domain name resolution.
🔹 proxy_connect_timeout, proxy_read_timeout, proxy_send_timeout; → Configures timeouts to optimize performance.
🔄 Forwarding Requests from abc.test.com
to laptop.asif.com
server {
listen 80;
server_name abc.test.com;
location / {
proxy_pass http://34.234.220.11; # 🔀 Forward to laptop.asif.com
proxy_set_header Host laptop.asif.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 10s;
proxy_read_timeout 20s;
proxy_send_timeout 20s;
}
}
📝 Explanation:
🔹 server_name abc.test.com; → Listens for requests to abc.test.com
.
🔹 proxy_pass http://34.234.220.11; → Forwards requests to laptop.asif.com
.
🔹 proxy_set_header Host laptop.asif.com; → Ensures the correct host header is forwarded.
🎯 Why Use a Forward Proxy?
✅ Security & Privacy - Protects client IPs from exposure.
✅ Traffic Control - Allows filtering and logging of traffic.
✅ Caching - Can improve performance by caching responses.
🚀 Now, you're ready to set up your forward proxy with Nginx! 🔥