现有三台机器: A:load balancer【只安装 nginx】 B: app server [rails + passenger] C: app server [rails + passenger]
按照 nginx 官方的配置教程:http://nginx.org/en/docs/http/load_balancing.html, load-balancer 的配置文件如下:
server {
listen 80;
server_name ip_of_a;
location / {
proxy_pass http://backend;
access_log /var/log/nginx/proxy.log;
#include /etc/nginx/proxy_params;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
upstream backend {
least_conn;
server ip_of_b;
server ip_of_c;
keepalive 20;
}
现在问题是一直没法正确代理到 b 和 c,大家如果有这方面的经验请帮忙!谢谢
@huacnlee 具体的表现是 appserver 已经收到了 load balancer 发过来的请求,但是 load balancer 还是只出现 ngxin 的欢迎页面,具体的日志如下:比如我从浏览器访问 load balancer, load balancer 的访问日志如下:
125.71.203.92 - - [27/Apr/2015:22:12:32 -0400] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
app server 的访问日志如下:
107.170.216.243 - - [27/Apr/2015:22:12:32 -0400] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
收到了请求,但是浏览器始终返回 nginx 的欢迎界面. 请帮我看看
load balancer?你说 Nginx?不要用那么多没用的名词,怎么访问的,用 IP 那个 IP,Nginx 上面 server_name 配置的那个 IP?Nginx 配置贴出来,不要修改成什么 ip_of_a
之类的,把实际内容贴出来。
按我给那个连接里面,一楼我的回复内容对一下你的配置,看看差别在哪里
按你说的看到 Nginx 欢迎页面,应该是在 你贴出来这个 server 节点之前还有其他的 server 节点,所以前面的抢先了。
server {
listen 80;
server_name 107.170.216.243;
location / {
proxy_pass http://backend;
access_log /var/log/nginx/proxy.log;
#include /etc/nginx/proxy_params;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
upstream backend {
server 192.241.225.37;
server 192.241.207.103;
}
这个是现在完整的配置
@huacnlee 192.241.225.37、192.241.207.103 是两个独立的 app server,环境是 rails + passenger,107.170.216.243 只装有 nginx,用来做 nginx 的 load balancer。我是直接访问 107.170.216.243 只出现了 nginx 的欢迎页面。
@huacnlee 最新的配置文件为
upstream backend {
server 192.241.225.37;
server 192.241.207.103;
}
server {
listen 80;
server_name 107.170.216.243;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_pass http://backend;
access_log /var/log/nginx/proxy.log;
#proxy_http_version 1.1;
#proxy_set_header Connection "";
}
}
前面也没有server { .. }
现在还是显示 nginx 的默认页面!
Nginx 重启了么,重启前最好 用 configtest 命令检测一下是否重启成功。
sudo /etc/init.d/nginx configtest
另外的问题!
192.241.225.37;
192.241.207.103;
这两个机器 80 端口跑的什么 Nginx?在 107.170.216.243 这台机器上面执行:
curl http://192.241.225.37
把结果发出来看看,我估计那边返回的就是 Nginx welcome 那个页面
@huacnlee nginx 已经重启了,配置没有问题。
在 107.170.216.243 这台机器访问curl http://192.241.225.37
返回如下:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"bam.nr-data.net","errorBeacon":"bam.nr-data.net","licenseKey":"f96debd018","applicationID":"8067875","transactionName":"elpYRRBeCFkAQx4UBlVWWVwHHg1bAVRJ","queueTime":0,"applicationTime":46,"ttGuid":"","agentToken":null,"agent":"js-agent.newrelic.com/nr-632.min.js"}</script>
<script type="text/javascript">window.NREUM||(NREUM={}),__nr_require=function(e,n,t){function r(t){if(!n[t]){var o=n[t]={exports:{}};e[t][0].call(o.exports,function(n){var o=e[t][1][n];return r(o?o:n)},o,o.exports)}return n[t].exports}if("function"==typeof __nr_require)return __nr_require;for(var o=0;o<t.length;o++)r(t[o]);return r}({QJf3ax:[function(e,n){function t(e){function n(n,t,a){e&&e(n,t,a),a||(a={});for(var u=c(n),f=u.length,s=i(a,o,r),p=0;f>p;p++)u[p].apply(s,t);return s}function a(e,n){f[e]=c(e).concat(n)}function c(e){return f[e]||[]}function u(){return t(n)}var f={};return{on:a,emit:n,create:u,listeners:c,_events:f}}function r(){return{}}var o="nr@context",i=e("gos");n.exports=t()},{gos:"7eSDFh"}],ee:[function(e,n){n.exports=e("QJf3ax")},{}],3:[function(e,n){function t(e){return function(){r(e,[(new Date).getTime()].concat(i(arguments)))}}var r=e("handle"),o=e(1),i=e(2);"undefined"==typeof window.newrelic&&(newrelic=window.NREUM);var a=["setPageViewName","addPageAction","setCustomAttribute","finished","addToTrace","inlineHit","noticeError"];o(a,function(e,n){window.NREUM[n]=t("api-"+n)}),n.exports=window.NREUM},{1:12,2:13,handle:"D5DuLP"}],"7eSDFh":[function(e,n){function t(e,n,t){if(r.call(e,n))return e[n];var o=t();if(Object.defineProperty&&Object.keys)try{return Object.defineProperty(e,n,{value:o,writable:!0,enumerable:!1}),o}catch(i){}return e[n]=o,o}var r=Object.prototype.hasOwnProperty;n.exports=t},{}],gos:[function(e,n){n.exports=e("7eSDFh")},{}],handle:[function(e,n){n.exports=e("D5DuLP")},{}],D5DuLP:[function(e,n){function t(e,n,t){return r.listeners(e).length?r.emit(e,n,t):(o[e]||(o[e]=[]),void o[e].push(n))}var r=e("ee").create(),o={};n.exports=t,t.ee=r,r.q=o},{ee:"QJf3ax"}],id:[function(e,n){n.exports=e("XL7HBI")},{}],XL7HBI:[function(e,n){function t(e){var n=typeof e;return!e||"object"!==n&&"function"!==n?-1:e===window?0:i(e,o,function(){return r++})}var r=1,o="nr@id",i=e("gos");n.exports=t},{gos:"7eSDFh"}],G9z0Bl:[function(e,n){function t(){var e=d.info=NREUM.info,n=f.getElementsByTagName("script")[0];if(e&&e.licenseKey&&e.applicationID&&n){c(p,function(n,t){n in e||(e[n]=t)});var t="https"===s.split(":")[0]||e.sslForHttp;d.proto=t?"https://":"http://",a("mark",["onload",i()]);var r=f.createElement("script");r.src=d.proto+e.agent,n.parentNode.insertBefore(r,n)}}function r(){"complete"===f.readyState&&o()}function o(){a("mark",["domContent",i()])}function i(){return(new Date).getTime()}var a=e("handle"),c=e(1),u=(e(2),window),f=u.document,s=(""+location).split("?")[0],p={beacon:"bam.nr-data.net",errorBeacon:"bam.nr-data.net",agent:"js-agent.newrelic.com/nr-632.min.js"},d=n.exports={offset:i(),origin:s,features:{}};f.addEventListener?(f.addEventListener("DOMContentLoaded",o,!1),u.addEventListener("load",t,!1)):(f.attachEvent("onreadystatechange",r),u.attachEvent("onload",t)),a("mark",["firstbyte",i()])},{1:12,2:3,handle:"D5DuLP"}],loader:[function(e,n){n.exports=e("G9z0Bl")},{}],12:[function(e,n){function t(e,n){var t=[],o="",i=0;for(o in e)r.call(e,o)&&(t[i]=n(o,e[o]),i+=1);return t}var r=Object.prototype.hasOwnProperty;n.exports=t},{}],13:[function(e,n){function t(e,n,t){n||(n=0),"undefined"==typeof t&&(t=e?e.length:0);for(var r=-1,o=t-n||0,i=Array(0>o?0:o);++r<o;)i[r]=e[n+r];return i}n.exports=t},{}]},{},["G9z0Bl"]);</script>
<title>SlotsServer</title>
<link data-turbolinks-track="true" href="/assets/application-de4b8a4baeea040c0602c2f3ae77ff57.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-79b779fc5fcc60ab524424af69a14fdf.js"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="dAr/Sxe0y55DZkH4AAA6FWxaqLpLgoPWXZy9Wfqqw8Q=" name="csrf-token" />
</head>
<body>
hello
</body>
</html>
是正常的结果。
@huacnlee 107.170.216.243 这条机器所有的配置如下:
/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
# include /etc/nginx/naxsi_core.rules;
##
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##
# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
# }
/etc/nginx/sites-enabled/slots-load-balancer
upstream backend {
server 192.241.225.37;
server 192.241.207.103;
}
server {
listen 80;
server_name 107.170.216.243;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_pass http://backend;
access_log /var/log/nginx/proxy.log;
#proxy_http_version 1.1;
#proxy_set_header Connection "";
}
}
另外/etc/nginx/sites-enabled/default 已经被我删除,不会产生影响
@huacnlee 这个是 nginx 目录的 tree
├── win-utf
├── uwsgi_params
├── sites-enabled
│ └── slots-load-balancer
├── sites-available
│ └── default
├── scgi_params
├── proxy_params
├── nginx.conf
├── naxsi-ui.conf.1.4.1
├── naxsi.rules
├── naxsi_core.rules
├── mime.types
├── koi-win
├── koi-utf
├── fastcgi_params
└── conf.d
配置文件只有 sites_enabled 下面的 slots-load-balancer。这个问题找了好久,还是没有解决。
@huacnlee 问题已经解决,是我自己犯了错误,load balancer 配置是 ok 的,但是两个 app server 把默认的配置没有删除,所以一直返回 nginx 的欢迎页面。非常感谢你。