一、安裝nginx
1、下載nginx
http://nginx.org/download/nginx-1.8.1.tar.gz
上傳到Linux服務器,我這里是上傳到/usr/tool/目錄下
進入tool文件夾下,解壓上傳的nginx壓縮包
[root@localhost /]# cd /usr/tool/
[root@localhost tool]# tar -zxvf nginx-1.8.1.tar.gz
2、準備環境
需要安裝gcc環境和相關第三方開發包,執行如下命令
[root@localhost tool]#yum -y install gcc-c++
[root@localhost tool]#yum install -y pcre pcre-devel
[root@localhost tool]#yum install -y zlib zlib-devel
[root@localhost tool]#yum install -y openssl openssl-devel
命令解釋
yum -y install gcc-c++
需要安裝gcc的環境。
yum install -y pcre pcre-devel
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。
yum install -y zlib zlib-devel
zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
yum install -y openssl openssl-devel
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,并提供豐富的應用程序供測試或其它目的使用。
nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
3、創建目錄
需要在/var下創建temp及nginx目錄
[root@localhost tool]#mkdir /var/temp/nginx/client -p
4、Nginx編譯
[root@localhost /]# cd /usr/tool/nginx-1.8.1/
[root@localhost nginx-1.8.1]# ./configure
--prefix=/usr/tool/nginx
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--with-http_gzip_static_module
--http-client-body-temp-path=/var/temp/nginx/client
--http-proxy-temp-path=/var/temp/nginx/proxy
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi
--http-scgi-temp-path=/var/temp/nginx/scgi
5、安裝Nginx:
安裝命令:make & make install
[root@localhost nginx-1.8.1]# make & make install
進入nginx目錄,查看目錄下有sbin文件夾,安裝成功
[root@localhost nginx-1.8.1]# cd /usr/tool/nginx
[root@localhost nginx]# ll
6、啟動nginx
進入sbin目錄
[root@localhost sbin]# ./nginx
7、訪問nginx
輸入安裝nginx的IP,默認端口為80
http://192.168.65.129/
image.png
如看到上圖信息,nginx安裝與啟動成功
二、配置虛擬主機
nginx的配置文件在
/usr/tool/nginx-1.8.1/conf/nginx.conf
1、通過端口區分
1.1、配置文件修改如下
每個server監聽1個端口
修改完成需要每次配置文件修改之后都需要重新加載配置文件,命令如下
[root@localhost nginx]# sbin/nginx -s reload
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html81;
index index.html index.htm;
}
}
}
1.2、復制文件夾用于81端口使用
[root@localhost nginx]# cp html html81 -r
修改html81目錄下index.html內容來區分端口
2、通過域名區分
2.1、修改nginx的配置文件如下
修改完刷新文件
[root@localhost nginx]# sbin/nginx -s reload
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html81;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.test01.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root htmltest01;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.test02.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root htmltest02;
index index.html index.htm;
}
}
}
2.2、復制文件夾
復制文件夾后并修改里面的index.html文件夾做區分
[root@localhost nginx]# cp html htmltest01 -r
[root@localhost nginx]# cp html htmltest02 -r
2.3、修改本機hosts
windows的hosts的文件在
C:\Windows\System32\drivers\etc
增加內容如下,ip換成安裝nginx機器的地址
192.168.65.129 www.test01.com
192.168.65.129 www.test02.com
2.4、瀏覽器訪問
瀏覽器輸入
http://www.test01.com/
image.png
瀏覽器輸入
http://www.test02.com/
image.png
至此,通過端口和域名區分虛擬主機設置完成
三、nginx配置反向代理
1、安裝2個Tomcat
我的Linux已經有1個Tomcat,復制2兩份
分別修改端口為8080和80801
分別修改Tomcat下webapp目錄下ROOT目錄下的index.jsp以做區別
然后分別啟動2個Tomcat
[root@localhost tomcat]# cp tomcat8 tomcat8-8080 -r
[root@localhost tomcat]# cp tomcat8 tomcat8-8081 -r
2、nginx配置文件如下
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcat8080 {
server 192.168.65.129:8080;
}
server {
listen 80;
server_name www.test01.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat8080;
index index.html index.htm;
}
}
upstream tomcat8081 {
server 192.168.65.129:8081;
}
server {
listen 80;
server_name www.test02.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat8081;
index index.html index.htm;
}
}
}
3、瀏覽器訪問
image.png
image.png
反向代理配置成功
四、nginx配置負載均衡
nginx默認的負載均衡的策略就是輪詢的方式。
配置文件如下
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream tomcat8080 {
server 192.168.65.129:8080;
server 192.168.65.129:8081;
}
server {
listen 80;
server_name www.test01.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat8080;
index index.html index.htm;
}
}
}
image.png
image.png
至此,nginx的負載均衡配置完成
負載均衡的幾種常用方式
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
upstream tomcat8080 {
server 192.168.65.129:8080;
server 192.168.65.129:8081;
}
2、weight
指定輪詢幾率,weight和訪問比率成正比,用于后端服務器性能不均的 情況。
upstream tomcat8080 {
server 192.168.65.129:8080 weight=3;
server 192.168.65.129:8081 weight=7;
}
權重越高,在被訪問的概率越大,如上例,分別是30%,70%。
3、ip_hash指令
上述方式存在一個問題就是說,在負載均衡系統中,假如用戶在某臺服務器上登錄了,那么該用戶第二次請求的時候,因為我們是負載均衡系統,每次請求都會重新定位到服務器集群中的某一個,那么已經登錄某一個服務器的用戶再重新定位到另一個服務器,其登錄信息將會丟失,這樣顯然是不妥的。
我們可以采用ip_hash指令解決這個問題,如果客戶已經訪問了某個服務器,當用戶再次訪問時,會將該請求通過哈希算法,自動定位到該服務器。
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
upstream tomcat8080 {
ip_hash;
server 192.168.65.129:8080;
server 192.168.65.129:8081;
}
4、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream tomcat8080 {
server 192.168.65.129:8080;
server 192.168.65.129:8081;;
fair;
}
5、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
upstream tomcat8080 {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
每個設備的狀態設置為:
1.down 表示單前的server暫時不參與負載
2.weight 默認為1.weight越大,負載的權重就越大。
3.max_fails:允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤
4.fail_timeout:max_fails次失敗后,暫停的時間。
5.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
配置實例:
#user nobody;
worker_processes 4;
events {
# 最大并發數
worker_connections 1024;
}
http{
# 待選服務器列表
upstream tomcat8080 {
# ip_hash指令,將同一用戶引入同一服務器。
ip_hash;
server 192.168.65.129:8080 fail_timeout=60s;
server 192.168.65.129:8081;
}
server{
# 監聽端口
listen 80;
# 根目錄下
location / {
# 選擇哪個服務器列表
proxy_pass http://tomcat8080 ;
}
}
}