我已经创建了一个包含nginx服务器的Docker映像(手动安装,而不是使用nginx映像)。无论从哪里访问web服务器,我总是将127.0.0.1
视为客户端IP地址。我用系统的nginx作为反向代理对容器进行了正面处理,同样如此。我从公共IP访问nginx容器,始终是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:
容器的nginx配置:
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;
}
}
在PHP中,我正确地看到了到客户端IP的REMOTE_ADDR
,对于相同的请求,在nginx中,我看到了127.0.0.1!
发布于 2022-11-12 03:02:19
这是nginx的错误配置。我没有说这个映像是基于Debian的,它通过apt
安装了nginx。我查看了图像日志,只看到了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
相反,与往常一样,完整的日志是在/var/log/nginx/access.log
中。:/
https://unix.stackexchange.com/questions/724541
复制相似问题