简体   繁体   中英

Unable to connect NGINX docker container to localhost php server

Basically, I have a NGINX container and i'm trying to redirect a request made to it to a standard php server php -S localhost:8000 ( that is outside the container ). I've done a research and apparently I had to use host.docker.internal, which itself points to 172.17.0.1 as its the bridge network gateway. I'm new to both NGINX and docker, can you guys help me on where my configuration is flawed? Already tried to change the value of "host-gateway" to my private IP, to my public IP, to 172.17.0.1 and nothing seems to work!

I want the request made to localhost:80 ( which docker nginx is listening ) to redirect the request to localhost:8080 ( working fine ) and then redirecting again to localhost:8000 ( get a 502 bad gateway error )

Here's my configs:

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"

default.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;
    }

}

custom.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;
}

Intranet IP should be used, like this:

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;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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