V
V
verweb2019-09-25 10:37:02
Nginx
verweb, 2019-09-25 10:37:02

How to set up proxying in nginx for several applications?

There are 3 running node.js applications
1. NUXT (client side) at 127.0.0.1:3000
2. API (express.js) at 127.0.0.1:8080
3. NUXT (admin panel) at 127.0.0.1:3001
In nginx I write:

location / {
    proxy_pass http://127.0.0.1:3000;
}
location /api/v1 {
    proxy_pass http://127.0.0.1:8080;
}
location /admin {
    proxy_pass http://127.0.0.1:3001;
}

I can't understand:
1. How to properly configure nginx so that ajax requests from / and /admin go not from 127.0.0.1, but from the domain name specified in the configuration?
2. Now if you go to %domain_name/% and %domain_name/api/v1% then the proxy works and returns what you need. But when switching to /admin, the admin panel opens for a fraction of a second and then displays the application from 127.0.0.1:3000 (client side) with an error about a non-existent page.
I will be glad to any answers / links to useful information!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Talaiko, 2019-09-25
@devpav

Hello.

worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
       worker_connections  1024;
}

http {
      include       mime.types;
      default_type  application/octet-stream;

      #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
      #                  '$status $body_bytes_sent "$http_referer" '
      #                  '"$http_user_agent" "$http_x_forwarded_for"';

      #access_log  logs/access.log  main;

      sendfile        on;
      #tcp_nopush     on;

      #keepalive_timeout  0;
      keepalive_timeout  65;
      send_timeout 300;
      proxy_read_timeout 300;
      proxy_connect_timeout 300;

      gzip  on;
      gzip_vary on;
      gzip_min_length 10240;
      gzip_proxied expired no-cache no-store private auth;
      gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/json;

      server {
        listen 80;

        if ($scheme = https) {
            return 301 http://$host$request_uri;
        }
      }

      server {
            listen 80;

            server_name localhost ************ 127.0.0.1;

            add_header Last-Modified $date_gmt;
            add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
            if_modified_since off;
            expires off;
            etag off;

            location / {
               root /usr/share/nginx/html/;
               index index.html;
            }

            location /api {
                proxy_pass http://rest:8080/api;
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-for $remote_addr;
                proxy_redirect off;
            }
      }
}

Read:
nginx.org/ru/docs/beginners_guide.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question