[英]Connect host nginx and msql with a docker container of php5-fpm
[英]Unable to connect NGINX docker container to localhost php server
基本上,我有一個 NGINX 容器,我正在嘗試將對其發出的請求重定向到標准 php 服務器php -S localhost:8000
(在容器外部)。 我做了一項研究,顯然我必須使用 host.docker.internal,它本身指向 172.17.0.1 作為其網橋網關。 我是 NGINX 和 docker 的新手,你們能幫我看看我的配置哪里有缺陷嗎? 已經嘗試將“host-gateway”的值更改為我的私有 IP、我的公共 IP 到 172.17.0.1,但似乎沒有任何效果!
我希望向 localhost:80 發出請求(其中 docker nginx 正在監聽)將請求重定向到 localhost:8080(工作正常),然后再次重定向到 localhost:8000(得到 502 錯誤網關錯誤)
這是我的配置:
docker-compose.yml
version: '3'
services:
server:
image: nginx
ports:
- "80:80"
- "8080:8080"
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./custom.conf:/etc/nginx/conf.d/custom.conf
extra_hosts:
- "host.docker.internal:host-gateway"
默認.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://localhost:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
自定義.conf
server {
listen 8080;
server_name localhost;
location / {
root /var/home/html;
index index.html;
}
location ~ \.php$ {
proxy_pass http://host.docker.internal:8000;
}
error_page 404 400 401 /error.html;
}
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
應該使用內網IP,像這樣:
server {
listen 80;
listen [::]:80;
server_name localhost;
# Modify localhost to 172.17.0.1
location / {
proxy_pass http://172.17.0.1:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.