Nginx with api Node

Asked

Viewed 231 times

2

I’ve had a problem for a while and I can’t fix it. The problem is this:

I have a server that has an api running PM2 on it at port 3000. I installed NGINX and am trying to link the route of my domain + path to perform the requests for api. I’d like to use a route similar to:

www.dominio.com.br/api

But I cannot configure NGINX to do this.

I set up NGINX this way:

location / {
   proxy_pass http://localhost:3000;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection 'upgrade';
   proxy_set_header Host $host;
   proxy_cache_bypass $http_upgrade;
}

And so it works, but with the route as follows:

www.dominio.com.br

I tried to put the /api in the complement of Location, thus:

location /api { OR location /api/ { but it didn’t work..

I tried tbm to use upstream and it didn’t work! I set it up this way:

upstream teste_api {
   server http://localhost:3000;
}

location /api {
   set $upstream teste_api;
}

And when I ran the command: sudo Nginx -t This error is presented: [emerg] "upstream" directive is not allowed here in /etc/nginx/sites-enabled/default:20

Somebody please help me?

  • Basically in the latest versions you would need to upstream normally in the configuration file (or part) and in your host file (or virtual) do the proxy_pass. Formless which version of Nginx vc uses?

  • Nginx version: Nginx/1.14.0 (Ubuntu)! Apparently upstream is not being recognized in the configuration file.

  • You could edit your question and add the excerpt from your upstream. Let tbm know if you get an error running the command: sudo Nginx -t

  • From what I’ve seen it’s a permission error! That’s right?

  • I believe, you need to remove the directive upstream out of the server block. It’s in your block server {}? This directive (upstream) must be allocated in the block http{}

  • Yes, it’s inside the server block! I’m going to do a test by placing it inside the http block. Location is inside http tbm?

  • Unfortunately tbm did not work, the error was now: [emerg] "http" Directive is not allowed here in /etc/Nginx/sites-enabled/default:1

  • Location/api should be inside the server block ... try to follow the example of the response I added and a feedback of what is resulting

Show 3 more comments

1 answer

2


Usually the directive upstream should be allocated to the block http {} in the Nginx configuration file (Nginx.conf) ... something like this:

Nginx.conf

http {
    ##
    # node socket upstream
    ##
    upstream teste_api {
      ip_hash;
      server http://localhost:3000;
    }
}

Already in your host configuration file (if it is unique) or your subdominios (virtual hosts) you must set the route within the block server {} .... similar to the following example:

server {
    ##
    # socket
    ##
    location /api {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://teste_api;
    }
}

Note that in the above example I am setting the headers of: upgrade, connection, version, "x-forwarded-for" and host ... you can set, omit numerous headers here but, these are the basics.

On the front end just call normally:

const socket = new WebSocket('ws://www.dominio.com.br/api')

Source:

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

  • It worked in parts! The setup worked! But I continued with the same error! If I leave only Location / - a request works normal, but if I add /api - a request does not work, it returns the error: { "message": "/api/users does not exist" }

  • I have other Locations that work almost well tbm. I have frontend Locations that works for example: Location/admin/ - then the route is www.dominio.com.br/admin/ - The only problem I have with this, is that I must put the last one / at the end of the route. rsrs

  • I made the examples for socket but, yes you can use for REST requests (get/post/del/put/...) the question is to treat them on your Node

  • 1

    Got it! Perfect! I got it 100%... Thank you very much, it was very helpful...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.