Настройте Nginx в качестве обратного прокси с Apache в контейнере Docker - PullRequest
1 голос
/ 19 июня 2019

У меня есть работающий контейнер, созданный с помощью файла Docker. Работающий контейнер имеет Apache, SSH-сервер и Ubuntu. Теперь я хочу установить Nginx в качестве обратного прокси-сервера с Apache в Docker-контейнере. Поэтому я установил Nginx и запустил Nginx. Теперь оба сервера работают на разных портах. Но это делает мой сайт недоступным. Сайт будет работать только тогда, когда я остановлю Nginx и запусту Apache с портом 80.

Файл конфигурации Apache в ** / etc / apache2 / sites-enabled папка **

<VirtualHost *:81>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /root_folder

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Содержимое ** reverse-proxy.conf в / etc / nginx / sites-enabled / folder **

server {
listen 443;
server_name 10.1.2.181;
add_header X-Frame-Options "SAMEORIGIN";




location / {
proxy_cache cacheone;
proxy_cache_revalidate on;
proxy_cache_min_uses 3;
proxy_cache_use_stale error timeout updating http_500 http_502
http_503 http_504;
# proxy_cache_background_update on;
proxy_cache_lock on;
proxy_pass      https://127.0.0.1:8443;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|exe|pdf|doc|xls|ppt|txt|odt|ods|odp|odf|tar|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ {
root           /root_path;

expires        max;
try_files      $uri @fallback;
}
}

location @fallback {
proxy_pass  https://127.0.0.1:80;
}

location ~ /\.ht    {return 404;}
location ~ /\.svn/  {return 404;}
location ~ /\.git/  {return 404;}
location ~ /\.hg/   {return 404;}
location ~ /\.bzr/  {return 404;}
}

Содержимое файла Nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

         gzip_vary on;
         gzip_proxied any;
         gzip_comp_level 6;
         gzip_buffers 16 8k;
         gzip_http_version 1.1;        
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;


        # Proxy Cache Settings
        proxy_cache_path /var/cache levels=1:2 keys_zone=cacheone:60m inactive=90m max_size=1000m;
        #proxy_cache_path /var/cache/nginx levels=2 keys_zone=cacheone:10m inactive=6s max_size=1024m;
        ##
        # Virtual Host Configs
        ##

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

Содержимое файла Docker, которое я использовал для создания образа контейнера, приведено ниже.

FROM ubuntu:18.04

ENV DEBIAN_FRONTEND=noninteractive




    RUN apt-get update && apt-get install -yq --no-install-recommends \
        apt-utils \
        curl \
        # Install git
        git \
        # Install apache
        apache2 \
        # Install php 7.2
        libapache2-mod-php7.2 \
        php7.2-cli \
        php7.2-json \
        php7.2-curl \
        php7.2-fpm \
        php7.2-gd \
        php7.2-ldap \
        php7.2-mbstring \
        php7.2-mysql \
        php7.2-soap \
        php7.2-sqlite3 \
        php7.2-xml \
        php7.2-zip \
        php7.2-intl \
        php-imagick \
        # Install tools
        openssl \
        nano \
        graphicsmagick \
        imagemagick \
        ghostscript \
        mysql-client \
        iputils-ping \
        locales \
        sqlite3 \
        ca-certificates \
        openssh-server \
        && echo "root:Docker!" | chpasswd \
        && apt-get clean && rm -rf /var/lib/apt/lists/*



    # Install composer
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

    # Set locales
    RUN locale-gen en_US.UTF-8 en_GB.UTF-8 de_DE.UTF-8 es_ES.UTF-8 fr_FR.UTF-8 it_IT.UTF-8 km_KH sv_SE.UTF-8 fi_FI.UTF-8

    COPY sshd_config /etc/ssh/
    COPY init_container.sh /bin/init_container.sh
    RUN chmod 755 /bin/init_container.sh
    EXPOSE 80 443 2222
    ENTRYPOINT ["/bin/init_container.sh"]
...