1

I've created a Docker image which contains nginx server (manually installed, not using the nginx docker image). Wherever I access the web server from, I always see 127.0.0.1 as client IP address. I fronted the container with system's nginx as a reverse proxy, same. I access the nginx container from the public IP, always 127.0.0.1.

version: "3.6"

networks:
  docker-network:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: "172.30.253.0/24"
          gateway: "172.30.253.1"
services:
  ##
  ##  WEBAPP container
  ##
  app-web:
    container_name: ${APP_NAME}_webapp
    image: "my/app:${APP_VERSION}"
    restart: unless-stopped
    expose:
      - "8000"
    ports:
      - 3380:8000
    networks:
      docker-network:
    depends_on:
      - docker-mysql
    environment:
      - DB_HOST=${APP_NAME}_db
      - DB_PORT=${MYSQL_PORT}
      - DB_USER=${MYSQL_USER}
      - DB_PASSWORD=${MYSQL_PASSWORD}
      - DB_NAME=${MYSQL_DATABASE}

  ##
  ##  DATABASE CONFIG
  ##
  docker-mysql:
    container_name: ${APP_NAME}_db
    image: "mariadb:10.6"
    restart: unless-stopped
    expose:
      - "3306"
    ports:
      - 3316:3306
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_PORT=${MYSQL_PORT}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
    volumes:
      - ./mysql/data:/var/lib/mysql:cached
      - ./mysql/conf/yetopen.cnf:/etc/mysql/conf.d/yetopen.cnf:ro,delegated
      - ./mysql-files:/var/lib/mysql-files
    networks:
      docker-network:

  ##
  ##  ADMINER
  ##
  adminer:
    container_name: ${APP_NAME}_adminer
    # Non official adminer with dblib included
    image: dehy/adminer:4.8.1
    restart: "no"
    environment:
      - ADMINER_DEFAULT_SERVER=${APP_NAME}_db
    ports:
      - 3382:8080
    networks:
      docker-network:

The container's nginx config:

server {
    listen 8000;
    root /var/www/web;
    index index.php;
    server_name _;

    # Set real IP address in the Docker network defined in docker-compose.yml
    set_real_ip_from  172.30.253.0/24;
    real_ip_header    X-Real-IP;

    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$ {
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
    location ~ /\.ht {
        deny all;
    }
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

In PHP I correctly see REMOTE_ADDR to the client's IP, and for the same request, in nginx log I see 127.0.0.1!

1 Answer 1

0

This was a misconfiguration of nginx. I omitted to say the image is a Debian-based one, with nginx installed via apt. I was looking at the image logs and only seeing access from 127.0.0.1:

mis_webapp   | 127.0.0.1 -  11/Nov/2022:20:33:24 +0000 "GET /index.php" 302
mis_webapp   | 127.0.0.1 -  11/Nov/2022:20:33:25 +0000 "GET /index.php" 200
mis_webapp   | 127.0.0.1 -  11/Nov/2022:20:33:28 +0000 "GET /index.php" 200
mis_webapp   | 127.0.0.1 -  11/Nov/2022:22:18:28 +0000 "GET /index.php" 302
mis_webapp   | 127.0.0.1 -  11/Nov/2022:22:18:29 +0000 "GET /index.php" 302

Instead, the full log is, as normally, in /var/log/nginx/access.log. :/

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 12, 2022 at 13:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .