-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgi.nginx.conf
98 lines (86 loc) · 3.99 KB
/
cgi.nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# cgi.nginx.conf
# Copyright Daniel Roesler (github.com/diafygi), Released under MIT License
# This perl blob converts a request into a CGI request and passes it to a provided $cgi_script,
# returning the script's response. This allows you to use cgi and wsgi scripts in nginx without
# running a separate process or FastCGI/uwsgi server.
# NOTE: This forks and creates a subprocess for each request, so it's not nearly as efficient as
# running a separate FastCGI or uwsgi server. This is mainly intended for quickly spinning up web
# applications that have light usage without having to worry about keeping another server running.
# Requirements: This script uses the nginx perl extension
# apt install nginx libnginx-mod-http-perl
# Example usage:
# location / {
# # example: python with wsgi app in myapp.py (flask app, django wsgi.py, etc.)
# set $cgi_script "cd /path/to/wsgi/app/ && /path/to/venv/bin/python3 -c 'import myapp, wsgiref.handlers; wsgiref.handlers.CGIHandler().run(myapp.app)'";
# # example: other cgi script
# set $cgi_script "/path/to/cgi-bin/myscript.cgi";
# # optional: list of headers to include (default is 'Host Content-Type Content-Length')
# set $cgi_headers "Host Content-Length Content-Type Cookie Authorization";
# # include the perl blob to convert the request to cgi
# include "/path/to/cgi.nginx.conf";
# }
perl "sub {
use IPC::Open2;
# send the request to an external command
sub handle {
my $r = shift;
# add basic cgi environ
$ENV{'REQUEST_METHOD'} = $r->request_method;
$ENV{'SCRIPT_NAME'} = $r->variable('cgi_script');
$ENV{'PATH_INFO'} = $r->uri;
$ENV{'QUERY_STRING'} = $r->args;
$ENV{'CONTENT_TYPE'} = $r->header_in('Content-Type');
$ENV{'CONTENT_LENGTH'} = $r->header_in('Content-Length') // '0';
$ENV{'SERVER_NAME'} = $r->header_in('Host');
$ENV{'SERVER_PORT'} = $r->variable('server_port');
$ENV{'SERVER_PROTOCOL'} = $r->variable('scheme');
$ENV{'REQUEST_BODY_FILE'} = $r->request_body_file;
# add headers to cgi environ
foreach (split(/ /, $r->variable('cgi_headers') // 'Host Content-Type Content-Length')) {
my $h = 'HTTP_' . uc($_);
$h =~ s/[^A-Z]/_/g;
$ENV{$h} = $r->header_in($_);
}
# pass the cgi request to the target sub command
my $pid = open2(my $cmdout, my $cmdin, $r->variable('cgi_script'));
# write any POST data
if ($r->request_body) {
print $cmdin $r->request_body;
}
elsif ($r->request_body_file) {
open(my $i, '<', $r->request_body_file);
while (<$i>) { print $cmdin $_; }
}
close($cmdin);
# capture headers and body from response output
my ($sts, $ct, $hdrs, $is_headers) = ('200 OK', 'text/plain', '', 1);
while (<$cmdout>) {
# update response status
if (m/^[Ss]tatus: ([^\r\n]+)/) { $sts = $1; }
# update content-type
if (m/^[Cc]ontent-[Tt]ype: ([^\r\n]+)/) { $ct = $1; }
# buffer response headers
if ($is_headers) {
# blank line indicates end of headers, so send response headers
if (m/^\r?\n$/) {
$r->status($sts);
foreach (split(/\\r\\n/, $hdrs)) { $r->header_out(split(/: /, $_, 2)); }
$r->send_http_header($ct);
$is_headers = 0;
}
# capture all non content-type headers
elsif (not m/^[Cc]ontent-[Tt]ype: /) {
$hdrs = $hdrs . $_;
}
}
# no longer buffering headers, so send anything else as response body
else {
$r->print($_);
}
}
}
# requests with bodies need to be async loaded
my $request = shift;
if ($request->has_request_body(\&handle)) { return OK; }
else { handle($request); return OK; }
}";