Filtration
Filtration

Reputation: 125

Multiple nginx websites with one container

So, first let me explain what I am trying to do. I have 2 websites, a frontend and a backend, the frontend is just HTML and vue, which uses the backend to store information (an api)

Websites: - erp.test (frontend) - api.erp.test (backend; php, api)

docker-compose.yml

version: '3'

services:
    #web
    frontend:
        build:
            context: .
            dockerfile: ./environment/nginx/Dockerfile
        container_name: frontend
        restart: always
        ports:
            - 80:80
            - 442:442
        volumes:
            - ./environment/nginx/sites-enabled:/etc/nginx/sites-enabled
            - ./frontend/public:/usr/share/nginx/html/frontend
            - ./api:/usr/share/nginx/html/api
        links:
            - php
    php:
        build:
            context: .
            args:
                version: 7.3.0-fpm
            dockerfile: ./environment/php/Dockerfile
        container_name: php_backend
        restart: always
        depends_on:
            - mysql
    mysql:
        build:
            context: .
            args:
                version: 5.7
            dockerfile: ./environment/mysql/Dockerfile
        restart: always
        volumes:
            - ./environment/mysql/data:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: laravel
            MYSQL_DATABASE: laravel
        ports:
            - 13306:3306
    command:
        build:
            context: .
            dockerfile: ./environment/command/Dockerfile
        container_name: command
        restart: always
        command: "tail -f /dev/null"
        volumes:
            - ./frontend:/frontend

This uses the following files for the sites-enabled.

My dockerfile for the nginx environment is the following:

FROM nginx

Config files for the websites:

etc/nginx/sites-enabled/api.erp.test

server {
    listen 80;
    listen [::]:80;
    server_name api.erp.test;
    root /usr/share/nginx/html/backend/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.3.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

etc/nginx/sites-enabled/erp.test

server {

    listen 80;
    listen [::]:80;
    server_name erp.test;
    root /usr/share/nginx/html/frontend/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    charset utf-8;
}

Both of them files should (I assume) be enabled and work. I checked the container and the files are in the correct position, and I've even added the IP address of the container to my hosts file on my machine like so:

172.18.0.3 erp.test 172.18.0.3 api.erp.test

Whenever I visit them urls, it just goes to the default nginx url and not the specific websites. Any idea what I am doing wrong?

Upvotes: 0

Views: 951

Answers (1)

Shawn C.
Shawn C.

Reputation: 6841

I believe for nginx in docker the virtual host files need to go into /etc/nginx/conf.d not /etc/nginx/sites-enabled

So in your docker-compose.yml change

./environment/nginx/sites-enabled:/etc/nginx/sites-enabled

to

./environment/nginx/sites-enabled:/etc/nginx/conf.d

Upvotes: 1

Related Questions