Proxy Pass Configuration Examples
When configuring Nginx as a reverse proxy, the behavior of the proxy_pass directive can vary depending on the presence of a trailing slash in the URL.
Example 1: Proxy Pass with Trailing Slash
location ^~ /abc/ {
proxy_pass http://192.168.1.1:8080/;
}
- A request to
http://www.a.com/abc/a.htmlwill be forwarded tohttp://192.168.1.1:8080/a.html - The trailing slash in
proxy_passremoves the/abcprefix from the request URI
Example 2: Proxy Pass without Trailing Slash
location ^~ /abc/ {
proxy_pass http://192.168.1.1:8080;
}
- A request to
http://www.a.com/abc/a.htmlwill be forwarded tohttp://192.168.1.1:8080/abc/a.html - The original request URI is appended to the proxy URL
HTTP Request Headers Overview
HTTP headers are key-value pairs sent in the request and response messages of the HTTP protocol. These headers provide metadata about the request or response and help control the behavior of web servers and clients.
Common Request Headers
Accept- Specifies the content types the client can handleAccept-Charset- Indicates supported character setsAccept-Encoding- Lists supported content encodings (e.g., gzip)Accept-Language- Preferred languages for the responseAuthorization- Credentials for HTTP authenticationCache-Control- Directives for caching mechanismsConnection- Controls network connection behavior (e.g., keep-alive)Cookie- Client-side stored session dataContent-Length- Size of the request body in bytesContent-Type- Media type of the request bodyDate- Timestamp of requestHost- Target domain name or IP address
Custom Headers in Reverse Proxy Configurations
When using Nginx as a reverse proxy, you can set custom headers using the proxy_set_header directive:
location / {
proxy_pass http://backend;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
Common Proxy Headers
X-Forwarded-For- Identifies the originating IP address of a clientX-Forwarded-Proto- Informs the backend about the original protocol (http/https)X-Real-IP- Passes the client's real IP address to the backend
Common Pitfall: Missing X-Forwarded-Proto Header
When terminating SSL at the Nginx layer and forwarding requests to backend servers, it's crucial to set the X-Forwarded-Proto header:
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
- Failing to set this header can cause issues with applications that enforce HTTPS connections
- In some cases, this can lead to infinite redirect loops or unexpected behavier
Advanced Proxy Configuration Example
Multiple upstream servers with different routing rules:
upstream static_servers {
server 192.168.10.173:80;
server 192.168.10.174:80;
}
upstream app_servers {
server 192.168.10.173:8080;
server 192.168.10.174:8080;
}
server {
listen 80;
server_name grace.kevin.com;
location / {
proxy_pass http://static_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /leasecore/ {
proxy_pass http://app_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /cms/api/ {
proxy_pass http://app_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}