John Davidson

php - How to Direct multiple hosts from traefik 2 to one nginx

0 comments
Message:


I want to route test1.test and test2.test to different folders in an nginx php config using traefik 2. Both are PHP applications.


Here is my nginx site.conf:


server {
listen 80;

server_name test1.test
root /var/www/html/test1;
}
server {
listen 80;

server_name test2.test
root /var/www/html/test2;
}

and here is mij docker-compose file for the traefik container


version: "3"

services:
traefik:
image: "traefik:v2.3"
container_name: "traefik"
restart: "unless-stopped"
security_opt:
- no-new-privileges:true
networks:
- proxy
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /etc/localtime:/etc/localtime:ro
- ./traefik.toml:/traefik.toml:ro
- ./acme.json:/acme.json
- ./configurations:/configurations
networks:
proxy:
external: true
internal:
external: false

And the docker-compose for my applications


version: "3"

services:
nginx:
image: nginx:latest
container_name: test_nginx
links:
- php
volumes:
- ./nginx/server.conf:/etc/nginx/nginx.conf:ro
- ./nginx/site.conf:/etc/nginx/conf.d/default.conf:ro
- ../../src:/var/www/html
networks:
- proxy
- internal
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.nginx.entrypoints=web"
- "traefik.http.routers.nginx.rule=Host(`test1.test`) || Host(`test2.test`)"
php:
build: ./php
container_name: test_php
ports:
- "9000:9000"
volumes:
- ../../src:/var/www/html
networks:
- internal

My hostsfile is all set and pointing to localhost (172.0.0.1)


But when browsing to
test1.local it gives 404 file not found
and browsing to test2.test is gives 404 not found nginx.


What I want to do is basicly have one traefik2, one nginx with multiple drupal sites after it.


Can someone explain me how to have 2 hosts pointing to same nginx and redirecting to the correct folder? Thank you.



My Answer:
In your nginx configuration, you need to add a location block for each server_name to properly route the requests to the correct folder. Here's an updated version of your nginx site.conf:

nginx
server {
listen 80;

server_name test1.test;
root /var/www/html/test1;

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

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}

server {
listen 80;

server_name test2.test;
root /var/www/html/test2;

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

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}


In this configuration, we added location blocks for each server_name that handle PHP requests by passing them to the PHP-FPM container. Make sure to adjust the fastcgi_pass directive to match the name of your PHP container.

Additionally, in your traefik configuration, you need to update the labels for the nginx service to properly route the requests. Here's an updated version of your docker-compose file:

yaml
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.nginx.entrypoints=web"
- "traefik.http.routers.nginx.rule=Host(`test1.test`) || Host(`test2.test`)"
- "traefik.http.services.nginx.loadbalancer.server.port=80"


With these changes, traefik should properly route requests for test1.test and test2.test to the correct folders in your nginx configuration.

Rate this post

5 of 5 based on 3290 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us