V
V
vitaly_742022-02-15 09:53:09
Docker
vitaly_74, 2022-02-15 09:53:09

What am I doing wrong with nginx + php reverse proxy setup?

I'm trying to set up docker for a project that has api - php, and a front on js, it turned out to give statics, but there were problems with php, php requests are not processed. Below is the config, in the Virtual_root section - the web folder, because for backing I use yii2.

version: '2'

services:
  proxy:
    image: 'jwilder/nginx-proxy'
    container_name: 'proxy'
    ports:
      - '81:80'
    volumes:
      - './nginx/vhosts:/etc/nginx/vhost.d'
      - './nginx/config:/etc/nginx/conf.d'
      - '/var/run/docker.sock:/tmp/docker.sock:ro'
      - './nginx/certs:/etc/nginx/certs'

  app1:
    image: nginx
    container_name: js
    volumes:
      - '../frontend:/usr/share/nginx/html'
    environment:
      VIRTUAL_HOST: lit.my
    networks:
      - proxy

  app3:
    image: php:7.3-fpm
    container_name: php
    volumes:
      - '../backend:/var/www/html/'
    environment:
      VIRTUAL_HOST: api.lit.my
      VIRTUAL_ROOT: '/web'
    networks:
      - proxy
      
networks:
  proxy:
    driver: bridge

Here are the logs that the server gives:
172.20.0.1, server: api.lit.my, request: "GET / HTTP/1.1", upstream: "http://api.lit.my/", host: "api.lit.my:81"
proxy    | nginx.1     | api.lit.my 172.20.0.1 - - [15/Feb/2022:06:47:01 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36" "api.lit.my"
proxy    | nginx.1     | 2022/02/15 06:47:01 [error] 37#37: *1 no live upstreams while connecting to upstream, client: 172.20.0.1, server: api.lit.my, request: "GET /favicon.ico HTTP/1.1", upstream: "http://api.lit.my/favicon.ico", host: "api.lit.my:81", referrer: "http://api.lit.my:81/"
proxy    | nginx.1     | api.lit.my 172.20.0.1 - - [15/Feb/2022:06:47:01 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://api.lit.my:81/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36" "api.lit.my"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vitaly_74, 2022-02-15
@vitaly_74

Solved the problem next. way:

version: '2'

services:
  proxy:
    image: 'jwilder/nginx-proxy'
    container_name: 'proxy'
    ports:
      - '81:80'
    volumes:
      - './nginx/vhosts:/etc/nginx/vhost.d'
      - './nginx/config:/etc/nginx/conf.d'
      - '/var/run/docker.sock:/tmp/docker.sock:ro'
      - './nginx/certs:/etc/nginx/certs'

  nginx1:
    image: nginx
    container_name: nginx-static
    volumes:
      - '../frontend:/usr/share/nginx/html'
    environment:
      VIRTUAL_HOST: lit.my

#    networks:
#      - proxy

  nginx2:
    image: nginx
    container_name: nginx-php
    volumes:
#      - '../backend:/usr/share/nginx/html'
      - './nginx/config.yii2:/etc/nginx/conf.d'
    environment:
      VIRTUAL_HOST: api.lit.my
    links:
      - php
    volumes_from:
      - php

  php:
    image: php:7.3-fpm
    container_name: php
    volumes:
      - '../backend:/var/www/'

and nginx-php config:
server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80;
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name api.lit.my;
    root        /var/www/web;
    index       index.php;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    #если хотите красивую страницу 404
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass php:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    location ~* /\. {
        deny all;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question