10

I have little experience with NGINX. I am trying to use it as a reverse proxy for a few docker containers running node. The goal is all requests will be funneled through NGINX. Based on routes(url path), a certain route domain.com/graphql will then be passed to a different docker container via NGINX. The domain.com/graphql is basically my API endpoint.

The problem I have is all of my Ajax/Relay client requests being made by the JS living on the client are getting passed as a 301 from NGINX

Request:

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 config:

   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;
             }


        }

How can I change my configuration in NGINX to allow the requests made to domain.com/graphql to behave with the same HTTP status as request's made to domain.com but be passed to my api servers.

jackncoke
  • 223
  • 1
  • 3
  • 9

1 Answers1

9

The 301 is the upstream server redirecting the requested URL http://domain.com/graphql to http://domain.com/graphql/. You can see this in the headers you posted. Change your client to request the URL with the trailing slash and see if the 301 goes away. Alternately change your upstream server such that it does not redirect to the URL with the trailing slash.

mab
  • 218
  • 1
  • 4
  • This save the day! Thanks. – Son Tr. Feb 27 '20 at 02:58
  • see trailing slash issue answer on this question: https://stackoverflow.com/questions/53787116/why-is-my-nginx-reverse-proxy-doing-a-301-redirect-instead-of-proxying which links to http://nginx.org/en/docs/http/ngx_http_core_module.html#location    """If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, ....""" – gaoithe Apr 23 '21 at 12:51