我正在尝试为我的 docker 容器获取主机名,因为我只能为此使用反向代理,所以我试图在 nginx 的帮助下完全实现这一点。
一个 docker 容器是一个 web 服务,它将端口 8080 暴露给我的本地主机。
所以我可以通过以下方式访问网络服务器:
http://localhost:8080
相反,我宁愿使用:
http://webservice.local
因此我添加到我的/etc/hosts
127.0.0.1 webservice.local
然后我安装了 nginx 并添加到/etc/nginx/sites-available/default
:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location webservice.local {
proxy_pass http://localhost:8080
}
重新加载 nginx 后,尝试在浏览器 ERR_CONNECTION_REFUSED
中打开时出现以下错误。http://webservice.local
我做错了什么?如何正确设置反向代理?
我不确定这是正确的语法。尝试这样的事情:
沿着这些路线的东西..
但是,如果您只想将端口 8080 重定向到 80,为什么不使用 socat 之类的网络实用程序呢?
然后,您应该在 nginx 中为每个上游添加虚拟主机,并将这些虚拟主机添加到 DNS 或 /etc/hosts 中,它们都将解析为 localhost。
或者您可以避免上游并使用虚拟主机,如下所示: