Nginx and hostname issue

Hi,

I’m trying to create a new stack of containers Nginx, php, mysql and wordpress.

For now I just have nginx alone and I’m trying to set a local hostname to avoid the classic localhost:80 for futur SSL certificate tests.

So instead of having localhost:80 when I run Nginx, I would like to have “myapp.local”

I created an .env file with a constant HOSTANE=myapp.local

My docker-compose.yml file is like that:

services:
  nginx:
    container_name: ${CONTAINER_NAME}-nginx
    image: nginx:1.27.3-alpine
    restart: unless-stopped
    env_file: .env
    environment:
      HOSTNAME: ${HOSTNAME}
    ports:
      - 80:80
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - internal

I created a default.conf file for nginx configuration and added:

    server_name ${HOSTNAME};

I saw 2 different ways to setup environment variable…once it is called NGINX_HOST and once it’s HOSTNAME

so I tried both way:

    environment:
      HOSTNAME: ${HOSTNAME}

and

    environment:
      NGINX_HOST=${HOSTNAME}

but when my container runs it’s always localhost:80

any idea what I missed ?
thx

You seem to have an nginx configuration issue. I don’t know how Nginx supports using environment variable sin the config, but you should search for “Nginx proxy” and “Traefik” or just “docker reverse proxy” as there are existing solutions with documentations. Even if you can solve the environment variable problem, you could only have a single hostname supported by the proxy container, so I don’t see why it would be better than just using the default config and pointing a hostname to 127.0.0.1 in your hots file.

Standard container based reverse proxies let you use multiple domains for multiple containers behind it.

I’m not sure what you mean. I don’t see where you set the variable. HOSTNAME would be the hostname of your host machine. If it is set incorrectly, the container will get an incorrect value as well. But again, I still don’t see why the hostname is required at all.

I see no reason to use a reverse proxy for local development. It is on 1 computer, in a docker container, so in which way it would make nginx works better with hostname ?

My issue it’s to test localhost web development app with http and https request. For SSL to work on local dev environment, you need a hostname and not using http://localhost.

I’m looking for a solution to avoid to add lines in hosts file each time I want to test a web app on my local machine.

Using a reverse proxy in development is completely fine, especially when you know the user will most likely have a reverse proxy in production, so you want to also test if the app behind the reverse proxy can handle the URLs and redirections properly when it gets requests on HTTP but the URL requested by the user is actually using HTTPS, so client-side scripts will need to handle that too.

ON the other hand, it is true, that I missed the SSL in your first message, so if you want to test SSL certs, it is also completely fine if you want to test it without a reverse proxy too, so you can make sure the app can handle that as well.

It doesn’t change the fact that if you want to use an environment variable in an nginx config, that is a templating issue, which could be solved by using a standard reverse proxy image, which can connect to PHP FPM as well, or an Apache HTTPD, depending on what wordpress image you are using.

But you can also check the description of the official nginx image:

https://hub.docker.com/_/nginx

Which describes how you can use env variables

Using environment variables in nginx configuration (new in 1.19)

Out-of-the-box, nginx doesn’t support environment variables inside most configuration blocks. But this image has a function, which will extract environment variables before nginx starts.

Here is an example using docker-compose.yml:

web:
  image: nginx
  volumes:
   - ./templates:/etc/nginx/templates
  ports:
   - "8080:80"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=80

By default, this function reads template files in /etc/nginx/templates/*.template and outputs the result of executing envsubst to /etc/nginx/conf.d.

So if you place templates/default.conf.template file, which contains variable references like this:

listen       ${NGINX_PORT};

outputs to /etc/nginx/conf.d/default.conf like this:

listen       80;

For more info, please, check the description, because there is more about the templates.

in this thread (https://forums.docker.com/t/nginx-cannot-access-files-from-wordpress-container-and-presents-a-403/143953/7), I think you’re answered also and the OP used nginx and not a reversed proxy. So why it worked for him and not for me ?
also if you check the docker-compose.yml file you will see he used variable for hostname in nginx container.
So why my situation is different than his ?
we have the same purpose and using the same container technologies (php/wordpress/mysql/phpadmin)

I quickly checked the topic, so I could miss something, but it seems we didn’t discuss the hostname there, so it is possible it didn’t work, just nobody noticed becuse it didn’t matter.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.