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

标签 linux nginx docker

我的应用程序需要在端口 :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/

相关文章:

c - fcntl,lockf,哪个更好用于文件锁定?

python - 在 python 中格式化输出

apache - Confluence 中的 JIRA 问题/过滤器宏生成无效链接

ssl - 是否可以在带有 NginX 的 Odoo 中使用 SSL 以避免标准端口(80 和 443)?

php - Nginx 和 PHP-cgi - 无法 file_get_contents 服务器上的任何网站

docker - 将文件放在 GKE 上的 Kubernetes 持久卷存储中

macos - 需要将 docker 从一台 mac 移动到另一台 mac

linux - 合并分割的 tar.gz 文件的某些部分 Linux 命令

docker - 运行时错误 : concurrent poll() invocation using celery

linux - 排序命令不适用于大文件