简体   繁体   English

使用另一个应用程序在Docker中运行Nginx

[英]run nginx in docker with another application

I have my application that needs to serve on port :5000 我的应用程序需要在端口:5000

Here's my dockerfile 这是我的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

And I run 然后我跑

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

And here's my nginx config file: 这是我的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;
    }
}

Then I expect this to redirect traffic from the webserver (port 80 ) to my app port 5000 , but seems like nginx doesn't do the reverse proxy correctly. 然后,我希望这会将流量从Web服务器(端口80 )重定向到我的应用程序端口5000 ,但是似乎nginx不能正确执行反向代理。 Traffic do not get directed to my app. 流量不会定向到我的应用程序。

How do I set up nginx and my app in the same container so that I can use it as a reverse proxy? 如何在同一容器中设置nginx和我的应用程序,以便可以将其用作反向代理?

Thanks! 谢谢!

You mention in the nginx config: 您在nginx配置中提到了:

proxy_pass http://127.0.0.1:8000;

But you want to redirect to the EXPOSE'd port 5000. 但是您想重定向到EXPOSE的端口5000。

proxy_pass http://127.0.0.1:5000;

If it is a typo, then make sure you publish 80 to and host port (ot that you are using the container IP address directly. 如果是错字,请确保将80发布到主机端口(并直接使用容器IP地址)。

Don't forget that, if you areusing a VM, you might have to port-forward that published port: see " Connect to a Service running inside a docker container from outside " 别忘了,如果您正在使用VM,则可能必须端口转发该已发布的端口:请参阅“ 从外部连接到在Docker容器内运行的服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM