1

我有一个在 Bluemix 上运行的 Java 后端应用程序,它运行良好。如果我去后端服务器地址:

http://backend.mybluemix.net/api/test

我得到了预期的回应。现在,我有一个前端应用程序,通过静态 buildpack 部署,可用于为静态资产提供服务,但我似乎无法将其正确代理到后端服务器。

这是我完整的 nginx.conf:

worker_processes 1;
daemon off;

error_log /home/vcap/app/nginx/logs/error.log;
events { worker_connections 1024; }

http {
  log_format cloudfoundry '$http_x_forwarded_for - $http_referer - [$time_local]
  "$request" $status $body_bytes_sent';
  access_log /home/vcap/app/nginx/logs/access.log cloudfoundry;
  default_type application/octet-stream;
  include mime.types;
  sendfile on;

  gzip on;
  gzip_disable "msie6";
  gzip_comp_level 6;
  gzip_min_length 1100;
  gzip_buffers 16 8k;
  gzip_proxied any;
  gzip_types text/plain text/css text/js text/xml text/javascript
  application/javascript application/x-javascript application/
  json application/xml application/xml+rss;

  tcp_nopush on;
  keepalive_timeout 30;
  port_in_redirect off; # Ensure that redirects don't include the internal container
  PORT - 63575
  server_tokens off;

  server {
    listen <%= ENV["PORT"] %> ;
    server_name localhost;

    location / {
      root /home/vcap/app/public;
      index index.html index.htm Default.htm;
    }
    location /api/ {
      proxy_pass      http://backend.mybluemix.net:80;
      proxy_redirect off;
      proxy_buffering off;
      proxy_set_header        Host            $host;
      proxy_set_header        X-Real-IP       $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }
}

我已经为 location 和 proxy_pass 尝试了各种 URI 组合。这是 nginx 安装的日志记录:

2016-01-12T10:05:13.785-0600[App/0]ERR[qtp1231318312-15]
    INFO spark.webserver.MatcherFilter -
    The requested route [/api/test/] has not been mapped in Spark
2016-01-12T10:07:21.551-0600[App/0]ERR[qtp1231318312-19]
    INFO spark.webserver.MatcherFilter -
    The requested route [/api/test/] has not been mapped in Spark
2016-01-12T10:07:24.679-0600[App/0]ERR[qtp1231318312-17]
    INFO spark.webserver.MatcherFilter -
    The requested route [/api/test/] has not been mapped in Spark
2016-01-12T10:13:14.335-0600[App/0]ERR[qtp1231318312-16]
    INFO spark.webserver.MatcherFilter -
    The requested route [/api/test/] has not been mapped in Spark

2016-01-12T10:13:14.325-0600[RTR/3]OUTbackend-1.mybluemix.net -
    [12/01/2016:16:13:14 +0000] "GET /api/test/ HTTP/1.1" 404 0 48
    "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36
    KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
    108.168.250.156:36668 x_forwarded_for:"108.214.198.71"
    x_forwarded_proto:"http" vcap_request_id:71f1eefe-72fd-43e3-5aaf-45d5b72eede0
    response_time:0.005718652 app_id:9cbede6c-69a9-4830-932d-b830f12ddb38 
    x_global_transaction_id:"4197164295"
2016-01-12T10:18:23.952-0600[RTR/1]OUTbackend-1.mybluemix.net -
    [12/01/2016:16:18:23 +0000] "GET /api/test HTTP/1.1" 200 0 7
    "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36
    (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
    108.168.250.153:48575 x_forwarded_for:"108.214.198.71"
    x_forwarded_proto:"http" vcap_request_id:c8d86b36-b3b9-4391-5e6d-9378ea3f36be
    response_time:0.006252107 app_id:9cbede6c-69a9-4830-932d-b830f12ddb38 
    x_global_transaction_id:"508431471"

最后一个看起来很有希望 - 得到了 200,但是当我去时我仍然得到一个错误:

http://frontend.mybluemix.net/api/test/

错误是这样的:

502 Bad Gateway

编辑 1 我注意到在某些配置中,请求会到达后端服务器,但 URI 附加了一个斜杠,这会导致服务器中断。

http://frontend.mybluemix.net/api/test

变成

http://frontend.mybluemix.net/api/test/
4

0 回答 0