1

我将 nginx 作为前端反向代理运行,在 docker(swarm,单节点)容器中,在 Restheart 应用程序前面,提供 restheart REST 服务。

我在 Alpine 上为 ngninx 使用最新的 docker 镜像。配置文件附在下面。

作为后端,我使用的是 Restheart 的标准 mvn build uberJar,其中大多数情况下只有 REST 资源访问的根已移至 /api 而不是 /。这在直接访问时非常有效。

当我直接在(打开调试)8080 端口上测试上游服务器( Restheart )时,所有 REST api 都按预期工作。

我使用 httpie 进行测试。

当我通过 nginx 在端口 8081 上使用相同的请求时,/api/... 和 /_logic 路径/... 被捕获并按预期工作。

由于我无法理解的原因, /_authtokens/... 和 /browser/ 路径没有传递到上游 Restheart 服务器,但最终被第一个块捕获,试图在本地找到具有该名称的文件文件系统,位于 /usr/share/nginx/html/...

真正困扰我的是,我不明白为什么它在前两条路径上完美运行,而不是在其他两条路径上?!

任何关于在哪里寻找/搜索的提示或指示将不胜感激?(现在已经用谷歌搜索并抓挠我的头发几天了...... ;-)

泽维尔

# Configuration file for the nginx front-end container.

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}




http {

    sendfile        off;
    keepalive_timeout  65;
    gzip  on;
    include       /etc/nginx/mime.types;
    # default_type  application/json;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  stdout  main;
    error_log stderr debug;


    upstream docker-restheart {
        server   myrestheart:8080;
    }

    server {
        listen 8081 ;   
        proxy_pass_request_headers      on;
        proxy_pass_request_body         on;
        proxy_http_version 1.1;
        proxy_redirect     default;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;  



        # Default will serve the index as a test.
        # This is also where non allocated locations will end up,
        # because it is the first block ..
        location / {            
            root /usr/share/nginx/html/;           
            }


        # Restheart api - ok
        location /api/  {
            proxy_pass         http://docker-restheart;            
            }

        # Restheart _logic - ok
        location /_logic/  {
            proxy_pass         http://docker-restheart;
            }

        # Restheart _authtokens - does not work ?
       location /_authtokens/  {
            proxy_pass         http://docker-restheart;
            }

        # Restheart browser - does not work ?
       location /browser/  {
            proxy_pass         http://docker-restheart;
            }



        # Restheart _authtokens - does not work ?  
        # Restheart browser - does not work ?
        # In both cases, no transfer to upstream happening, 
        # but instead, caught by the first block ...


    }
}

# This compose file defines the docker stack for swarm deployement
#

version: "3"

networks:
  myoverlay:
    driver: overlay

volumes:
    dbdata:

services:
  mymongo:
    image: "mvertes/alpine-mongo:latest"
    command: ["mongod","--quiet"]
    ports:
      - 27017:27017
    deploy:
      replicas: 1
    volumes:
      - dbdata:/data/db/
    networks:
      - myoverlay

  myrestheart:
    image: openjdk:8-jre-alpine
    volumes:
      - ./target/MyRestheart-1.0-SNAPSHOT.jar:/myjar.jar:ro
    ports:
      - 8080:8080
    command: ["java","-Dmongo=mongodb://mymongo:27017", "-jar", "/myjar.jar"]
    deploy:
      replicas: 1
    networks:
      - myoverlay

  mynginx:
    image: nginx:alpine    
    ports:
      - 8081:8081    
    volumes:
      - ./nginx.conf:/nginx.conf
    command: ["nginx", "-g","daemon off;","-c","/nginx.conf"]
    deploy:
      replicas: 1
    networks:
      - myoverlay
4

2 回答 2

0

尝试删除或移动到location /nginx conf 文件中的条目的末尾。

于 2017-11-08T07:53:29.110 回答
0

我们过去在 Nginx 配置中遇到过类似的问题。在我们的例子中,我们转移到位置的正则表达式语法,并且它有效。就像是:

location ~ /(api|browser|_logic|ping|_authtokens) {
    proxy_pass http://docker-restheart;
}

参考:https ://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms

于 2017-11-08T09:12:02.520 回答