nginx1.10.2叢集安裝
經典
http://zyan.cc/nginx_php_v6/
下載
wget http://nginx.org/download/nginx-1.10.2.tar.gz
準備
yum install -y pcre-devel
yum install gcc gcc-c++ ncurses-devel perl
yum -y install make gcc gcc-c++ncurses-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl--devel (可選)
移動
mv nginx-1.10.2.tar.gz ~
解壓
tar zxf nginx-1.10.2.tar.gz
配置
cd ~/nginx-1.10.2/
./configure --user=root --group=root --prefix=/usr/local/nginx-1.10.2
./configure --user=root --group=root--prefix=/usr/local/nginx-1.10.2 \--with-http_stub_status_module--with-http_ssl_module
./configure --user=root --group=root--prefix=/usr/local/nginx-1.10.2
--conf-path=/usr/local/nginx-1.10.2/nginx.conf\
--openssl-path=/usr/local/openssl-1.0.1c \
--with-http_ssl_module \
--with-pcre=/usr/local/pcre-8.21 \
--with-http-stub-status-module
編譯安裝
make && make install
測試
cd /usr/local/nginx-1.10.2/nginx/sbin/
./nginx -t
啟動
./nginx
/nginx -s reload
停止
./nginx -s stop
驗證
netstat -ntlpu
負載均衡配置(基於權重)
修改conf/nginx.conf
http {
upstream cancer {
server cancer01:8080 weight=5;
server cancer02:8080 weight=5;
}
location/ {
proxy_pass http://cancer;
}
}
負載均衡,完整如下:
user www www;
worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events{
use epoll;
worker_connections102400;
}
http{
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 464k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;
###2012-12-19 change nginx logs
log_format main '$http_x_forwarded_for -$remote_user [$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" $request_time $remote_addr';
upstream web_app {
server127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
####chinaapp.sinaapp.com
server {
listen 80;
server_name chinaapp.sinaapp.com;
index index.jsp index.html index.htm;
#釋出目錄/data/www
root /data/www;
location / {
proxy_next_upstream http_502 http_504error timeout invalid_header;
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_pass http://web_app;
expires 3d;
}
}
}
負載均衡(種類)
nginx 的 upstream目前支援 4 種方式的分配
1)、輪詢(預設),每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
2)、weight,指定輪詢機率,weight和訪問比率成正比,用於後端伺服器效能不均的情況。
2)、ip_hash,每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題。
3)、fair(第三方),按後端伺服器的響應時間來分配請求,響應時間短的優先分配。
4)、url_hash(第三方)
#定義負載均衡裝置的 Ip及裝置狀態
upstream myServer {
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
server{
}
down 表示單前的server暫時不參與負載
weight 預設為1.weight越大,負載的權重就越大。
max_fails :允許請求失敗的次數預設為1.當超過最大次數時,返回proxy_next_upstream 模組定義的錯誤
fail_timeout:max_fails 次失敗後,暫停的時間。
backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
Nginx還支援多組的負載均衡,可以配置多個upstream 來服務於不同的Server.
會話共享
基於會話共享配置nginx+tomcat+redis
準備,jar包
apache tomcat 7.0.55
nginx 1.7.2
redis 2.8.9
tomcat-redis-session-manager-1.2-tomcat-7.jar
jedis-2.2.0.jar
commons-pool-1.6.jar
tcl8.5.11-src.tar.gz
修改tomct,/conf/context.xml
<ValveclassName="com.radiadesign.catalina.session.RedisSessionHandlerValve"/>
<ManagerclassName="com.radiadesign.catalina.session.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60"
/>
安裝redis
配置nginx
upstream backend{
server192.168.1.246:8080;
server192.168.1.246:9080;
}
server{
proxy_redirectoff;
proxy_set_headerHost $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://backend;
}
測試頁面:
<%@ page language="java"contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>sessionid=<%=session.getId()%>
<br>tomcat(1、2、3)
</body>
</html>
反向代理
例項:
upstreamtornado {
server 127.0.0.1:8888;
}
server {
listen 80;
root /root/nmapp2_venv;
index index.py index.html;
server_name server;
location / {
#if (!-e $request_filename) {
# rewrite ^/(.*)$ /index.py/$1 last;
#}
}
location ~ /index\.py {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP$remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://tornado;
}
}
Nginx 反向代理的指令不需要新增額外的模組,預設自帶 proxy_pass 指令,只需要修改配置檔案就可以實現反向代理。
再舉一個例子吧。比如要配置後端跑 apache 服務的 ip 和埠,也就是說,我們的目標是實現通過 http://ip:port 能訪問到你的網站。
只要新建一個 vhost.conf,加入如下內容(記得修改 ip 和域名為你的 ip 和域名)。修改nginx.conf,新增 include quancha.conf 到http{}段, reload nginx就可以了。
反向代理模板:
## Basic reverse proxy server ##
upstream apachephp {
server ip:8080; #Apache
}
## Start www.nowamagic.net ##
server {
listen 80;
server_name www.nowamagic.net;
access_log logs/quancha.access.log main;
error_log logs/quancha.error.log;
root html;
index index.html index.htmindex.php;
## send request back to apache ##
location / {
proxy_pass http://apachephp;
#Proxy Settings
proxy_redirect off;
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_next_upstream error timeout invalid_header http_500 http_502http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}