1

I try to setup a web server with Docker, so I will use the main domain of my server "server.domain.com" for admin use (server.domain.com/phpmyadmin, ect...) and I want to redirect all the other domain to an apache container who listen on port 81. So I have this code on my default.conf:

server {
    listen 80;
    listen [::]:80 default_server;

    location / {
       proxy_pass  http://web/;
    }
}

main.conf:

server {
    listen 80;
    listen [::]:80;

    server_name server.domain.com;

    location /phpmyadmin/ {
       proxy_pass http://phpmyadmin/;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

(Updated conf)

And my nginx.conf:

user  nginx;
worker_processes  2;

error_log  /var/log/nginx/error.log warn;
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;
    gzip_comp_level   5;
    gzip_http_version 1.0;
    gzip_min_length   0;
    gzip_types        *;
    gzip_vary         on;


    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites/*;
}

a part of ‘docker-compose.yml‘:

nginx:
    build: ./server/proxy
    ports:
        - "80:80"
    #volumes:
       #- nginx_conf:/etc/nginx/
    networks:
        - web_network
    depends_on:
        - web
        - phpmyadmin
        - panel

At this moment I use "depends_on" for use the name of the container on my config but you talk only about network so I think "depends_on" is not obliged ?

But that gives me an error connection refused. If I replace the 127.0.0.1 by server.domain.com the first vhost not working and redirect to nginx webRoot.

So I have no idea why ...

Thank you !

4

1 回答 1

0

As far as I understand this nginx container is listening on port 80 and all connection requests going to your machine will be passed to it. So it's a proxy container only. I have a project with similar implementation. Let's try to make it out.

I suggest that you have 2 conf files for clarity.

1) main.conf - will serve your "server.domain.com"

server {
    listen 80;
    listen [::]:80;

    server_name server.domain.com;

    location /phpmyadmin {
       proxy_pass http://server.domain.com:82;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

That's all of the configuration you basically need here. Later if you need them, you'll pass headers.

2) default.conf - will serve any other domain

server {
    listen 80;
    listen [::]:80 default_server;

    location / {
       proxy_pass  http://server.domain.com:81;
    }
}

This configuration assumes that:

1) There is a container running apache and requests coming to your machine on port 81 will be passed to apache2 container's port 80 (or whatever it's listening to)

2) There is a container running phpmyadmin and requests coming to your machine on port 82 will be passed to phpmyadmin container's port 80 (or whatever...)

SOME IMPROVEMENTS YOU SHOULD CONSIDER:

1) If you start all those containers with docker-compose you'll be able to set up a virtual network for them. This would allow you to proxy pass requests straight to the container by name. In my project I do it like:

proxy_pass http://adminer;

where adminer is defined as:

adminer:
    image: phpmyadmin/phpmyadmin
    volumes:
        - ./db_interface/conf/config.inc.php/:/etc/phpmyadmin/config.inc.php
    networks:
     - demo_webnet
     - prod_webnet

If you have questions just ask, I'll explain.

2) You could place another nginx server together with your apache2 server in its container. They work nice in bundle. Nginx is better to server statics. Apache2 better suits PHP in your case. I can show you how to do that as well.

IN CASE YOU NEED IT

It looks like you're trying to do something similar to what I did for our company's needs. If you're interested I can give you access to my project. I've built up a whole server infrastructure with docker and it now perfectly deployed on our server. In short it works as follows:

  • nginx proxy container above all
  • a container with apache2-nginx-php5.6 for demo apps
  • a container with apache2-nginx-php7.0 for demo apps
  • a container with apache2-nginx-php5.6 for production apps
  • a container with apache2-nginx-php7.0 for production apps
  • a container with maria db for demo
  • a container with maria db for production
  • a container with phpmyadmin to access both db services

  • Any request comes to nginx proxy.

  • It matches some virtual host and gets proxied to one of the 4 containers that have apache2 and nginx inside.

Also there is a lot of cool stuff like cron configured to autoreload apache2 and nginx when it detects changes to files, supervising services, https support and so on..

I'm planning to further develop it as an open source project, whoever is interested should let me know.

于 2017-12-06T22:26:00.327 回答