обратный прокси в контейнере, показывающий сайт по умолчанию - PullRequest
0 голосов
/ 11 ноября 2018

Независимо от того, что я делаю, я продолжаю сталкиваться с проблемой, когда мой веб-сайт публикует веб-сайт nginx по умолчанию. Я пытаюсь докернизировать свой веб-сервер так, чтобы он мог указывать на домашний помощник, работающий в другом контейнере. Я смог заставить его работать, когда оба были размещены на одном и том же raspi, не работали в контейнерах, но не когда оба выполнялись в контейнерах.

Я приложил мои nginx.conf, Dockerfile и default.conf, которые я использовал для запуска среды. Я провел последние 2 дня в поисках кого-то, кто пытался сделать что-то подобное, но я предполагаю, что совершаю такую ​​глупую ошибку, что большинству удалось понять это самостоятельно.

nginx.conf:

user  nginx;
worker_processes  1;

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;

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

default.conf (/etc/nginx/conf.d/hass.conf)

    map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    # Update this line to be your domain
    server_name nekohouse.ca;

    # These shouldn't need to be changed
    listen [::]:80 default_server ipv6only=off;
    return 301 https://$host$request_uri;
}

server {
    # Update this line to be your domain
    server_name nekohouse.ca;

    # Ensure these lines point to your SSL certificate and key
    ssl_certificate fullchain.pem;
    ssl_certificate_key privkey.pem;
    # Use these lines instead if you created a self-signed certificate
    # ssl_certificate /etc/nginx/ssl/cert.pem;
    # ssl_certificate_key /etc/nginx/ssl/key.pem;

    # Ensure this line points to your dhparams file
    ssl_dhparam /etc/nginx/dhparams.pem;


    # These shouldn't need to be changed
    listen [::]:443 default_server ipv6only=off; # if your nginx version is >= 1.9.5 you can also add the "http2" flag here
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
    ssl on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

    proxy_buffering off;

    location / {
        proxy_pass http://localhost:8123;
        proxy_set_header Host $host;
        proxy_redirect http:// https://;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

default.conf (/etc/nginx/conf.d/default.conf)

server {
    listen       8100;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

1 Ответ

0 голосов
/ 11 ноября 2018

Проблема из-за этой строки

proxy_pass http://localhost:8123;

При работе в контейнерах вы должны понимать, что localhost относится к контейнеру nginx, а не к хосту докера.

Итак, вы должны либо изменить localhost на имя хоста вашего докера, либо использовать docker-compose, чтобы вы могли изменить его на имя определенного контейнера.

Если вы просто запускаете контейнеры отдельно, вы также можете просто использовать IP-адрес контейнера, но имейте в виду, что он будет меняться при каждом перезапуске контейнера.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...