嘿,各位开发人员。
看在上帝的份上,我似乎不能让这个工作......。我有一个docker容器,里面运行着一个phoenix应用。它托管在一个子域 "sub.example.com "上,我需要用https服务。
于是我给自己弄了一个通配符SSL证书,安装好,配置好生产文件,并暴露了docker容器的80和443端口。
试了一下,80端口工作正常,但是443总是返回一个 "ERR_CONNECTION_RESET"。日志显示443什么都没有,但80端口工作正常。
我已经试了一段时间了,现在我需要你的帮助。有什么问题吗?请看下面的代码。
FROM bitwalker/alpine-elixir-phoenix:latest
# create app folder
RUN mkdir /app
WORKDIR /app
COPY . .
# setting the port and the environment (prod = PRODUCTION!)
EXPOSE 80
EXPOSE 443
# install dependencies (production only)
RUN mix local.rebar --force
RUN mix deps.get --only prod
RUN mix compile
config :example, ExampleWeb.Endpoint,
http: [port: 80],
url: [host: "sub.example.com"],
cache_static_manifest: "priv/static/cache_manifest.json",
https: [
cipher_suite: :strong,
otp_app: :example,
port: 443,
keyfile: System.get_env("SSL_KEY_PATH"),
certfile: System.get_env("SSL_CERT_PATH"),
cacertfile: System.get_env("SSL_CHAINED_CERT_PATH")
]
* Trying 64.225.24.82...
* TCP_NODELAY set
* Connected to sub.example.com (64.225.24.82) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to sub.example.com:443
* Closing connection 0
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to sub.example.com:443
EDIT:我是手动设置容器内的ENV变量。例如,在bash中。
SSL_KEY_PATH=path/example.key SSL_CERT_PATH=path/example.crt SSL_CHAINED_CERT_PATH=path/example_chained.crt MIX_ENV=prod mix phx.server
你的Docker文件中没有指定 "子域 "和 "子域"。MIX_ENV
所以可能是用默认的 MIX_ENV=dev
.
它也没有指定用于运行实际应用程序的命令,通常是指 mix phx.server
本地化。
下面是我用来在本地运行一个应用的Docker文件示例,这个文件是 不 优化了大小或构建速度,不应该在生产中使用(为此做一个完整的混合版本),但为了以防万一,它有帮助。
FROM elixir:1.10.1-alpine AS dev_img
# Environment
ENV MIX_ENV prod
ENV PORT 4000
ENV SSL_PORT 4001
# Install required system dependencies
RUN apk update; \
apk upgrade --no-cache; \
apk add --no-cache libsodium libsodium-dev nodejs npm curl git inotify-tools build-base; \
mix local.rebar --force; \
mix local.hex --force
WORKDIR /opt/app
COPY . .
RUN mix do deps.get, compile
RUN cd assets; \
npm ci; \
cd ..
ENV PG_DB_PORT 5432
ENV PG_DB_HOST db-container-name
ENV SSL_KEY_PATH
ENV OTHER_VARS
CMD ["mix", "phx.server"]
经过几天的搜索、试验和错误 以及与其他开发者的交锋, 我已经用nginx的代理服务器解决了这个问题。以下是实现这一切的配置。
phoenix - prod.exs
config :example, ExampleWeb.Endpoint,
http: [port: 5000],
url: [host: "sub.example.com", port: 5000]
nginx配置
upstream phoenix {
server 0.0.0.0:5000;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
server_name sub.example.com;
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/nginx/ssl/example_chained.pem;
ssl_certificate_key /etc/nginx/ssl/example.pem;
large_client_header_buffers 4 16k;
location ~ live/websocket {
proxy_http_version 1.1;
proxy_set_header Origin '';
proxy_set_header X-Forwarded-Host $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_ssl_server_name on;
proxy_pass http://0.0.0.0:5000;
break;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_ssl_server_name on;
proxy_pass http://0.0.0.0:5000;
break;
}
}
进一步的讨论在这里找到。
https:/elixirforum.comtphoenix-app-in-docker-with-ssl-not-receiving-anything-onport-44331229。