# How to use C as a scripting language?

**URL:** <https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491>\
**Category:** NGINX\
**Created:** [December 3, 2011, 8:39am UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491 "2011-12-03T08:39:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [December 3, 2011, 8:39am UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/1 "2011-12-03T08:39:05Z")

</div>

I am using php in nginx. I wonder how can I modify nginx configuration  
to work with C too. Considered a simple c file (hello.c and compiled as  
a.out) with printf of

printf(“Content-type: text/html\r\nStatus: 200 OK\r\n\r\n”);  
printf(“Hello World”);

how can I display Hello World by browsing the compiled version on  
localhost/a.out

Posted at Nginx Forum:

> **[How to use C as a scripting language?](https://forum.nginx.org/read.php?2,219531,219531#msg-219531)**
>
> I am using php in nginx. I wonder how can I modify nginx configuration to work with C too. Considered a simple c file (hello.c and compiled as a.out) with printf of printf("Content-type: text/html\\r\\nStatus: 200 OK\\r\\n\\r\\n");...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 3, 2011, 9:50am UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/2 "2011-12-03T09:50:42Z")

</div>

On 3 Dez 2011 07h38 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am using php in nginx. I wonder how can I modify nginx  
> configuration to work with C too. Considered a simple c file  
> (hello.c and compiled as a.out) with printf of
> 
> printf(“Content-type: text/html\r\nStatus: 200 OK\r\n\r\n”);  
> printf(“Hello World”);
> 
> how can I display Hello World by browsing the compiled version on  
> localhost/a.out

Use either:

1. fcgiwrap [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/Fcgiwrap)

2. thttpd [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/ThttpdCGI)

3. mini-httpd

Nginx doesn’t support CGI.

— appa

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 3, 2011, 11:30am UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/3 "2011-12-03T11:30:46Z")

</div>

On 03/12/11 07:38, etrader wrote:

> I am using php in nginx. I wonder how can I modify nginx configuration  
> to work with C too. Considered a simple c file (hello.c and compiled as  
> a.out) with printf of
> 
> printf(“Content-type: text/html\r\nStatus: 200 OK\r\n\r\n”);  
> printf(“Hello World”);
> 
> how can I display Hello World by browsing the compiled version on  
> localhost/a.out

The best solution probably depends on what you’re trying to achieve and  
your particular circumstances. As well as the solutions already  
suggested, one method I’ve used is to build the C program against  
libevent, effectively using libevent to build a mini webserver that is  
running all the time. nginx can then use that as an upstream proxy.

James

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 3, 2011, 1:45pm UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/4 "2011-12-03T13:45:14Z")

</div>

Thanks James!  
The idea of building a mini-webserver for C programming is brilliant. I  
searched a lot to find a guidance for building very basic web server in  
C (obviously I prefer to write the web server in C with native API for C  
files); but I was unable to find. All web servers in C are advanced  
applications such as nginx. Could you please give me a hint how to start  
to write a mini web server in C.

Posted at Nginx Forum:

> **[Re: How to use C as a scripting language?](https://forum.nginx.org/read.php?2,219531,219552#msg-219552)**
>
> I am using php in nginx. I wonder how can I modify nginx configuration to work with C too. Considered a simple c file (hello.c and compiled as a.out) with printf of printf("Content-type: text/html\\r\\nStatus: 200 OK\\r\\n\\r\\n");...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 3, 2011, 2:30pm UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/5 "2011-12-03T14:30:25Z")

</div>

Today Dec 3, 2011 at 07:44 etrader wrote:

> The idea of building a mini-webserver for C programming is brilliant. I  
> searched a lot to find a guidance for building very basic web server in  
> C (obviously I prefer to write the web server in C with native API for C  
> files); but I was unable to find. All web servers in C are advanced  
> applications such as nginx. Could you please give me a hint how to start  
> to write a mini web server in C.

[Google Search](http://www.google.com/search?q=libevent+webserver+in+40+lines)

–  
WNGS-RIPE

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 3, 2011, 2:13pm UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/6 "2011-12-03T14:13:41Z")

</div>

On 03/12/11 12:44, etrader wrote:

> Thanks James!  
> The idea of building a mini-webserver for C programming is brilliant. I  
> searched a lot to find a guidance for building very basic web server in  
> C (obviously I prefer to write the web server in C with native API for C  
> files); but I was unable to find. All web servers in C are advanced  
> applications such as nginx. Could you please give me a hint how to start  
> to write a mini web server in C.

Start here:

[http://libevent.org/](http://libevent.org/)

libevent has all of the hard stuff done for you. You basically just  
need to use it’s API to get hold of all the HTTP request information  
and to let it handle the response to the user.

James

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 3, 2011, 2:33pm UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/7 "2011-12-03T14:33:31Z")

</div>

On Saturday 03 December 2011 16:44:56 etrader wrote:

> Thanks James!  
> The idea of building a mini-webserver for C programming is brilliant. I  
> searched a lot to find a guidance for building very basic web server in  
> C (obviously I prefer to write the web server in C with native API for C  
> files); but I was unable to find. All web servers in C are advanced  
> applications such as nginx. Could you please give me a hint how to start  
> to write a mini web server in C.

Instead of building a web server, you can consider to write a fastcgi  
or scgi application.

[http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ap\_guide.htm](http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ap_guide.htm)

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 3, 2011, 3:13pm UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/8 "2011-12-03T15:13:03Z")

</div>

> Instead of building a web server, you can consider to write a fastcgi  
> or scgi application.
> 
> [http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ap\_guide.htm](http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ap_guide.htm)
> 
> wbr, Valentin V. Bartenev

Another solution would be writing uWSGI plugins:

> **[The uWSGI project — uWSGI 2.0 documentation](https://uwsgi-docs.readthedocs.io/en/latest/)**

You will end with items passed by nginx to your app easy accessible and  
you will not need to learn how to program a server/daemon  
and focus on your app.

In addition to this your app will be accessibile via uwsgi/http/fastcgi  
and can use the whole uwsgi api and operational modes  
(preforked/threaded/async/green).

The doc on writing plugins is not available, but you can subscribe to  
the  
uWSGI list and ask for help (or read the sources of the other plugins).

–  
Roberto De Ioris

> **[unbit - Overview](https://github.com/unbit)**
>
> makers of uWSGI and many more open source projects - unbit

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [December 3, 2011, 3:57pm UTC](https://www.ruby-forum.com/t/how-to-use-c-as-a-scripting-language/214491/9 "2011-12-03T15:57:30Z")

</div>

You could look at the lua module. I hardly ever write C for nginx now.

[http://openresty.org/](http://openresty.org/) has some examples.

Also, I’m a big fan of using HTTP over things like fastcgi,scgi, etc.  
Much easier to test, debug, etc. But that’s just my opinion.

–Brian
