I’ve following server blocks in my nginx configuration:
server {
listen 80; #default_server;
listen [::]:80; #default_server;
client_max_body_size 20M;
client_header_timeout 600;
client_body_timeout 600;
keepalive_timeout 600;
server_name example1-proxy.in;
location / {
proxy_pass http://example1.in;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Forwarded $proxy_add_forwarded;
proxy_set_header X-Forwarded-For "";
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}
server {
listen 80; #default_server;
listen [::]:80; #default_server;
client_max_body_size 20M;
client_header_timeout 600;
client_body_timeout 600;
keepalive_timeout 600;
server_name example2-proxy.in;
location / {
proxy_pass http://example2.in;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Forwarded $proxy_add_forwarded;
proxy_set_header X-Forwarded-For "";
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}
I want to configure the example1-proxy and example2-proxy as a proxy pass in another nginx configuration, so I did the following:
server {
listen 80; #default_server;
listen [::]:80; #default_server;
client_max_body_size 20M;
client_header_timeout 600;
client_body_timeout 600;
keepalive_timeout 600;
server_name example5.in;
location /temp1/ {
proxy_pass http://example1-proxy.in;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Forwarded $proxy_add_forwarded;
proxy_set_header X-Forwarded-For "";
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
location /temp2/ {
proxy_pass http://example2-proxy.in;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Forwarded $proxy_add_forwarded;
proxy_set_header X-Forwarded-For "";
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}
when I hit the url http://example5.in/temp2, the http://example2-proxy.in should be called but I get following error:
{
"timestamp": 1643470058798,
"status": 403,
"error": "Forbidden",
"message": "Access Denied",
"path": "/temp2/some path to api "
}
I don’t get why above configuration doesn’t work,please suggest any solution.
2
Answers
solved it,actually in the second nginx configuration:
the $host should be replaced by the example1-proxy.in, only then this server block will be active. so correct config will be :
When you use a proxy pass, then NGINX looks at the destination url,
if it sees that the destination url does not include a URI (The part after the domain name e.g. /temp) which is your case since you are forwarding to "http://example2-proxy.in" then NGINX will add whatever URI it received initially to the next server.
In your case that will result with a request to http://example2-proxy.in/temp2/
If you want to send a request to http://example2-proxy.in/ you need to specify the URI for the destination server such as http://example2-proxy.in/
This is a snapshot from the NGINX websitehere: