-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdefault.conf
117 lines (94 loc) · 5.56 KB
/
default.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=prerendercloud_cache:10m max_size=100m inactive=60m use_temp_path=off;
server {
set $root /usr/share/nginx/html;
# must use resolver and $variable to fix nginx inability to respect TTL of headless-render-api.com's load balancer
# https://tenzer.dk/nginx-with-dynamic-upstreams/
resolver 8.8.8.8;
set $prerendercloud 'service.headless-render-api.com';
listen 80;
server_name localhost;
location / {
root $root;
try_files $uri @prerendercloud;
}
location @prerendercloud {
# gzip all traffic to service.headless-render-api.com
proxy_set_header Accept-Encoding "gzip";
# un-gzip traffic from service.headless-render-api.com if the client doesn't support it
gunzip on;
root $root;
# get token after signing up at https://headless-render-api.com
# proxy_set_header X-Prerender-Token YOUR_SECRET_API_TOKEN_HERE;
# API options (HTTP headers)
# read https://headless-render-api.com/docs/api before enabling any of these
# proxy_set_header "Prerender-Disable-Ajax-Preload" "true";
# proxy_set_header "Prerender-Disable-Ajax-Bypass" "true";
# proxy_set_header "Prerender-Remove-Script-Tags" "true";
# proxy_set_header "Prerender-Remove-Trailing-Slash" "true";
# proxy_set_header "Prerender-Follow-Redirects" "true";
# not recommended: read docs before enabling this
# (if you do enable it, you'll have to adjust proxy_cache_key, at least)
# proxy_set_header origin-header-whitelist prerendercloud-is-mobile-viewer;
proxy_cache prerendercloud_cache;
# this caches prerendercloud results for 5m
proxy_cache_valid 200 5m;
# don't let rate limiting or server errors interrupt a request
proxy_intercept_errors on;
error_page 429 500 502 503 504 =200 /index.html;
# this serves stale while waiting on a request to service.headless-render-api.com to update cache (after 1st req)
# see http://serverfault.com/questions/576402/nginx-serving-stale-cache-response-while-updating
proxy_cache_use_stale updating;
set $prerender 0;
# RECOMMENDED DEFAULT CONFIG
# only prerender if the path does not have an extension or does have an html extension
# Note: this prerenders all user-agents, but the extension regex if statement
# is unnecessary (just conservative). The previous `location /` with a try_files for static resources
# is sufficient for preventing prerendering of static resources
if ($uri ~ "^(([^.]|\.html?)+)$") {
set $prerender "EXTENSION";
}
# ALL USER-AGENTS (RECOMMENDED)
# (comment this out if using the "bots only section")
set $prerender "${prerender}-USER_AGENT";
# "BOTS ONLY" USER-AGENTS (NOT RECOMMENDED)
# (comment this out if using the "all user-agents")
# if ($http_user_agent ~* "googlebot|yahoo|bingbot|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator|redditbot|Applebot|WhatsApp|flipboard|tumblr|bitlybot") {
# set $prerender "${prerender}-USER_AGENT";
# }
# don't prerender AJAX requests (optimization to avoid routing XHR/AJAX through service.headless-render-api.com)
# see: https://github.com/sanfrancesco/prerendercloud-ajaxmonkeypatch#ajax-bypass
if ($http_x_prerendered ~ true) {
set $prerender 0;
# Note: now your XHR origin will receive the x-prerendered header
# so if you use Access-Control-Allow-Headers from your XHR origin
# you have 3 options to avoid header warnings/errors
# 1. either add x-prerendered to the whitelist (ideal)
# 2. or if you're using nginx to proxy paths to different XHR origins,
# strip the header for that API proxy with: proxy_set_header x-prerendered "";
# 3. if the above 2 options are untenable, you can disable this optimization
# by including the header: Prerender-Disable-Ajax-Bypass: true with your API requests
}
# don't prerender if the prerender user agent is retrieving the page
if ($http_user_agent ~ "prerendercloud") {
set $prerender 0;
}
# change this if you are using origin-header-whitelist or have other unique requirements
# http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key
proxy_cache_key "$prerender$host$uri";
if ($prerender = "EXTENSION-USER_AGENT") {
# * non-standard ports will fail because request_uri drops the port (so only 80/443 will work)
# * this particular configuration with the $uri variable in the proxy_pass is one of the few ways
# of preventing strange nginx URL re-encoding and decoding
# * https://stackoverflow.com/a/21012310
# * https://stackoverflow.com/a/37584637
rewrite .* /$scheme://$host$request_uri? break;
# The previous line is supposed to detect the scheme and host but
# if this isn't working, just hard code your URL:
# rewrite .* /https://www.example.com$request_uri? break;
proxy_pass http://$prerendercloud$uri;
break;
}
# forces non-existent files (404s) to load index.html (single page apps)
rewrite .* /index.html break;
}
}