tomaytotomato
tomaytotomato

Reputation: 4028

Symfony: how to specify port and scheme using Docker?

Encountering issues with url generation with symfony; using docker-compose to run php, nginx and postgreSQL.

The 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

Which correctly sets nginx, allowing me to access it at localhost:8080 -http 200 OK

The issue is, that symfony and twig are not using this url correctly. e.g.

index.html.twig

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

Outputs, the wrong scheme and doesn't include 8080 port.

https://localhost/email/web/styles/main.css?v=1

One suggestion, was to add these variables to .env file

SERVER_HOST=localhost
SERVER_SCHEME=http
SERVER_PORT=8080

However this had no effect.

The symfony config_dev.yml does not have anything that specifies https or a different port, the same for the base config as well.

Any ideas how to get this working, does Symfony not get the scheme from nginx?

Intellij Xdebug, shows this in $_SERVER

[SYMFONY__SERVER__HOST] => localhost

Thanks

Upvotes: 1

Views: 1350

Answers (2)

Jake Litwicki
Jake Litwicki

Reputation: 232

Your nginx config needs to have a proper server_name

server {
    server_name _;
    server_alias myapp.dev;
}

Upvotes: 0

Stephan van Leeuwen
Stephan van Leeuwen

Reputation: 631

This is probably because nginx is listening on port 80 within the container which makes symfony generate the url as if the access port is also port 80.

One thing you can try is setting the framework.assets.base_urls to http://localhost:8080/ within your config. This allows you to define a base URL to use for all asset urls. You should also remove the {{ url }} part in your twig file as the asset function will add this for you.

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

For more information: https://symfony.com/doc/current/reference/configuration/framework.html#base-urls

Upvotes: 2

Related Questions