【问题标题】:node.js proxy app in docker behind nginx proxynginx代理后面的docker中的node.js代理应用程序
【发布时间】:2017-01-28 21:11:04
【问题描述】:

我正在尝试将 nginx 代理设置为 node.js 代理到 docker 中的 aws elasticsearch 应用程序。

nginx version: nginx/1.10.0 - installed on the machine, not in docker
Docker version 1.12.5, build 047e51b/1.12.5
CentOS Linux release 7.3.1611 (Core)
docker run -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -p 127.0.0.1:9200:9200 mydocker/es_kibana_proxy -b 0.0.0.0 search-elasticsearch-dev-tdxka.us-east-1.es.amazonaws.com

netstat -nlp:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8431/nginx: master
tcp        0      0 127.0.0.1:9200          0.0.0.0:*               LISTEN      7963/docker-proxy-c

当我在本地计算机上运行容器并通过没有 nginx 的浏览器访问它时,它可以工作 - http://127.0.0.1:9200/_plugin/kibana/

nginx.conf:

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

conf.d]# cat default.conf

server {
    listen       80;
    server_name nginx.mydomain.com;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /es-kibana-dev { proxy_pass http://127.0.0.1:9200/_plugin/kibana/; }

    location /es-dev {

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass http://127.0.0.1:9200;
      proxy_redirect off;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

当我转到nginx.mydomain.com/es-kibana-dev 时,我可以看到 kibana 页面,但它卡住了,我得到 404,并且 error.log 中出现以下错误:

[error] 8234#8234: *1 open() "/usr/share/nginx/html/styles/main.css" failed (2: No such file or directory), client: 12.12.12.1, server: nginx.mydomain.com, request: "GET /styles/main.css?_b=7562 HTTP/1.1", host: "nginx.mydomain.com", referrer: "http://nginx.mydomain.com/es-kibana-dev"

error] 8234#8234: *1 open() "/usr/share/nginx/html/bower_components/requirejs/require.js" failed (2: No such file or directory), client: 12.12.12.1, server: nginx.mydomain.com, request: "GET /bower_components/requirejs/require.js?_b=7562 HTTP/1.1", host: "", referrer: "http://nginx.mydomain.com/es-kibana-dev"

[error] 8234#8234: *3 open() "/usr/share/nginx/html/require.config.js" failed (2: No such file or directory), client: 12.12.12.1, server: nginx.mydomain.com, request: "GET /require.config.js?_b=7562 HTTP/1.1", host: "nginx.mydomain.com", referrer: "http://nginx.mydomain.com/es-kibana-dev"

[error] 8234#8234: *4 open() "/usr/share/nginx/html/images/initial_load.gif" failed (2: No such file or directory), client: 12.12.12.1, server: nginx.mydomain.com, request: "GET /images/initial_load.gif HTTP/1.1", host: "nginx.mydomain.com", referrer: "http://nginx.mydomain.com/es-kibana-dev"

我尝试设置不同的代理到 es 索引,当我转到 http://nginx.mydomain.com/es-dev 时,我看到了:

{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"es-dev","index":"es-dev"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"es-dev","index":"es-dev"},"status":404}

我可以在我的本地主机上正确看到索引 - http://127.0.0.1:9200,没有 nginx。

{
  "name" : "Morbius",
  "cluster_name" : "elasticsearch-dev",
  "version" : {
    "number" : "2.3.2",
    "build_hash" : "72aa8010df1a4a359c9c588",
    "build_timestamp" : "2016-11-14T15:59:50Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

我感觉问题出在 nginx 配置上。

欢迎提出任何建议,

谢谢

【问题讨论】:

    标签: node.js nginx docker proxy


    【解决方案1】:

    以下块对我有用:

    location /es-kibana-dev {
      rewrite ^/es-kibana-dev/(.*) /_plugin/kibana/$1 break;
      proxy_pass http://localhost:9200;
    }
    
    location /es-dev {
      rewrite ^/es-dev/(.*) /$1 break;
      proxy_pass http://127.0.0.1:9200;
    }
    

    访问(不要忘记尾部斜杠):

    【讨论】:

    • 谢谢,但我仍然有同样的问题,如果你想知道,我正在使用这个 docker 容器https://github.com/santthosh/aws-es-kibana,如果我在机器内部 curl 到本地主机,我想通过 nginx 访问curl http://localhost:9200 我可以看到索引,但是从浏览器中我看到错误,另一个想法?谢谢!
    • 这是我从浏览器看到的,nginx 代理到 nodejs docker 和它的代理到 aws es
    • {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"es-dev","index":"es-dev"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"es-dev","index":"es-dev"},"status":404}
    • 在服务器内卷曲到本地主机显示容器正常工作
    • curl http://localhost:9200 { "name" : "Warlock", "cluster_name" : "elasticsearch-dev", "version" : { "number" : "2.3.2", "build_hash" : "", "build_timestamp" : "", "build_snapshot" : false, "lucene_version" : "5.5.0" }, "tagline" : "You Know, for Search" }
    【解决方案2】:
    location / {
            proxy_set_header X-Real-IP *privare_ip;
            proxy_http_version 1.1;
            proxy_set_header Connect "Keep-Alive";
            proxy_set_header Proxy-Connection "Keep-Alive";
            proxy_set_header Authorization "";
            proxy_pass http://127.0.0.1:9200;
            }
    

    两者都适用

    http://my.domain.com/ => http://127.0.0.1:9200;
    http://my.domain.com/_plugin/kibana  => http://127.0.0.1:9200/_plugin/kibana
    

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 2017-05-28
      • 1970-01-01
      • 2016-04-26
      • 2015-03-09
      • 2011-08-21
      • 2018-11-13
      • 1970-01-01
      相关资源
      最近更新 更多