简体   繁体   English

NGINX 反向代理到 docker 应用程序

[英]NGINX reverse proxy to docker applications

I am currently learning to set up nginx but I am already having an issue.我目前正在学习设置 nginx,但我已经遇到了问题。 There are gitlab and nextcloud running on my vps and both are accessible with the right port.我的 vps 上运行着 gitlab 和 nextcloud,两者都可以通过正确的端口访问。 Therefore I created a nginx config with a simple proxy_pass command but I always reveice 502 Bad Gateway .因此,我使用简单的proxy_pass命令创建了一个 nginx 配置,但我总是收到502 Bad Gateway Nextcloud, Gitlab and NGINX are docker container and NGINX has port 80 opened. Nextcloud、Gitlab 和 NGINX 是 docker 容器,NGINX 开放了 80 端口。 The remaining two containers are having port 3000 and 3100 opened.剩下的两个容器打开了端口 3000 和 3100。

/etc/nginx/conf.d/gitlab.domain.com.conf /etc/nginx/conf.d/gitlab.domain.com.conf

upstream gitlab {
    server x.x.x.x:3000;
}
server {
    listen 80;
    server_name gitlab.domain.com;
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass http://gitlab/;
     }
}

/var/logs/error.log /var/logs/error.log

2018/04/12 08:10:41 [error] 7#7: *1 connect() failed (113: Host is unreachable) while connecting to upstream, client: xx.201.226.19, server: gitlab.domain.com, request: "GET / HTTP/1.1", upstream: "http://xxx.249.7.15:3000/", host: "gitlab.domain.com"
2018/04/12 08:10:42 [error] 7#7: *1 connect() failed (113: Host is unreachable) while connecting to upstream, client: xx.201.226.19, server: gitlab.domain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://xxx.249.7.15:3000/favicon.ico", host: "gitlab.domain.com", referrer: "http://gitlab.domain.com/

What is wrong with my configuration?我的配置有什么问题?

I think you could get away with a config way simpler than that.我认为您可以使用比这更简单的配置方式。

Maybe something like this:也许是这样的:

http {
 ...
server {
listen 80;
charset utf-8;
...

location / {
  proxy_pass http://gitlab:3000;
}
}
}

I assume you are using docker's internal DNS for accessing the containers for example gitlab points to the gitlab containers internal IP.我假设您使用 docker 的内部 DNS 来访问容器,例如 gitlab 指向 gitlab 容器的内部 IP。 If that is the case then you can open up a container and try ping the gitlab container from the other container.如果是这种情况,那么您可以打开一个容器并尝试从另一个容器 ping gitlab 容器。 For example you can ping the gitlab container from the nginx container like this:例如,您可以像这样从 nginx 容器 ping gitlab 容器:

 $ docker ps (use this to get the container id)

Now do:现在做:

$ docker exec -it <container_id_for_nginx_container> bash
# apt-get update -y
# apt-get install iputils-ping -y
# ping -c 2 gitlab

If you can't ping it then it means the containers have trouble communicating with each other.如果您无法 ping 通,则意味着容器之间无法相互通信。 Are you using docker-compose?你在使用 docker-compose 吗? If you are then I would suggest look at the "links" keyword which is used to link containers that should be able to communicate with each other.如果您是,那么我建议您查看“链接”关键字,该关键字用于链接应该能够相互通信的容器。 So for example you would probably link the gitlab container to postgresql.例如,您可能会将 gitlab 容器链接到 postgresql。

Let me know if this helps.如果这有帮助,请告诉我。

Another option that uses the advantage that your Docker containers are just processes in an isolated own control group is to bind each process (container) to a port on the host network (instead of an isolated network group).另一个利用 Docker 容器只是独立控制组中的进程的优势的选项是将每个进程(容器)绑定到主机网络上的端口(而不是独立的网络组)。 This bypasses Docker routing, so beware of the caveat that ports may not overlap on the host machine (no different than any normal process sharing the same host network.这会绕过 Docker 路由,因此请注意端口可能不会在主机上重叠(与共享同一主机网络的任何正常进程没有什么不同)的警告。

You mentioned running Nginx and Nextcloud (I assume you are using the nextcloud fpm image because of FastCGI support).您提到运行 Nginx 和 Nextcloud(我假设您使用的是 nextcloud fpm 映像,因为它支持 FastCGI)。 In this case, I had to do the following on my Arch Linux machine:在这种情况下,我必须在我的 Arch Linux 机器上执行以下操作:

  1. /usr/share/webapps/nextcloud is bounded (bind mounted) to the container at /var/www/html . /usr/share/webapps/nextcloud被绑定(绑定安装)到/var/www/html的容器。
  2. The UID of both host and container process must be the same (in my case, user host http and container www-data are UID=33)主机和容器进程的 UID 必须相同(在我的例子中,用户主机http和容器www-data的 UID=33)
  3. The 443 server block in nginx.conf must set root to the host's nextcloud path, root /usr/share/webapps/nextcloud; nginx.conf中的443服务器块必须设置root为宿主机的nextcloud路径, root /usr/share/webapps/nextcloud; . .
  4. The FastCGI script path for each server block that calls php-fpm over FastCGI must be adjusted to refer to the Docker container's Nextcloud base path, fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;每个通过 FastCGI 调用 php-fpm 的服务器块的 FastCGI 脚本路径必须调整为引用 Docker 容器的 Nextcloud 基本路径, fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; . . In other words, you cannot use $document_root as you normally would, because this points to the host's nextcloud root path.换句话说,您不能像往常一样使用$document_root ,因为它指向主机的 nextcloud 根路径。
  5. Optional: Adjust paths to database and Redis in the config.php file to not use localhost , rather the hostname of the host machine.可选:在config.php文件中调整数据库和 Redis 的路径,不使用localhost ,而是使用主机的主机名。 localhost seems to reference the container's host despite having been bound to the host machine's main network.尽管localhost已绑定到主机的主网络,但它似乎引用了容器的主机。

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

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