繁体   English   中英

无法将 NGINX docker 容器连接到本地主机 php 服务器

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM