简体   繁体   English

Docker + Nginx:使proxy_pass起作用

[英]Docker + Nginx: Getting proxy_pass to work

I'm having a problem trying to get Nginx to proxy a path to another server that is also running in Docker. 我在尝试让Nginx将路径代理到另一个也在Docker中运行的服务器时遇到问题。

To illustrate, I'm using Nexus server as an example. 为了说明这一点,我以Nexus服务器为例。

This is my first attempt... 这是我的第一次尝试。

docker-compose.yml :- docker-compose.yml :-

version: '2'
services:
  nexus:
    image: "sonatype/nexus3"
    ports:
     - "8081:8081"
    volumes:
     - ./nexus:/nexus-data

  nginx:
    image: "nginx"
    ports:
    - "80:80"
    volumes:
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro

nginx.conf :- nginx.conf :-

worker_processes 4;
events { worker_connections 1024; }
http {
        server {
              listen 80;

              location /nexus/ {
                proxy_pass http://localhost:8081/;
              }
        }
}

When I hit http://localhost/nexus/ , I get 502 Bad Gateway with the following log:- 当我点击http://localhost/nexus/ ,我得到502 Bad Gateway并显示以下日志:

nginx_1  | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://[::1]:8081/", host: "localhost"
nginx_1  | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "localhost"
nginx_1  | 172.18.0.1 - - [29/May/2017:02:20:50 +0000] "GET /nexus/ HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"

In my second attempt..., 我第二次尝试...

docker-compose.yml - I added links to Nginx configuration:- docker-compose.yml我添加了指向Nginx配置的links :-

version: '2'
services:
  nexus:
    image: "sonatype/nexus3"
    ports:
     - "8081:8081"
    volumes:
     - ./nexus:/nexus-data

  nginx:
    image: "nginx"
    ports:
    - "80:80"
    volumes:
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    links:
    - nexus:nexus

nginx.conf ... Instead of using http://localhost:8081/ , I use http://nexus:8081/ :- nginx.conf ...我使用http://nexus:8081/ :-而不是使用http://localhost:8081/

worker_processes 4;
events { worker_connections 1024; }
http {
        server {
              listen 80;

              location /nexus/ {
                proxy_pass http://nexus:8081/;
              }
        }
}   

Now, when I hit http://localhost/nexus/ , it gets proxied properly but the web content is partially rendered. 现在,当我点击http://localhost/nexus/ ,它会被正确代理,但是Web内容会被部分渲染。 When inspecting the HTML source code of that page, the javascript, stylesheet and image links are pointing to http://nexus:8081/[path] ... hence, 404. 检查该页面的HTML源代码时,javascript,样式表和图像链接指向http://nexus:8081/[path] ...因此是404。

What should I change to get this to work properly? 我应该改变些什么才能使其正常工作?

Thank you very much. 非常感谢你。

The following additional options are what I have used 以下是我使用的其他选项

http {
    server {
          listen 80;

          location /{
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    Host $http_host;
            server_name_in_redirect on;
            proxy_pass      http://nexus:8081;

          }

          location /nexus/ {
            proxy_pass          http://nexus:8081/;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    Host $http_host;
            server_name_in_redirect on;
          }
    }

} }

My solution is to include the redirect for the '/' path in the nginx config. 我的解决方案是在nginx配置中包括对“ /”路径的重定向。 The Nexus app will be making requests to '/' for it resources which will not work. Nexus应用将向其请求“ /”,以请求无法使用的资源。

However, this is not ideal and will not work with an Nginx configuration serving multiple apps. 但是,这不是理想的选择,不适用于为多个应用程序提供服务的Nginx配置。

The docs cover this configuration and indicate that you need to configure Nexus to serve on /nexus . 文档介绍了此配置,并指出您需要配置Nexus才能在/nexus上投放。 This would enable you to configure Nginx as follows (from docs) minus the hack above. 这将使您能够按照以下方式(从docs)配置Nginx(减去上述hack)。

location /nexus {
  proxy_pass http://localhost:8081/nexus;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

I would recommend using that configuration. 我建议使用该配置。

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

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