When implementing domain proxying in Nginx, many developers face the requirement to forward traffic from one domain (e.g., connect3.domain.ru) to another (e.g., connect2.domain.ru) while keeping the original URL visible in the browser's address bar. This is different from a simple redirect or rewrite operation.
The key lies in proper proxy_pass configuration with correct header handling. Here's the working solution:
server {
listen 80;
server_name connect3.domain.ru www.connect3.domain.ru;
location / {
proxy_pass http://connect2.domain.ru;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Optional but recommended settings
proxy_redirect off;
proxy_buffering off;
proxy_http_version 1.1;
}
}
The magic happens through these specific directives:
- proxy_pass: Points to the target domain
- proxy_set_header Host $host: Preserves the original hostname
- proxy_redirect off: Prevents URL rewriting in redirect responses
For production environments, consider adding these enhancements:
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
Watch out for these issues:
- Mixed Content: Ensure your target server generates relative URLs or properly handles the proxied domain
- Cookie Domains: The backend application might need configuration to handle the proxy domain
- WebSocket Support: Requires additional proxy headers as shown above
After implementing, verify with:
nginx -t && service nginx reload
Then test with curl to check headers:
curl -I http://connect3.domain.ru
When implementing domain proxying in Nginx, maintaining the original URL in the browser's address bar while serving content from a different domain requires careful configuration. The key is to use proxy_pass
correctly while preserving request headers.
Here's the corrected Nginx configuration that achieves seamless domain proxying while keeping the original URL:
server {
listen 80;
server_name connect3.domain.ru www.connect3.domain.ru;
access_log /var/log/nginx/connect3.domain.ru.access.log;
error_log /var/log/nginx/connect3.domain.ru.error.log;
location / {
proxy_pass http://connect2.domain.ru;
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;
# Optional: Handle redirects properly
proxy_redirect http://connect2.domain.ru/ http://$host/;
}
}
The critical components that make this work:
proxy_set_header Host $host
- Preserves the original hostname- No trailing slash in
proxy_pass
URL - Maintains URI structure proxy_redirect
- Adjusts any Location headers in responses
For HTTPS connections, add these additional parameters:
server {
listen 443 ssl;
server_name connect3.domain.ru;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass https://connect2.domain.ru;
proxy_ssl_server_name on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
After implementing the configuration:
- Test with
curl -v http://connect3.domain.ru
to verify headers - Check Nginx error logs for any proxy-related issues
- Verify the response contains the expected content with original hostname