
Posted on
Nginx as Reverse Proxy
Reverse Proxy
A reverse proxy sits in front of a server and can
- Act as Load Balancer
- Protect the origin servers from attacks by hiding it's IP addresses
- Cache content for faster performance
- Provide SSL encryption and decryption to and from the server
Directive Needed
Nginx usesproxy_pass
Directive for configuring reverse proxy.
- Syntax: proxy_pass
- Request:https://www.example.com
location /{ proxy_pass http://10.1.1.3:9000/app1}
Nginx uses proxy_pass directory to pass the request to 'example.com' to another server with ip address of10.1.1.3:9000
.
Important: When nginx starts connection with another server(backend) with proxy pass. Itcloses the previous connection with the client which results in some request information islost. So one shouldcapture the original details like actual ip address of client, host details etc andforward to the backend server.
Forward client details to backend server via reverse proxy
One need to Redefining Request Headers and forward it to backend server. To redefine request header useproxy_set_header
.
Use proxy_set_header
- Syntax: proxy_set_header ;
server{ listen 80; proxy_set_header Host$host;# replaces host header that comes from client proxy_set_header X-Real-IP$remote_addr;# captures original ip address of requester and sends it to backend proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;# all the traverse route the request takes location /{ proxy_pass http://10.1.1.3:9000/app1}}
References
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse