Created
January 11, 2017 12:27
-
-
Save w00lf/5770c5808eae4f110bfa05dd8d75a0af to your computer and use it in GitHub Desktop.
Mruby nginx redirects with redis
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
version: '2' | |
services: | |
redis: | |
image: redis | |
nginx: | |
build: . | |
command: tail -f /var/log/nginx/access.log | |
environment: | |
- REDIS_URL=redis | |
ports: | |
- "3000:3000" | |
depends_on: | |
- redis |
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
FROM centos:latest | |
MAINTAINER Mikl Taraskin <example@gmail.com> | |
ADD nginx-build-rpm /tmp/nginx-build-rpm | |
WORKDIR /tmp/nginx-build-rpm | |
RUN rpm -Uvh nginx-build.rpm | |
RUN yum install -y redis | |
RUN mkdir -p /etc/nginx/include.d/rewrites/ | |
ADD example.rb /etc/nginx/include.d/rewrites/example.rb | |
ADD nginx.conf /etc/nginx/nginx.conf |
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
client = Redis.new('redis', 6379, 2) # Connect to the server ENV['REDIS_URL'] | |
client.select 0 | |
r = Nginx::Request.new | |
Nginx.errlogger Nginx::LOG_ERR, "request uri: #{r.uri}" | |
if matched_redirect = client.get(r.uri) | |
matched_redirect | |
else | |
r.uri | |
end |
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
# Work around problem 'no "events" section in configuration' | |
events { } | |
http { | |
server { | |
listen 3000; | |
location / { | |
mruby_set $redirect_candidate "/etc/nginx/include.d/rewrites/example.rb"; | |
if ($redirect_candidate != $request_uri) { | |
rewrite ^(.*)$ $redirect_candidate permanent; | |
} | |
return 200 "hello world"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Пара вопросов: