G
G
goodjinny2019-09-18 12:10:12
Nginx
goodjinny, 2019-09-18 12:10:12

How to make apache/nginx under docker work alongside local nginx?

Greetings dear experts!
The essence of the problem is as follows. I have a bunch of nginx + php7.2 + mysql running locally, which I use to develop sites. The task was to raise my ancient project, which was still written in php5.3. To do this, I found the necessary docker assembly (apache + php5.3 + mysql), which does not work at startup because there is a port conflict - port 80 is already taken by local nginx.
How can this problem be solved without removing and stopping the local nginx/mysql?
I am not a great expert in networking, so I turn to you for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2019-09-18
@goodjinny

I've also recently been working with docker.
I will share my experience.
I am using docker-compose.
For example, I need for a site to open external access, which is spinning somewhere in the docker.

# Development configuration
version: "3.1"

services:

  app:
    container_name: rusaddress.app
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile-dev
    volumes:
      - .:/www
      - ./docker/php/log:/var/log
    depends_on:
      - db
    links:
      - db
    expose:
      - 9000



  # Nginx api server
  nginx-api:
    container_name: rusaddress.nginx-api
    restart: always
    build: ./docker/nginx
    volumes:
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/sites-enabled/vhost-api.rusaddress.conf:/etc/nginx/sites-enabled/vhost-api.rusaddress.conf
    ports:
      - "127.0.0.150:8090:90"
    depends_on:
      - app
    expose:
      - "90"
    command: ["nginx", "-g", "daemon off;"]
...

Server configuration that is running in docker
/docker/nginx/sites-enabled/vhost-api.rusaddress.conf
server {
    listen                          90;

    client_max_body_size            208M;
    access_log                      /var/log/nginx/api.access.log;
    root                            /www/public;

    location / {
        try_files                   $uri $uri/ /api.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_split_path_info     ^(.+\.php)(/.+)$;
        fastcgi_pass                app:9000;
        fastcgi_index               api.php;
        include                     fastcgi_params;
        fastcgi_param               SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param               PATH_INFO $fastcgi_path_info;
    }
}

Now my site is available locally
at 127.0.0.150:8090
As I understand it, you have a problem with this.
Now we somehow need to open external access, for this we do the following ...
On a real host, configure nginx
server {
    listen      			%ip%:%httpport%;
    server_name 		api.rusaddress.local;
   
    access_log  			"%sprogdir%/userdata/logs/api.rusaddress.local.access.log combined";
    error_log   			"%sprogdir%/userdata/logs/api.rusaddress.local.error.log error";
  
  add_header				"Access-Control-Allow-Origin" "*";
  add_header				"Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization";
  add_header				"Access-Control-Request-Methods" "GET, POST, OPTIONS";

    location / {
  proxy_pass  		http://127.0.0.150:8090;	
        proxy_set_header	Host $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 $server_name;
    }
}

Pay attention to the address 127.0.0.150:8090
Now the site is available via api.rusaddress.local
api.rusaddress.local <---- this is your public address
In my case, local, as I did it for myself, for development.
Restored the picture of how it would work on a real server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question