gpt4 book ai didi

linux - 使用另一个应用程序在 docker 中运行 nginx

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:57 25 4
gpt4 key购买 nike

我的应用程序需要在端口 :5000 上提供服务

这是我的 dockerfile

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
ADD ./nginx.conf /etc/nginx/sites-available/default
RUN service nginx restart

RUN go get github.com/a/mycmd

EXPOSE 5000

然后我跑

sudo docker run --publish 5000:5000 --rm app /go/bin/mycmd

这是我的 nginx 配置文件:

limit_req_zone $binary_remote_addr zone=limit:10m rate=2r/s;

server {
listen 80;

set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
server_name 123.13.13.13 example.com;

location / {
proxy_read_timeout 3000s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:5000;
limit_req zone=limit burst=5 nodelay;
}
}

然后我希望这会将流量从网络服务器(端口 80)重定向到我的应用程序端口 5000,但似乎 nginx 没有不要正确地做反向代理。流量不会定向到我的应用。

如何在同一容器中设置 nginx 和我的应用程序,以便将其用作反向代理?

谢谢!

最佳答案

您在 nginx 配置中提到:

proxy_pass http://127.0.0.1:8000;

但是你想重定向到 EXPOSE 的端口 5000。

proxy_pass http://127.0.0.1:5000;

如果是拼写错误,请确保将 80 发布到主机端口(否则您将直接使用容器 IP 地址。

不要忘记,如果您使用的是虚拟机,您可能必须转发已发布的端口:请参阅“Connect to a Service running inside a docker container from outside

关于linux - 使用另一个应用程序在 docker 中运行 nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35058369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com