title: How to setup dynamic subdomains in docker with NGINX
body: <p>Basically, I need to have <strong><em>dynamic subdomains</em></strong>, so the site should be available at any subdomain in <strong>Docker</strong> like this:</p>
  
  <p><code>admin.example.com</code>
  <code>adrian.example.com</code>
  <code>files.example.com</code> .</p>
  
  <p>I don't have a fixed number of subdomains, so I can't just put them all in the hosts file.</p>
  
  <p>Server_name also didn't help: <code>server_name www.$hostname;</code></p>
  
  <p>They should all point to the same website.
  I've tried <strong><em>jwilder</em></strong> reverse proxy, but wasn't able to set it up correctly.</p>
  
  <p>I have a <code>docker-compose.yml</code> and <code>Dockerfile</code>.</p>
  
  <p>Could someone give me a working code that I could use, and then change it for my needs. And if I need to change my hosts file also.</p>
  
  <p>I did some research, but my nginx and docker knowledge is not enough.</p>
  
  <p><strong><em>Nginx.conf</em></strong></p>
  
  <pre><code>server {
      server_name .example.local;
  
      listen 80 default;
      client_max_body_size 1008M;
      access_log /var/log/nginx/application.access.log;
      error_log  /var/log/nginx/error.log;
      root /application/web;
      index index.php;
  
      if (!-e $request_filename) {
          rewrite ^.*$ /index.php last;
      }
  
      location ~ \.php$ {
          fastcgi_pass php-fpm:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
          include fastcgi_params;
          fastcgi_buffers 16 16k;
          fastcgi_buffer_size 32k;
          proxy_buffer_size   128k;
          proxy_buffers   4 256k;
          proxy_busy_buffers_size   256k;
      }
  
  }
  
  </code></pre>
  
  <p><strong><em>Dockerfile</em></strong></p>
  
  <pre><code>FROM phpdockerio/php73-fpm:latest
  
  RUN mkdir /application
  WORKDIR "/application"
  COPY . /application
  
  # Fix debconf warnings upon build
  ARG DEBIAN_FRONTEND=noninteractive
  
  RUN apt-get update                                                                                                  &amp;&amp; \
      # Installing packages
      apt-get -y --no-install-recommends --assume-yes --quiet install                                                    \
      nano curl git ca-certificates ruby-dev gcc automake libtool rubygems build-essential make php-pear                 \
      php7.3-mysql php7.3-bcmath php-imagick php7.3-intl php7.3-gd php-yaml php7.3-soap php7.3-dev mysql-client       &amp;&amp; \
      # Xdebug
      pecl install xdebug                                                                                             &amp;&amp; \
      # Cleaning up after installation
      apt-get clean &amp;&amp; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
  </code></pre>
  
  <p><strong><em>docker-compose.yml</em></strong></p>
  
  <pre><code>version: "3.1"
  services:
  
    db:
      image: mysql:5.6
      container_name: ls-db
      working_dir: /application
      volumes:
        - .:/application:cached      # User-guided caching
        - ./phpdocker/sql:/docker-entrypoint-initdb.d
      environment:
        MYSQL_DATABASE: ls
        MYSQL_USER: drupal
        MYSQL_PASSWORD: drupal
        MYSQL_ROOT_PASSWORD: root
      ports:
        - "6006:3306"
      networks:
        - ls
  
    web:
      image: nginx:alpine
      container_name: ls-webserver
      working_dir: /application
      volumes:
        - .:/application:cached      # User-guided caching
        - ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      ports:
        - "6060:80"
      networks:
        - ls
  
    php-fpm:
      build: phpdocker/php-fpm
      container_name: ls-php-fpm
      working_dir: /application
      volumes:
        - .:/application:cached      # User-guided caching
        - ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini
      networks:
        - ls
  
  networks:
    ls: # this network (app1)
      driver: bridge
  
  volumes:
    db:
  </code></pre>