-
Notifications
You must be signed in to change notification settings - Fork 12
/
rawgithack.conf.txt
183 lines (156 loc) · 5.86 KB
/
rawgithack.conf.txt
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# rawgithack
# servers configuration
root /etc/nginx/rawgithack;
proxy_cache_path /var/cache/nginx/rawgithack
use_temp_path=off levels=1:2
max_size=5g inactive=7d
keys_zone=rawgithack.cache:20m;
limit_req_zone $http_x_forwarded_for zone=purge:10m rate=1r/m;
gzip on;
error_log logs/error.log warn;
# file extension to mime-type mapping
# kind of workaround because nginx can detect mime-type only for local file
map $extension $detect_content_type {
~*^(?:appcache|manifest)$ text/cache-manifest;
~*^atom$ application/atom+xml;
~*^bat$ application/x-msdownload;
~*^coffee$ text/coffeescript;
~*^css$ text/css;
~*^csv$ text/csv;
~*^eot$ application/vnd.ms-fontobject;
~*^geojson$ application/vnd.geo+json;
~*^(?:hbs|handlebars)$ text/x-handlebars-template;
~*^htc$ text/x-component;
~*^html?$ text/html;
~*^ics$ text/calendar;
~*^jscad$ application/javascript;
~*^json$ application/json;
~*^jsonld$ application/ld+json;
~*^kml$ application/vnd.google-earth.kml+xml;
~*^(?:md|markdown)$ text/markdown;
~*^m?js$ application/javascript;
~*^mhtml$ multipart/related;
~*^n3$ text/n3;
~*^nt$ application/n-triples;
~*^otf$ font/otf;
~*^(?:owl|rdf)$ application/rdf+xml;
~*^pdf$ application/pdf;
~*^rss$ application/rss+xml;
~*^shexc?$ text/shex;
~*^svg$ image/svg+xml;
~*^swf$ application/x-shockwave-flash;
~*^stl$ model/stl;
~*^tt(?:c|f)$ application/x-font-ttf;
~*^ttl$ text/turtle;
~*^vcard$ text/vcard;
~*^vcf$ text/x-vcard;
~*^vtt$ text/vtt;
~*^woff$ application/font-woff;
~*^woff2$ application/font-woff2;
~*^xht(?:ml)?$ application/xhtml+xml;
~*^xml$ text/xml;
~*^(?:xsl|xsd)$ application/xml;
~*^xslt$ application/xslt+xml;
~*^ya?ml$ text/yaml;
~*^wasm$ application/wasm;
default '';
}
# defines which extensions should include charset definition
map $extension $content_type_charset_string {
~*^(?:bat|eot|htc|kml|nt|otf|pdf|svg|swf|ttc|ttf|woff2?|wasm)$ '';
default '; charset=utf-8';
}
map $detect_content_type $detect_content_type_with_fallback {
'' $upstream_http_content_type;
default $detect_content_type$content_type_charset_string;
}
map $host $origin {
~*^gl(cdn)?.githack.com$ gitlab.com;
~*^bb(cdn)?.githack.com$ bitbucket.org;
~*^raw(cdn)?.githack.com$ raw.githubusercontent.com;
~*^gist(cdn)?.githack.com$ gist.githubusercontent.com;
}
map "$detect_content_type:$host" $check_redirect {
~*:\w+cdn.githack.com$ 0;
~*^.+: 0;
default 1;
}
map "$host:$rev" $cache_control {
~*^\w+cdn.githack.com:(?!master).+$ 'public, immutable';
default 's-maxage=300, public';
}
map "$host:$rev" $expires {
~*^\w+cdn.githack.com:(?!master).+$ max;
default 5m;
}
lua_package_path '/etc/nginx/rawgithack/?.lua';
server {
listen 80;
server_name _;
resolver 1.1.1.1 1.0.0.1;
location / {
ssi on;
limit_except GET { deny all; }
if ($host != 'raw.githack.com') {
return 301 $scheme://raw.githack.com;
}
}
location ~* ^/.+/.+/.+/(.+/)*$ {
rewrite ^(.*)/$ $1/index.html last;
}
location ~* ^/[^/]+/[^/]+/(?<rev>[^/]+)/.+?(?:\.(?<extension>[a-zA-Z0-9]+))?$ {
limit_except GET { deny all; }
if ($check_redirect) {
add_header Content-Type text/html;
return 301 https://$origin$request_uri;
}
proxy_read_timeout 10s;
# caching
proxy_cache rawgithack.cache;
proxy_cache_key "$origin$uri";
proxy_cache_revalidate on;
proxy_cache_valid any 5m;
proxy_cache_use_stale updating;
proxy_cache_background_update on;
proxy_cache_lock on;
proxy_force_ranges on;
proxy_http_version 1.1;
proxy_intercept_errors on;
proxy_pass https://$origin;
# we need to hide these headers in order to redefine them
# if we don't, they will be set twice
proxy_hide_header Status;
proxy_hide_header Expires;
proxy_hide_header Content-Type;
proxy_hide_header Cache-Control;
proxy_hide_header X-Frame-Options;
proxy_hide_header X-XSS-Protection;
proxy_hide_header Content-Disposition;
proxy_hide_header Content-Security-Policy;
proxy_hide_header Strict-Transport-Security;
proxy_hide_header Access-Control-Allow-Origin;
expires $expires;
# robots, go away!
add_header X-Robots-Tag none;
add_header Cache-Control $cache_control;
add_header Access-Control-Allow-Origin *;
add_header X-Githack-Cache-Status $upstream_cache_status;
add_header Content-Type $detect_content_type_with_fallback;
}
location /purge {
set $_url "";
limit_req zone=purge nodelay;
limit_req_status 429;
limit_except POST { deny all; }
default_type text/html;
lua_need_request_body on;
# lua_code_cache off;
content_by_lua_block { require("rawgithack").purge_request() }
}
location /proxy {
internal;
rewrite_by_lua_block { require("rawgithack").patch_proxy() }
proxy_http_version 1.1;
proxy_pass $_url;
}
}