【问题标题】:containerized reverse proxy showing default site显示默认站点的容器化反向代理
【发布时间】:2018-11-11 01:12:20
【问题描述】:

无论我做什么,我都会遇到我的网站发布默认nginx网站的问题。我正在尝试对我的网络服务器进行 docker 化,以便它可以指向在另一个容器中运行的家庭助理。当两者都托管在同一个 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;
    #}
}

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    问题是因为这条线

    proxy_pass http://localhost:8123;
    

    在容器中运行时,您应该了解localhost 指的是nginx 容器,而不是docker 主机。

    因此,您应该将 localhost 更改为您的 docker 主机的主机名,或者使用 docker-compose 以便您可以将其更改为定义的容器的名称。

    如果您只是单独运行容器,您现在也可以只使用容器 IP,但请注意,每次重新启动容器时它都会更改。

    【讨论】:

    • 感谢您的提示。我认为这可能是问题所在,所以我构建了一个 docker-compose.yml 文件。缩短版:version: '3' services: homeassistant: networks: - hass nginx: networks: - hass networks: hass: 我还将 localhost 行更改为:http://<stackname>_homeassistant:8123; 它运行..但它仍然没有转发到我的其他容器...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多