docker - 显示默认站点的容器化反向代理
问题描述
无论我做什么,我都会遇到我的网站发布默认 nginx 网站的问题。我正在尝试对我的网络服务器进行 docker 化,以便它可以指向在另一个容器中运行的家庭助理。当两者都托管在同一个 raspi 上时,我已经能够让它工作,而不是在容器中运行,但当两者都在容器中运行时却不行。
我已经附加了我用来启动环境的 nginx.conf、Dockerfile 和 default.conf。在过去的两天里,我一直在寻找试图做类似事情的人,但我认为我犯了一个如此愚蠢的错误,以至于大多数人都能自己解决。
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;
#}
}
解决方案
问题是因为这条线
proxy_pass http://localhost:8123;
在容器中运行时,您应该了解它localhost
是指nginx
容器而不是 docker 主机。
因此,您应该更改localhost
为 docker 主机的主机名或使用docker-compose
,以便您可以将其更改为定义的容器的名称。
如果您只是单独运行容器,您现在也可以只使用容器 IP,但请注意,每次重新启动容器时它都会更改。
推荐阅读
- jquery - jquery 和 bootstrap 导航栏在 Angular 7 中折叠或展开子菜单的布局中不起作用
- r - 为什么在我将变量从因子转换为数字后值发生了变化
- ado.net - 如何从 Web API 接收的 xml 文件中提供数据表?
- python - 创建名称存储在配置文件中的对象
- javascript - 使用 D3 力模拟作为纯函数
- c# - 如何将 IBM Watson 的 Text-To-Speech 与 Unity 结合使用?
- reactjs - React Router 动态传递 props
- wix - 如何解决错误:light.exe 错误 LGHT1105
- elasticsearch - 使用 JS SDK 的 ElasticSearch 批量 API
- python - 错误:TypeError:不能将序列乘以“float”类型的非整数