L
L
lavezzi12019-10-17 07:21:52
Nginx
lavezzi1, 2019-10-17 07:21:52

How to configure nginx if nodejs server is mixed?

There is an application on nodejs (express), there are static URLs (for example mysite.com/:code) where you need to pull out the code and show either data from the database or make a redirect. And actually rest api.
Now nginx looks like this:

server {

        server_name ***.com www.***.com;

        root /var/www/html;
        index index.html

        gzip on;
        gzip_types text/plain application/xml text/css application/javascript;
        gzip_min_length 1000;

        location /api/ {
                proxy_set_header   X-Forwarded-For $remote_addr;
                proxy_set_header   Host $http_host;
                proxy_pass         http://***:3000; # set the adress of the Node.js instance here
        }

I give pages to /var/www/html/ and obviously I get 404 when I go to pages like mysite.com/:code
How to solve the problem? Give statics also through express? Then how to solve the problem that mysite.com/favicon.ico is also trying to pull off the API.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-10-17
@vshvydky

server {
    listen 80;

    location ~ /api {
      proxy_pass http://nodejs;
    }

    location ~ /files/(.*) {
      root /data;
    }

    location / {
      proxy_pass http://front;
    }

    location /some {
      proxy_pass http://some;
    }
  }

something like this depending on the architecture

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question