S
S
SchmeL2016-10-03 18:28:54
Nginx
SchmeL, 2016-10-03 18:28:54

How to properly configure locations in nginx behind a balancer?

The scheme is approximately the following: There is site.ru, consisting of several applications - a site in php and java applications for a personal account. At the moment, a subdomain is configured for the cabinet and everything works - there is one location, which is directly proxied to the java application.

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

But there was a need to remove the application from the subdomain and make it accessible by url: https://site.ru/cabinet
If you use the settings below, then https://site.ru/cabinet opens without styles and js.
The fact is that in the java application itself there are paths like /static/jqery.js - to which it gives 404 and styles are not loaded.
The php part of the site works fine. Now the links lead to the php site https://site.ru/static/script.js Is it possible to make all links to static files look like https://site.ru/cabinet/static/script when visiting /cabinet .js ?
# Сервера с php приложением
upstream php {
ip_hash;
server 192.168.100.20 max_fails=2 fail_timeout=360 weight=10;
server 192.168.100.30 max_fails=2 fail_timeout=360;
}

# Сервера с java приложением
upstream cabinet {
ip_hash;
server 192.168.100.101 max_fails=2 fail_timeout=360;
}

# Балансировщик, если запрашивают / - отправляет на php, если /cabinet - на java приложение
server {
listen 443 ssl;
server_name site.ru;

ssl on;
ssl_certificate         /etc/nginx/ssl/site.ru.crt;
ssl_certificate_key     /etc/nginx/ssl/site.ru.key;
ssl_protocols                   TLSv1 TLSv1.1 TLSv1.2;

   location / {
  proxy_pass http://php;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_http_version 1.1;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_next_upstream error timeout invalid_header http_500 http_503 http_504 http_502;
  proxy_connect_timeout 50;
  proxy_read_timeout 50;
  }

    location /cabinet {
  proxy_pass http://cabinet;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_http_version 1.1;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_next_upstream error timeout invalid_header http_500 http_503 http_504 http_502;
  proxy_connect_timeout 50;
  proxy_read_timeout 50;
  }
}

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DuD, 2016-10-03
@DuD

options 2.
Or in the cabinet location, use a regulator to exclude statics so that it gets to /
Or make a /cabinet/static location and direct it to where the statics lie.

S
SchmeL, 2016-10-04
@SchmeL

Or, in the location of the cabinet, exclude statics with a regulator so that it gets on /

How about an example? Just the same with regular expressions and the problem.
The second option disappears, since the statics are generally on another machine, and the construction of the form
location /cabinet/static {
  proxy_pass http://cabinet;

didn't work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question