Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Today I had a problem on nginX. I don't know where to start! :|

Fair enough, this is my nginx stanza:

location /geo {
add_header 'Access-Control-Allow-Origin' '*';
if ( $arg_callback ) {
echo_before_body '$arg_callback(';
echo_after_body ');';
}

proxy_pass https://api.example.com/geo;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}

NOTE: each part of nginX block is called stanza. I bet you didn't know about this one! :D

echo_before_body command will prepend something to the response which will be returned from nginX.

echo_after_body will append something to the response.

proxy_pass will proxy your requests to a backend server.

$arg_callback will get value of parameter callback in URL. So for example if you use $arg_type, you will get the value of type argument which is provided in URL: http://sample.com?type=SOMETHING

So far so good. The problem was that when I give call the URL with callback parameter https://api.example.com/geo?callback=test. It
generates /geo/geo URL instead of /geo. To circumvent the issue I used $request_uri in proxy_pass section proxy_pass https:// api.fax.plus$request_uri;. The route should be OK now, but there is one big problem here now that responses are returned in binary format instead of JSON. I removed Upgrade & Connection & proxy_http_version lines and it worked like a charm!

Don't ask me! I don't know what are Upgrade and Connection headers.

The output is like the below response for a URL similar to http://api.example.com/geo?callback=test:

test(
{
"username": "alireza",
"password": "123456"
}
)

#nginx #stanza #proxy_pass #echo_before_body #echo_after_body
Caddy is the HTTP/2 web server with automatic HTTPS. I wanted to proxy pass websocket service using Caddy but it didn't work:

wss.example.org {
proxy / myservice:8083 {
header_upstream Host {host}
header_upstream X-Real-IP {remote}
}
tls myemail@gmail.com
}


In the documentation it is mentioned that proxy can proxy pass web sockets too. The problem was about not using websocket inside of the proxy stanza, so we solved it using:

wss.example.org {
proxy / myservice:8083 {
header_upstream Host {host}
header_upstream X-Real-IP {remote}
websocket
}
tls myemail@gmail.com
}



#webserver #caddy #proxy #proxy_pass #wss #ws #websocket