Understanding Proxy Services
A proxy acts as an intermediary between clients and servers. In networking, proxies are commonly used to forward requests while providing control, security, or performance benefits. There are two primary types of proxies: - Forward Proxy: Serves client-side needs. For example, when a client within a restricted network uses a proxy to access external resources (e.g., bypassing geo-restritcions or accessing the internet through a gateway).
- Reverse Proxy: Serves server-side needs. It receives client requests and forwards them to backend servers, often used for load balancing, SSL termination, or hiding internal architecture.
The key distinction lies in who the proxy serves: forward proxies assist clients, while reverse proxies assist servers. ### Nginx Proxy Capabilities
Nginx supports multiple protocols as a reverse proxy, including HTTP, HTTPS, FastCGI, gRPC, and more. However, as a forward proxy, Nginx has limited support—it primarily handles HTTP and does not naitvely support HTTPS tunneling without additional modules like ngx_http_proxy_connect_module. ### Basic Reverse Proxy Configuration
The core directive for reverse proxying in Nginx is proxy_pass: ```
Syntax: proxy_pass URL;
Context: location, if in location, limit_except
**Example Scenario:** Expose an internal service running on port 8080 via a public endpoint on port 80. 1. Create two static HTML files:
- `/opt/app/code1/http_proxy.html`: Public-facing content
- `/opt/app/code2/http_proxy_redirect.htm`: Internal-only content
2. Configure Nginx:
Public-facing server block: ```
server {
listen 80;
server_name www.zhangbiao.com;
location ~ /test_proxy\.html$ {
proxy_pass http://127.0.0.1:8080;
}
}
Internal service block: ``` server { listen 8080; server_name 127.0.0.1;
location / {
root /opt/app/code2;
index http_proxy_redirect.htm;
}
}
Now, accessing `http://www.zhangbiao.com/test_proxy.html` will fetch content from the internal service on port 8080. ### Advanced Proxy Directives
- **Buffering**:
- `proxy_buffering on|off;` – Enables response buffering (default: on)
- `proxy_buffer_size 32k;` – Size of buffer for response headers
- `proxy_buffers 4 128k;` – Number and size of buffers for response body
- `proxy_busy_buffers_size 256k;` – Max buffer size actively sending data
- **Header Manipulation**:
- `proxy_set_header Host $http_host;`
- `proxy_set_header X-Real-IP $remote_addr;`
- `proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;`
- `proxy_hide_header HeaderName;` – Removes specific headers from response
- **Timeouts**:
- `proxy_connect_timeout 30s;` – Time to establish connection to backend
- `proxy_send_timeout 60s;` – Time to send request to backend
- `proxy_read_timeout 60s;` – Time to read response from backend
- **Redirection Handling**:
- `proxy_redirect default;` – Rewrites `Location` and `Refresh` headers in backend responses
### Recommended Proxy Configuration Template
A robust and reusable proxy setup often includes a shared parameter file (e.g., `proxy_params`): ```
# /etc/nginx/proxy_params
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
Then include it in your location blocks: ``` location / { proxy_pass http://backend_service; include proxy_params; }
This approach promotes consistency, reduces duplication, and simplifies maintenance across multiple proxy configurations.