Nginx Reverse proxy for cross domains
A reverse proxy is a server that sits in front of web servers and forwards client request to those web servers.
Use case: If we try to access URL http://example.com:8080/api from webpage at http://example.com due to browser cross domain restrictions we will see CORS errors.
To solve, we can setup Nginx reverse proxy server, so that , we can request URL http://example.com/api from webpage at http://example.com and the reverse proxy server would take care of forwarding the request to server running at port 8080.
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /api {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
}
To pass a request to an HTTP proxied server, the proxy_pass
directive is specified
inside a location.
$host
- This variable is set, in order of preference to: the host name from the request line itself, the “Host” header from the client request, or the server name matching the request.$scheme
- The schema of the original request.X-Forwarded-Proto
- header gives the proxied server information on the schema of the original request.X-Real-IP
- It is set to the IP address of the client.$proxy_add_x_forwarded_for
- This variable takes the value of originalX-Forwarded-For
header retrieved from the client and adds the Nginx server IP address to the end.
References:
Categories :
HTTP