1

I have dockerized symfony app that I access on my machine via 127.0.0.1:8000

it runs via docker compose with an nginx container and a app container like so

nginx:
    container_name: nginx
    image: "${NGINX_IMAGE}"
    build: build/nginx
    restart: always
    env_file: .env
    ports:
      - "8000:443"
    volumes:
      - "./build/nginx/build/${MODE}.conf:/etc/nginx/conf.d/default.conf:ro"
      - "./build/nginx/build/certs:/etc/nginx/certs"
      - "${APP_HOST_DIR}/public:/var/www/app/public:ro"
    networks:
      - app_network
    depends_on:
      - app

  app:
    container_name: app
    image: "${APP_IMAGE}"
    restart: always
    build: build/app
    env_file: .env
    networks:
      - app_network
    volumes:
      - "${APP_HOST_DIR}:${APP_CONTAINER_DIR}"

the nginx conf is

server {
        listen 80;
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name localhost;

        ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
        ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
        
        ssl_protocols TLSv1.2 TLSv1.1 TLSv1;

        root /usr/share/nginx/html/;

        index index.html index.nginx-debian.html;
}

Now I would like to use a separate nginx container to access my website from any device on my network via a custom port like 192.168.1.12:8100 so that from the app point of view it thinks it is coming from local machine and I don't need to modify all symfony settings like trusted hosts, my php and js variables.

at the moment I get untrusted host 192.168.1.12. I don't want to add a new host because I also have to modify other files to enable a single ip. I would like nginx to proxy somehow.

Here is what I have got:

docker-compose 2

version: "3"
services:

  nginx:
    container_name: nginx_lan_access
    image: "nginx"
    build: build/nginx
    restart: always
    ports: 
      - 8100:8000 
    volumes:
      - "./default.conf:/etc/nginx/conf.d/default.conf:ro"


default.conf 2

server {
    listen 8100 default;
    listen [::]:8100 default;
    server_name 192.168.1.12;

    location / {
        proxy_pass https://127.0.0.1:8000;
    }
}
jotyhista
  • 57
  • 1
  • 1
  • 7

1 Answers1

0

You use same port for nginx and container published port. Change like this

ports: 
  - 8080:8000


server {
    listen 8100 default;
    listen [::]:8100 default;
    server_name 192.168.1.12;

    location / {
        proxy_pass https://127.0.0.1:8080;
    }
}

You connects to 192.168.1.12:8100, nginx listen 8100, proxy it to 8080->8000.

Also if needed with iptables allow incoming and outgoing traffic to/from containers network (by default bridged 172.16.0.0/12 network or check ip addr).

gapsf
  • 846
  • 1
  • 6
  • 12
  • I tried and it is not working for me. I updated my question to give more details. – jotyhista Aug 28 '22 at 21:12
  • Just wondering : why you do not install nginx on a host? Why this docker-mania is? – gapsf Aug 28 '22 at 21:32
  • What you have in iptables, ip route, io addr? – gapsf Aug 28 '22 at 21:36
  • I have not added anything extra myself so it should be default. I run my docker on OSX. sorry Im not very informed on which command to run; let me know if I can run a specific command – jotyhista Aug 29 '22 at 07:09