Restheart前端Nginx代理配置

Nginx proxy configuration in front of Restheart

我是 运行 nginx 作为前端反向代理,在 docker(群,单节点)容器中,在 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/...

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

任何有关 look/search 的提示或指示将不胜感激? (已经用谷歌搜索并抓了几天头发了……;-)

泽维尔

# 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

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

我们过去在 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