[ Module development - Socket ]

Maxim Dounin mdounin at mdounin.ru
Tue Apr 11 14:10:16 UTC 2017


On Tue, Apr 11, 2017 at 04:28:54AM -0400, comput3rz wrote:

> Hi,
> I need to develop a module which make a connexion with an external server. I
> tried to use simple TCP sockets available  
> in the "sys/socket.h" (Linux) library but it appears that it wasn't a good
> idea as nothing was working correctly. 
> Then, I searched in the source code keywords like "connection, socket" and I
> found the "ngx_connection.h" file which I 
> suppose, is the API to be used if I want to do an external connection.
> Looking on the web for tutorials/resources using
> this API I found nothing more than unanswered stackoverflow threads...
> I'm pretty sure that including "sys/socket.h" into nginx is not the good
> solution as it makes ugly code and broke the pipes 
> system which makes nginx so fast, could you please help me to find a
> solution ? 

Try looking into src/http/ngx_http_upstream.c for the upstream 
module implementation, the base module used by proxy and other 
modules which talk to backend servers.  Basically, you are 
expected to use the ngx_event_connect_peer() function to connect 
to an external server, and then use appropriate event handling.

Additional and probably simplier examples can be found in the 
mail module (src/mail/ngx_mail_auth_http_module.c, 
src/mail/ngx_mail_proxy_module.c), the stream module 
(src/stream/ngx_stream_proxy_module.c), as well as in OCSP 
stapling code (src/event/ngx_event_openssl_stapling.c).

Maxim Dounin

More information about the nginx mailing list