【问题标题】:Nginx proxy configuration in front of RestheartRestheart前面的Nginx代理配置
【发布时间】:2017-11-07 11:10:54
【问题描述】:

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

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

作为后端,我使用的是 Restheart 的标准 mvn 构建 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

【问题讨论】:

    标签: nginx proxy docker-swarm restheart


    【解决方案1】:

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

    【讨论】:

    • 谢谢@mturatti。你(间接地)帮助我找到了答案。与其说是正则表达式还是非正则表达式,但在该位置之外的各种 proxy_xxx 指令的分解是一个问题。我将它移动到位置块内,复制,现在可以正常工作了。不知道为什么因式分解适用于 /api 和 /_logic 而不是 /browser 或 /_authtokens ...
    【解决方案2】:

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

    【讨论】:

    • 谢谢安德里亚。如果我移动位置没有变化。我不想删除,因为最终,我想在根目录下提供来自 nginx 的静态内容,而无需通过 restheart/undertow ...
    猜你喜欢
    • 2020-09-05
    • 2019-11-12
    • 2015-10-20
    • 1970-01-01
    • 2012-08-05
    • 2020-01-25
    • 2014-03-12
    • 1970-01-01
    相关资源
    最近更新 更多