Мне нужна небольшая помощь, чтобы разобраться, как настроить балансировщик нагрузки с обратным проксированием в nginx. По сути, у меня есть два веб-приложения, которые находятся в подкаталогах на серверах Apache, например / flavors / Chocolate и / flavors / Vanilla. Это приложение работает на нескольких серверах для аварийного переключения, поэтому мой список вышестоящих серверов для Chocolate выглядит следующим образом:
upstream Chocolate {
ip_hash;
server 192.168.10.100;
server 192.168.10.101;
server 192.168.10.102;
}
Теперь я хочу иметь возможность принимать запросы на балансировщике нагрузки 192.168.10.99 для https: //chocolate.company.com и прокси передают их вышестоящим серверам через порт 80 (http) в их фактические местоположения по адресу 192.168.10.xxx/flavors/Chocolate без перезаписи URI для сайта из https://chocolate.company.com.
Вот что у меня есть (это устраняет ошибки слева и справа от меня):
upstream Chocolate {
ip_hash;
server 192.168.10.100;
server 192.168.10.101;
server 192.168.10.102;
}
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
### server port and name ###
listen chocolate.company.com:443;
ssl on;
server_name chocolate.company.com;
### SSL log files ###
access_log logs/ssl-access.log;
error_log logs/ssl-error.log;
### SSL cert files ###
ssl_certificate /.pki/chocolate.company.com.crt;
ssl_certificate_key /.pki/chocolate.company.com.key;
### Add SSL specific settings here ###
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers RC4:HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
keepalive_timeout 60;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
### We want full access to SSL via backend ###
location / {
rewrite ^(.*)$ /flavors/Chocolate break;
proxy_pass http://chocolate.company.com;
### force timeouts if one of backend is died ##
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_50$
### Set headers ####
proxy_set_header Accept-Encoding "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
### Most PHP, Python, Rails, Java App can use this header ###
#proxy_set_header X-Forwarded-Proto https;##
#This is better##
proxy_set_header X-Forwarded-Proto $scheme;
add_header Front-End-Https on;
### By default we don't want to redirect it ####
proxy_redirect off;
}
Может кто-нибудь помочь мне здесь? Я чувствую, что упускаю что-то действительно глупое и просто не имею этой «эврики»! Момент, который я получаю после того, как наварил кое-что, и подумал, что есть шанс, что кто-то из вас намного лучше справится с Nginx, чем я (что почти не достигнуто вообще). Заранее спасибо!
Думаю, проблема только в proxy_pass
директива. Вы упоминаете http://chocolate.company.com
где вы фактически должны использовать здесь имя вышестоящей группы. Я изменил название вышестоящей группы, чтобы лучше документировать изменение:
# this is where all requests should be proxied to
upstream chocolate_upstream {
ip_hash;
server 192.168.10.100;
server 192.168.10.101;
server 192.168.10.102;
}
# this is a redirect to send all requests to https instead - optional
server {
listen 80;
return 301 https://$host$request_uri;
}
# this is the actual configuration
server {
### server port and name ###
listen chocolate.company.com:443;
ssl on;
server_name chocolate.company.com;
### log files for both access and errors ###
access_log logs/ssl-access.log;
error_log logs/ssl-error.log;
### SSL cert files ###
ssl_certificate /.pki/chocolate.company.com.crt;
ssl_certificate_key /.pki/chocolate.company.com.key;
### Add SSL specific settings here ###
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers RC4:HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
keepalive_timeout 60;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
### We want full access to SSL via backend ###
location / {
# this must use the name of the upstream group - mandatory
# no need to rewrite but we can add the URI path here as well
proxy_pass http://chocolate_upstream/flavors/Chocolate;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_50$
proxy_set_header Accept-Encoding "";
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-Forwarded-Proto $scheme;
add_header Front-End-Https on;
proxy_redirect off;
}