假設有三臺服務器html
A 192.168.1.250 nginx服務器node
B 192.168.1.251 程序服務器linux
C 192.168.1.252 程序服務器nginx
3、配置nginxweb
vi nginx.conf
###############################################################################
user www www;
worker_processes 1;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 51200;
##############################################################################
events{
use epoll;
worker_connections 51200;
}
##############################################################################
http{
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;後端
##############################################################################
upstream mystream {
server 192.168.1.251;
server 192.168.1.252;
}
##############################################################################
server
{
listen 80;
server_name www.test.com *.test.com;服務器
location / {
proxy_pass http://mystream;app
proxy_set_header Host $host;負載均衡
proxy_set_header X-Real-IP $remote_addr;tcp
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.1.253;
deny all;
}
###############################################################################
log_format www_test_com '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/nginx/logs/nginx.log www_test_com;
}
}
################################################################################
4、測試配置是否正常
[root@linux-1 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
############################以上說明配置正確###################
5、啓動和中止nginx負載均衡器
啓動:
ulimit -SHn 51200
/usr/local/nginx/sbin/nginx
中止:
killall -9 nginx ####注意nginx能夠寫腳本形式讓它啓動中止
6、測試
web1-------->ip:192.168.1.251------------>index.html------------->the is web1
web2-------->ip:192.168.1.252------------>index.html------------->the is web2
測試結果:


7、簡單的使用監控模塊(沒有像LVS的ipvsadm強大,只是簡單的查看而已)
http://192.168.1.250/nginx_status

##########################到些簡單的負載配置完成#######################
8、簡單講解配置文件(講解有錯誤請指教):
worker_processes 1; ###通常配置跟CPU的數量同樣
error_log /usr/local/nginx/logs/nginx_error.log crit;###nginx錯誤日誌使用
use epoll;###epoll模式
worker_connections 51200; ###每一個進程最大鏈接數51200個
upstream 192.168.1.250 {
server 192.168.1.251;
server 192.168.1.252;
} ###############這個就是負載了,下面的就是後端的源服務器
location / {
proxy_pass http://192.168.1.250;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}##########後端源Web服務器經過X-Forwarded-For獲取用戶真實IP地址
location /nginx_status { stub_status on; access_log off; allow 192.168.1.253; deny all;}############這個就是簡單監控nginx的狀態