有一个属于messaging
微服务的Nginx 容器在 Laravel 中带有服务器 IP 的172.19.0.8
HTTP 请求。服务器内的容器向另一台服务器发送 HTTP 请求。但目标服务器将请求者的 IP 地址识别为非172.19.0.8
服务器的 IP 地址。
我已经看到了这个问题问题不同。链接中给出的问题原因是设置了防火墙,而我的服务器中没有防火墙(仍然是测试服务器,尚未配置)。我还向另一台服务器发出了 HTTP 请求,但我无法访问该服务器。
这是 docker-compose 文件
version: '3'
networks:
common_network:
external: true
messaging_network:
name: messaging_network
driver: bridge
services:
messaging_php:
build:
context: ./dockerfiles
dockerfile: php8.1.Dockerfile
args:
uid: "${UID-1000}"
gid: "${GID-1000}"
container_name: "messaging_php"
image: messaging_php
restart: always
tty: true
networks:
- messaging_network
volumes:
- .:/var/www/html
ports:
- "${MESSAGING_PHP_PORT}:9000"
messaging_nginx:
build:
context: ./dockerfiles
dockerfile: nginx.Dockerfile
container_name: messaging_nginx
image: messaging_nginx
restart: always
tty: true
ports:
- "127.0.0.1:${MESSAGING_NGINX_PORT}:80"
networks:
- messaging_network
- common_network
volumes:
- .:/var/www/html
depends_on:
- messaging_php
messaging_mysql:
image: mysql:8.0.26
container_name: messaging_mysql
restart: always
tty: true
volumes:
- mysqldata:/var/lib/mysql
ports:
- "${MESSAGING_MYSQL_PORT}:3306"
environment:
MYSQL_USER: ${DB_USERNAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_HOST: '%'
SERVICE_TAGS: "messaging"
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
networks:
- messaging_network
volumes:
mysqldata:
driver: local
这是nginx.Docerfile
FROM nginx:stable-alpine
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/nginx.conf
和default.conf
server {
resolver 127.0.0.11;
listen 80;
server_name localhost;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
client_max_body_size 64M;
index 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; }
location ~ \.php$ {
fastcgi_pass messaging_php:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
include /etc/nginx/conf.d/services/reverse_proxy.conf;
location ~ /\.(?!well-known).* {
deny all;
}
}
和nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 50000;
proxy_connect_timeout 60000;
proxy_send_timeout 60000;
send_timeout 60000;
client_max_body_size 50000;
client_header_timeout 60000;
client_body_timeout 60000;
fastcgi_read_timeout 30000;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
我该如何修复该问题以便它找到服务器的 IP,而不是容器的 IP?
答案1
上午 11:34
添加
proxy_set_header 主机 $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-主机 $host:443; proxy_set_header X-Forwarded-服务器 $host; proxy_set_header X-Forwarded-端口 443; proxy_set_header X-Forwarded-Proto https;