-
-
Save viperfx/6de479eacbe2df84a203688518056bc0 to your computer and use it in GitHub Desktop.
Redwood Cookbook - Getting OG and meta tags working with nginx, pre-render.io and docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multi-stage | |
# 1) Node image for building frontend assets | |
# 2) nginx stage to serve frontend assets | |
# Name the node stage "builder" | |
# Set your env variables here. | |
FROM node:15 AS builder | |
ARG FIREBASE_API_KEY | |
ARG FIREBASE_PROJECT_ID | |
ENV FIREBASE_API_KEY=$FIREBASE_API_KEY | |
ENV FIREBASE_PROJECT_ID=$FIREBASE_PROJECT_ID | |
# Set working directory | |
WORKDIR /app | |
# Copy all files from current directory to working dir in image | |
COPY . . | |
# install node modules and build assets | |
RUN env | |
RUN yarn | |
ARG NODE_ENV | |
ENV NODE_ENV=$NODE_ENV | |
RUN yarn rw build web | |
# nginx state for serving content | |
FROM nginx:alpine | |
# Set working directory to nginx asset directory | |
WORKDIR /usr/share/nginx/html | |
# Remove default nginx static assets | |
RUN rm -rf ./* | |
# Copy static assets from builder stage | |
COPY nginx.conf /etc/nginx/conf.d/default.conf | |
COPY --from=builder /app/web/dist . | |
# Containers run nginx with global directives and daemon off | |
ENTRYPOINT ["nginx", "-g", "daemon off;"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
listen 80; | |
listen [::]:80 default ipv6only=on; | |
server_name _; # all hostnames | |
root /usr/share/nginx/html; | |
index index.html; | |
location / { | |
try_files $uri @prerender; | |
} | |
location @prerender { | |
proxy_set_header X-Prerender-Token YOUR_TOKEN_HERE; | |
set $prerender 0; | |
if ($http_user_agent ~* "googlebot|bingbot|yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest\/0\.|pinterestbot|slackbot|vkShare|W3C_Validator|whatsapp") { | |
set $prerender 1; | |
} | |
if ($args ~ "_escaped_fragment_") { | |
set $prerender 1; | |
} | |
if ($http_user_agent ~ "Prerender") { | |
set $prerender 0; | |
} | |
if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") { | |
set $prerender 0; | |
} | |
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs | |
resolver 8.8.8.8; | |
if ($prerender = 1) { | |
#setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing | |
set $prerender "service.prerender.io"; | |
rewrite .* /https://$host$request_uri? break; | |
proxy_pass http://$prerender; | |
} | |
if ($prerender = 0) { | |
rewrite .* /index.html break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment