我目前正在尝试在Docker设置中部署Symfony应用。当前设置为:
[ Traefik ] -> [ Nginx ] -> [ PHP-FPM ]
dnsChallenge
选项)。traefik.frontend.rule
和label
来分配域例如,Traefik
生成证书的过程没有任何问题。 Traefik仪表板和Portainer可以正常工作。但是,当我通过浏览器访问Symfony应用程序时,Symfony认为它不在HTTPS连接上。浏览器中的url字段中包含https://foo.example.tld
,但是由Symfony控制的内容会退回到http
,例如。使用绝对路径生成的图片资源或网址。
var_dump($request->getScheme(), $request->getSchemeAndHttpHost()); // 'http' and 'http://example.tld'
我认为这是有道理的,因为Traefik
和Nginx
之间的连接使用端口80。如果我将端口443分配给Nginx
并让Nginx
监听在那个端口而不是端口80上,我得到了相同的结果。
问题是,如何解决此问题?我是否必须生成本地SSL证书,并让Nginx
加载并告诉Traefik
使用它?还有另一种方法吗?我是否需要转发Traefik
或Nginx
// docker-compose.yml (shortened)
version: '3.7'
services:
traefik:
image: traefik:1.7
container_name: traefik
command: --api --docker --docker.exposedbydefault=false
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./config/traefik.prod.toml:/traefik.toml
- acme-storage:/acme:rw
labels:
- traefik.enable=true
- traefik.backend=medison-traefik
- traefik.frontend.rule=Host:dashboard.example.tld
- traefik.port=8080
restart: always
webserver:
image: nginx:alpine
container_name: webserver
volumes:
- ./storage/uploads:/var/www/html/web/uploads:ro
- ./config/nginx.prod.conf:/etc/nginx/conf.d/default.conf:ro
working_dir: /var/www/html/web
depends_on:
- php-fpm
labels:
- traefik.enable=true
- traefik.backend=webserver
- traefik.frontend.rule=Host:foo.example.tld
restart: always
php-fpm:
image: user/image:master
container_name: php-fpm
volumes:
- ./storage/uploads:/var/www/html/web/uploads:rw
- ./logs/php:/var/www/var/logs:rw
working_dir: /var/www/html/web
restart: always
volumes:
acme-storage:
// nginx.prod.conf
server {
listen 80 default;
client_max_body_size 108M;
access_log /var/log/nginx/application.access.log;
root /var/www/html/web;
index app.php;
if (!-e $request_filename) {
rewrite ^.*$ /app.php last;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index app.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
include fastcgi_params;
}
}
对于使用端口443的测试,我在nginx.prod.conf
之后的listen 80 default;
第2行listen 443 default;
中将其更改为- traefik.port=443
,并在docker-compose.yml
中添加了- traefik.frontend.rule=Host:foo.example.tld
答案 0 :(得分:1)
当我使用Ingress将项目移至kubernetes时,我从htts重定向到http时遇到了同样的问题。
我设置了可信任的代理来请求。有了帮助。
https://symfony.com/doc/4.4/deployment/proxies.html
// public/index.php
// ...
Request::setTrustedProxies(
// trust *all* requests (the 'REMOTE_ADDR' string is replaced at
// run time by $_SERVER['REMOTE_ADDR'])
['127.0.0.1', 'REMOTE_ADDR'],
// if you're using ELB, otherwise use a constant from above
Request::HEADER_X_FORWARDED_AWS_ELB
);