Symfony:如何使用Docker指定端口和scheme?

用symfony遇到url生成问题; 使用docker-compose运行php,nginx和postgreSQL。

docker-compose.dev.yml

 services: php: volumes: - '.:/srv' environment: - SYMFONY_ENV=dev front: environment: - VIRTUAL_HOST=localhost - VIRTUAL_PORT=8080 ports: - 0.0.0.0:8080:80 volumes: - ./web:/srv - /keys/reports.crt:/certs/domain.crt:ro - /keys/reports.key:/certs/domain.key:ro 

哪个正确设置nginx,允许我在localhost:8080上访问它localhost:8080http 200 OK

问题是,symfony和树枝没有正确使用这个url。 例如

index.html.twig

  <link rel="stylesheet" type="text/css" href="{{ url }}{{ asset('email/web/styles/main.css') }}?v=1"> 

输出错误的scheme并不包括8080端口。

HTTPS:?//localhost/email/web/styles/main.css V = 1

一个build议是将这些variables添加到.env文件

 SERVER_HOST=localhost SERVER_SCHEME=http SERVER_PORT=8080 

但是这没有效果。

symfony config_dev.yml没有指定https或不同端口的任何内容,对于基本configuration也是如此。

任何想法如何得到这个工作,Symfony不从nginx的计划吗?

Intellij Xdebug,在$ _SERVER中显示

[SYMFONY__SERVER__HOST] => localhost

谢谢

这可能是因为nginx正在侦听容器内的端口80,这使得symfony生成的url就像访问端口也是80端口一样。

您可以尝试的一件事是在您的configuration中将framework.assets.base_urls设置为http://localhost:8080/ 。 这使您可以定义一个基本的URL用于所有的资源url。 您还应该移除您的twig文件中的{{ url }}部分,因为资产function将为您添加此部分。

 framework: assets: base_urls: - 'http://localhost:8080/' 

欲了解更多信息: https : //symfony.com/doc/current/reference/configuration/framework.html#base-urls

你的nginxconfiguration需要有一个合适的server_name

 server { server_name _; server_alias myapp.dev; }