Nginx 作为反向代理提供 301 服务

Nginx 作为反向代理提供 301 服务

我对 NGINX 的使用经验很少。我尝试将其用作运行节点的几个 docker 容器的反向代理。目标是所有请求都将通过 NGINX 进行传输。根据路由(url 路径),某个路由domain.com/graphql将通过 NGINX 传递到不同的 docker 容器。这domain.com/graphql基本上是我的 API 端点。

我遇到的问题是,客户端上的 JS 发出的所有 Ajax/Relay 客户端请求都被 NGINX 传递为 301

要求:

Request URL:http://domain.com/graphql
Request Method:POST
Status Code:301 Moved Permanently
Remote Address:192.168.99.100:80
Response Headers
view source
Connection:keep-alive
Content-Length:185
Content-Type:text/html
Date:Thu, 08 Sep 2016 15:14:02 GMT
Location:http://domain.com/graphql/
Server:nginx/1.11.3
Request Headers
view source
accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,it;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Content-Length:620
content-type:application/json
Host:nomralph.com
Origin:http://domain.com
Pragma:no-cache
Referer:http://domain.com/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36

Nginx 配置:

   upstream frontend {
                least_conn;
                server frontend:4444 weight=10 max_fails=3 fail_timeout=30s;
                keepalive 64;
        }

       upstream graphql-upstream {
              least_conn;
              server graphql:3000 weight=1 max_fails=3 fail_timeout=30s;
              keepalive 64;
       }

        server {
              listen 80;
              server_name domain.com www.domain.com;
              root  /var/www/public;
              # Handle static files

              location / {
                  proxy_pass            http://frontend;
                  proxy_http_version    1.1;
                  proxy_set_header      Upgrade $http_upgrade;
                  proxy_set_header      Connection 'upgrade';
                  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-NginX-Proxy    true;
                  proxy_cache_bypass    $http_upgrade;
              }

             location /graphql {
                  proxy_pass graphql-upstream/graphql;
                  add_header 'Access-Control-Allow-Origin' '*';
                  add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                  proxy_http_version    1.1;
                  proxy_set_header      Upgrade $http_upgrade;
                  proxy_set_header      Connection 'upgrade';
                  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-NginX-Proxy    true;
                  proxy_cache_bypass    $http_upgrade;
             }


        }

我如何更改 NGINX 中的配置,以允许对发出的请求domain.com/graphql以与对发出的请求相同的 HTTP 状态运行,domain.com但传递给我的 api 服务器。

答案1

301 是上游服务器将请求的 URL 重定向http://domain.com/graphqlhttp://domain.com/graphql/。您可以在您发布的标头中看到这一点。将您的客户端更改为请求带有尾部斜杠的 URL,看看 301 是否消失。或者更改您的上游服务器,使其不重定向到带有尾部斜杠的 URL。

相关内容