Created
October 23, 2017 09:11
-
-
Save nnnikolay/2d45e9eb9a58fa02525c0185d2a596f3 to your computer and use it in GitHub Desktop.
AWS Elastic Beanstalk deployment scripts with: - using Memcached directly from Nginx; - advanced logging info like redirect to url and response time; - redirect to https
This file contains hidden or 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
files: | |
"/tmp/deployment/nginx.conf": | |
owner: root | |
group: root | |
mode: "000644" | |
content: | | |
user nginx; | |
load_module /usr/lib64/nginx/modules/ngx_http_perl_module.so; | |
worker_processes auto; | |
error_log /var/log/nginx/error.log; | |
pid /var/run/nginx.pid; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
perl_set $mkey ' | |
sub { | |
my $r = shift; | |
# fetch current locale | |
my $header = $r->header_in("Accept-Language") . ","; | |
my $locale = substr(substr($header, 0, index($header, ",")), 0, 2); | |
if ( $locale ne "en") { $locale = "de"; } | |
my $key = $locale . ":" . $r->uri; | |
return $key; | |
} | |
'; | |
port_in_redirect off; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
log_format custom '$remote_addr - $remote_user [$time_local] "$request" ' | |
'$status $body_bytes_sent "$http_referer" ' | |
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_http_location" $upstream_response_time'; | |
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; | |
log_format healthd '$msec"$uri"' | |
'$status"$request_time"$upstream_response_time"' | |
'$http_x_forwarded_for'; | |
include /etc/nginx/conf.d/*.conf; | |
# End Modification | |
} | |
"/tmp/deployment/vhost.conf": | |
owner: root | |
group: root | |
mode: "000644" | |
content: | | |
upstream nodejs { | |
server 127.0.0.1:8081; | |
keepalive 256; | |
} | |
server { | |
listen 8080; | |
set $needRedir 0; | |
if ($http_x_forwarded_proto = "http") { set $needRedir 1; } | |
if ($needRedir) { return 301 https://$host$request_uri; } | |
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") { | |
set $year $1; | |
set $month $2; | |
set $day $3; | |
set $hour $4; | |
} | |
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd; | |
access_log /var/log/nginx/access.log custom; | |
location / { | |
set $memcached_key $mkey; | |
memcached_pass <memcache-host>:11211; | |
error_page 404 = @dynamic_request; | |
charset utf-8; | |
default_type text/html; | |
} | |
location @dynamic_request { | |
proxy_pass http://nodejs; | |
proxy_set_header Connection ""; | |
proxy_http_version 1.1; | |
proxy_set_header Host $host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_buffer_size 16k; | |
proxy_buffers 8 16k; | |
proxy_busy_buffers_size 32k; | |
} | |
gzip on; | |
gzip_comp_level 4; | |
gzip_types text/html text/plain text/css application/json application/x-javascript application/javascript text/javascript text/xml application/xml application/xml+rss image/svg+xml font/x-woff font/woff2; | |
location /static { | |
alias /var/app/current/static; | |
expires 30d; | |
add_header Pragma public; | |
add_header Cache-Control "public"; | |
} | |
} | |
"/opt/elasticbeanstalk/hooks/appdeploy/pre/60config_generate.sh": | |
owner: root | |
group: root | |
mode: "000755" | |
content: | | |
#!/usr/bin/env bash | |
set -xe | |
# first generate the configs from ELB | |
/opt/elasticbeanstalk/containerfiles/ebnode.py --action generate-config | |
# copy main nginx file | |
cp /tmp/deployment/nginx.conf /tmp/deployment/config/#etc#nginx#nginx.conf | |
# copy vhost file | |
cp /tmp/deployment/vhost.conf /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf | |
"/etc/nginx/conf.d/proxy.conf": | |
mode: "000644" | |
owner: root | |
group: root | |
content: | | |
client_body_timeout 12; | |
client_header_timeout 12; | |
send_timeout 10; | |
container_commands: | |
00_replace_memcache_host: | |
command: "/bin/sed -i \"s/<memcache-host>/$CACHE_MEMCACHED_HOST/\" /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment