Blog
Docker: Rails-Redirect auf nicht standardmässige Ports
· Von Magnus Lübeck
Einleitung
Wenn Sie eine Entwicklungsumgebung mit Docker aufsetzen, möchten Sie Ihre Rails-Applikation manchmal mit einem nginx-Proxy testen. Bei einem nicht standardmässigen Port (nicht 80 oder 443) stossen Sie auf ein kleines Problem: nginx lauscht im Container auf Port 80, Sie mappen in Ihrer Datei docker-compose.yml aber einen anderen Port.
nginx:
image: nginx:latest
ports:
- 8088:80
- ./contrib/nginx.dev.conf:/etc/nginx/conf.d/default.conf
Meine nginx-Konfiguration
upstream docdeposit {
server docdeposit:3000;
}
server {
#--- here lays dragons
listen 80;
server_name docdepos.it;
location / {
proxy_pass http://docdeposit;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Ihre Rails-Applikation debuggen
class ApplicationController < ActionController::Base
...
def authorized
if signed_in?
true
else
ap request.env
redirect_to '/login'
end
end
end
Das Standardverhalten testen
Beim Testen meiner Applikation möchte ich also auf http://localhost:8085/login weitergeleitet werden.
$ curl -v http://localhost:8085
* Trying 127.0.0.1:8085...
* Connected to localhost (127.0.0.1) port 8085 (#0)
> GET / HTTP/1.1
> Host: localhost:8085
...
< HTTP/1.1 302 Found
...
< Location: http://localhost/login
...
<html><body>You are being <a href="http://localhost/login">redirected</a>.</body></html>
Die Debug-Ausgabe zeigt uns, was die Applikation über die HTTP-Header vom nginx-Server erhält.
"SERVER_PROTOCOL" => "HTTP/1.0",
"HTTP_HOST" => "localhost",
"HTTP_X_FORWARDED_FOR" => "172.26.0.1",
"HTTP_X_FORWARDED_PROTO" => "http",
"HTTP_CONNECTION" => "close",
"HTTP_USER_AGENT" => "curl/7.81.0",
"HTTP_ACCEPT" => "*/*",
"puma.request_body_wait" => 0.0034820139408111572,
"SERVER_NAME" => "localhost",
"SERVER_PORT" => "80",
"PATH_INFO" => "/",
Nun ändern wir Folgendes:
- Eine einfache Änderung in der Datei
docker-compose.yml - Eine kleine Änderung an der nginx-Konfiguration
default.conf
In der Datei docker-compose.yml können Sie entweder auf eine Variable verweisen, die in Ihrer Datei .env gesetzt ist, oder den Wert fest eintragen. Ich nenne die Variable NGINX_PORT:
version: "3.9"
services:
nginx:
image: nginx:latest
links:
- docdeposit
ports:
- ${NGINX_PORT:-80}:80
volumes:
#- ./contrib/nginx.dev.conf:/etc/nginx/conf.d/default.conf
- ./contrib/nginx.dev.conf:/etc/nginx/templates/default.conf.template
environment:
#--- hard coded
#NGINX_PORT: "8085"
#--- Dynamicaly override the NGINX port in the config, so that rails redirect works properly
#--- in your local .env file, by setting the variable NGINX_PORT to something
#--- that is free on your computer (i.e `NGINX_PORT=8085`)
NGINX_PORT: "${NGINX_PORT:-80}"
Die kleine Änderung in Ihrer Datei nginx.dev.conf/default.conf:
upstream docdeposit {
server docdeposit:3000;
}
server {
#--- here lays dragons
listen 80;
server_name docdepos.it;
location / {
proxy_pass http://docdeposit;
# proxy_set_header Host $host;
proxy_set_header Host $host:${NGINX_PORT};
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Das möchten Sie sehen: einen korrekten Redirect, ohne Ihre Rails-Applikation umkonfigurieren zu müssen, allein anhand der von nginx weitergereichten Header:
(venv) [maglub@Magnuss-MBP-2:~/dev/kmg/demo/docdeposit (main)]$ curl -v http://localhost:8085
* Trying 127.0.0.1:8085...
* Connected to localhost (127.0.0.1) port 8085 (#0)
> GET / HTTP/1.1
> Host: localhost:8085
...
< HTTP/1.1 302 Found
...
< Location: http://localhost:8085/login
...
<html><body>You are being <a href="http://localhost:8085/login">redirected</a>.</body></html>
Und so sieht die Debug-Ausgabe der Rails-Applikation aus:
"SERVER_PROTOCOL" => "HTTP/1.0",
"HTTP_HOST" => "localhost:8085",
"HTTP_X_FORWARDED_FOR" => "172.26.0.1",
"HTTP_X_FORWARDED_PROTO" => "http",
"HTTP_CONNECTION" => "close",
"HTTP_USER_AGENT" => "curl/7.81.0",
"HTTP_ACCEPT" => "*/*",
"puma.request_body_wait" => 0.0033560097217559814,
"SERVER_NAME" => "localhost",
"SERVER_PORT" => "8085",
"PATH_INFO" => "/",
Referenzen
Dieser Beitrag wurde mit KI aus dem englischen Original übersetzt.
Blog