can onurer
can onurer

Reputation: 31

Change nginx server name in Docker

I have a project running on docker. I use Nginx reverse proxy to run my app. All works fine but trying to personalize the server_name on nginx but couldn't figure out how.

Docker yml file I've added server name to /etc/hosts by docker

version: "3"

services:
  nginx:
    container_name: nginx
    volumes:
      - ./nginx/logs/nginx:/var/log/nginx
    build:
      context: ./nginx
      dockerfile: ./Dockerfile
    depends_on:
      - menu-app
    ports:
      - "80:80"
      - "433:433"
    extra_hosts:
      - "www.qr-menu.loc:172.18.0.100"
      - "www.qr-menu.loc:127.0.0.1"
    networks:
      default:
        ipv4_address: 172.18.0.100

  menu-app:
    image: menu-app
    container_name: menu-app
    volumes:
      - './menu-app/config:/var/www/config'
      - './menu-app/core:/var/www/core'
      - './menu-app/ecosystem.json:/var/www/ecosystem.json'
      - './menu-app/tsconfig.json:/var/www/tsconfig.json'
      - './menu-app/tsconfig-build.json:/var/www/tsconfig-build.json'
      - "./menu-app/src:/var/www/src"
      - "./menu-app/package.json:/var/www/package.json"
    build:
      context: .
      dockerfile: menu-app/.docker/Dockerfile
    tmpfs:
      - /var/www/dist
    ports:
      - "3000:3000"
    extra_hosts:
      - "www.qr-menu.loc:127.0.0.1"
      - "www.qr-menu.loc:172.18.0.100"
networks:
  default:
    ipam:
      driver: default
      config:
        - subnet: 172.18.0.0/24

And I have Nginx conf

server_names_hash_bucket_size 1024;

upstream local_pwa {
    server menu-app:3000;
    keepalive 8;
}
server {
            listen 80 default_server;
            listen [::]:80 default_server;
            server_name www.qr-menu.loc 172.18.0.100;
            proxy_buffer_size 128k;
            proxy_buffers 4 256k;
            proxy_busy_buffers_size 256k;
            location / {
                proxy_set_header    X-Forwarded-For $remote_addr;
                proxy_pass http://local_pwa/;
            }
        }

but unfortunately, app runs on localhost instead of www.qr-menu.loc

I couldn't figure out how to change server_name on Nginx.

Upvotes: 1

Views: 4526

Answers (1)

chrisinmtown
chrisinmtown

Reputation: 4324

This is a really, really late answer. The server_name directive tells nginx which configuration block to use on receipt of a request. Also see: http://nginx.org/en/docs/http/server_names.html

I think the docker-compose extra_hosts directive might only work for domain-name resolution within the docker network. In other words, on your computer that's running docker the name "www.qr-menu.loc" is not available, but in a running docker container that name should be available.

Upvotes: 0

Related Questions