Nginx作为反向代理服务301

前端之家收集整理的这篇文章主要介绍了Nginx作为反向代理服务301前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我对Nginx没什么经验.我试图将它用作运行节点的几个docker容器的反向代理.目标是通过Nginx汇集所有请求.根据路由(url路径),某个路由domain.com/graphql将通过Nginx传递到不同的docker容器. domain.com/graphql基本上是我的API端点.

我遇到的问题是生活在客户端的JS所做的所有Ajax / Relay客户端请求都是从Nginx301传递的

请求:

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发出的请求的行为与对domain.com的请求具有相同的HTTP状态,但是会传递给我的api服务器.

最佳答案
301是上游服务器将请求的URL http://domain.com/graphql重定向到http://domain.com/graphql/.您可以在发布的标题中看到这一点.更改您的客户端以请求带有斜杠的URL,并查看301是否消失.或者更改您的上游服务器,使其不会重定向到带有斜杠的URL.

猜你在找的Nginx相关文章