简体   繁体   English

无法 map Nginx 端口 80 与 docker 容器端口 80

[英]Not able to map Nginx Port 80 with docker container port 80

I'm running a NodeJS app that is running on port 4242 and here's the dockefile, docker-compose and nginx conf looks like this我正在运行一个在端口 4242 上运行的 NodeJS 应用程序,这是 dokefile、docker-compose 和 nginx conf 看起来像这样

Dockerfile Dockerfile

FROM node:12-alpine
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
EXPOSE 4242
CMD node index.js.

Docker Compose Docker 组成

services:
   web:
      build: .
   ports:
      - "80:4242"
   environment:
   DATABASE_URL: <DB_URL>
   depends_on:
      - db
command: >
    npm run start"

NGINX NGINX

server {
listen 80;
server_name server_name;

location / {
        try_files $uri $uri/ =404;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_pass http://localhost:80;
}
}

I wanted to make this app accessible via port 80 through Nginx along with with basic auth on port 80 but I'm not able to map the port correctly.我想通过 Nginx 以及端口 80 上的基本身份验证使此应用程序可通过端口 80 访问,但我无法正确 map 端口。

EDIT: I always get port already in use error because Nginx by default is listening on port 80 whereas and in docker-compose I'm mapping 80 with the 4242 which Dockerfile is exposing编辑:我总是得到端口已经在使用错误,因为 Nginx 默认情况下正在侦听端口 80,而在 docker-compose 中,我将 80 与 4242 映射,其中 Z3254677A7917C6C01F55212F8Z6

Thanks in advance提前致谢

You can't have both nginx and docker container on port 80.您不能在端口 80 上同时拥有 nginx 和 docker 容器。

They must listen to different ports to avoid "port already in use error".他们必须监听不同的端口以避免“端口已在使用错误”。

You could map the container port 4242 to host port 4242: replace - "80:4242" by - "4242:4242" in docker-compose file.您可以 map 容器端口 4242 到主机端口 4242:在 docker-compose 文件中将- "80:4242"替换为- "4242:4242"

Restart your container with docker-compose restart web , you can check that your container is running with docker-compose ps (you'll see mapped ports at the same time)使用docker-compose restart web重新启动您的容器,您可以使用docker-compose ps检查您的容器是否正在运行(您将同时看到映射的端口)

Then, you have to change nginx conf accordingly:然后,您必须相应地更改 nginx conf:

server {
  listen         80;
  server_name    your_server_name;

  location / {
    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;
    proxy_pass http://localhost:4242;
  }
}

Notes:笔记:

  • I removed try_files as it was bypassing the proxy_pass instruction.我删除了try_files因为它绕过了proxy_pass指令。 Please provide context if you try to achieve something else with try_files .如果您尝试使用try_files实现其他目标,请提供上下文。
  • Replace your_server_name with your real server name.your_server_name替换为您的真实服务器名称。
  • reload nginx重新加载 nginx

In the end you have:最后你有:

  1. nginx (directly on host) listening on port 80, nginx(直接在主机上)监听80端口,
  2. location block with basic auth,具有基本身份验证的location块,
  3. forwarding traffic to host port 4242,将流量转发到主机端口 4242,
  4. which is mapped to docker container port 4242 with proxy_pass ,它使用proxy_pass映射到 docker 容器端口 4242 ,
  5. Finally hitting your NodeJS app.最后点击你的 NodeJS 应用程序。

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

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