Last active
May 29, 2019 05:38
-
-
Save tphummel/0084db290e633107ddaccc4e28f95cc6 to your computer and use it in GitHub Desktop.
Vagrantfile to install nginx, configure javascript custom modules, and a hello world example
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
# vagrant box add --name ol76 --checksum cfb58aaa12f56702afd739966273df63cd3ff3362e6a5a3fa6fa9ffcec4762e3 --checksum-type sha256 https://yum.oracle.com/boxes/oraclelinux/ol76/ol76.box | |
# https://nginx.org/en/docs/njs/examples.html#subrequest | |
Vagrant.configure("2") do |config| | |
config.vm.box = "ol76" | |
config.vm.network "forwarded_port", guest: 80, host: 8111, host_ip: "127.0.0.1" | |
config.vm.synced_folder ".", "/vagrant_data" | |
config.vm.provision "shell", inline: <<-SHELL | |
sudo cat > /etc/yum.repos.d/nginx.repo <<EOF | |
[nginx-stable] | |
name=nginx stable repo | |
baseurl=http://nginx.org/packages/rhel/7/\\$basearch/ | |
gpgcheck=0 | |
enabled=1 | |
gpgkey=https://nginx.org/keys/nginx_signing.key | |
EOF | |
sudo yum -y install nginx | |
sudo yum -y install nginx-module-njs | |
sudo cat > /etc/nginx/nginx.conf <<EOF | |
load_module /etc/nginx/modules/ngx_http_js_module.so; | |
load_module /etc/nginx/modules/ngx_stream_js_module.so; | |
user nginx; | |
worker_processes 1; | |
error_log /var/log/nginx/error.log warn; | |
pid /var/run/nginx.pid; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
js_include add-headers.js; | |
js_set \\$is_tom is_tom; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
log_format main '\\$remote_addr - \\$remote_user [\\$time_local] "\\$request" ' | |
'\\$status \\$body_bytes_sent "\\$http_referer" ' | |
'"\\$http_user_agent" "\\$http_x_forwarded_for"'; | |
access_log /var/log/nginx/access.log main; | |
sendfile on; | |
keepalive_timeout 65; | |
server { | |
listen 80; | |
server_name localhost; | |
location / { | |
add_header X-Is-Tom \\$is_tom; | |
root /usr/share/nginx/html; | |
index index.html index.htm; | |
} | |
} | |
} | |
EOF | |
sudo cat > /etc/nginx/add-headers.js <<EOF | |
function is_tom(r) { | |
return "true"; | |
} | |
EOF | |
sudo systemctl start nginx | |
echo "---------------------" | |
echo "run this command from your host OS to observe the custom header" | |
echo "curl -v localhost:8111 2>&1 | grep -i tom" | |
echo "---------------------" | |
SHELL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment