什么是nginx?
Nginx是一款高性能的http 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器。由俄羅斯的程序設計師Igor Sysoev所開發,官方測試nginx能夠支支撐5萬并發鏈接,并且cpu、內存等資源消耗卻非常低,運行非常穩定。開源、免費。
Nginx的應用場景
1.http服務器。Nginx是一個http服務可以獨立提供http服務。可以做網頁靜態服務器。
2.虛擬主機。可以實現在一臺服務器虛擬出多個網站。例如個人網站使用的虛擬主機。
3.反向代理,負載均衡。當網站的訪問量達到一定程度后,單臺服務器不能滿足用戶的請求時,需要用多臺服務器集群可以使用nginx做反向代理。并且多臺服務器可以平均分擔負載,不會因為某臺服務器負載高宕機而某臺服務器閑置的情況。
Nginx的安裝步驟
第一步:把nginx的壓縮包上傳到服務器
第二步:解壓。
第三步:創建一個makefile。
參數設置如下:
./configure
--prefix=/usr/local/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
注意:上邊將臨時文件目錄指定為/var/temp/nginx,需要在/var下創建temp及nginx目錄
第四步:編譯make
第五步:安裝make install
Nginx實現虛擬機
可以實現在同一臺服務運行多個網站,而且網站之間互相不干擾。
同一個服務器可能有一個ip,網站需要使用80端口。網站的域名不同。
區分不同的網站有三種方式:
<li>ip區分
<li>端口區分
<li>域名區分
Ip區分虛擬主機
需要一臺服務器綁定多個ip地址。
方法一:
使用標準的網絡配置工具(比如ifconfig和route命令)添加lP別名:
當前ip配置情況:
在eth0網卡再綁定一個ip:192.168.101.103
/sbin/ifconfig eth0:1 192.168.101.103 broadcast 192.168.101.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.101.103 dev eth0:1
方法二:
1、將/etc/sysconfig/network-scripts/ifcfg-eth0文件復制一份,命名為ifcfg-eth0:1
修改其中內容:
DEVICE=eth0:1
IPADDR=192.168.25.103
其他項不用修改
2、重啟系統
配置nginx基于ip地址的虛擬主機
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;
server { #一個Server就是一個虛擬主機
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
}
基于ip的虛擬主機配置
server {
listen 80;
server_name 192.168.25.141;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-141;
index index.html index.htm;
}
}
server {
listen 80;
server_name 192.168.25.100;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-100;
index index.html index.htm;
}
}
Nginx重新加載配置文件。
基于端口的虛擬主機
server {
listen 81;
server_name 192.168.25.141;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-81;
index index.html index.htm;
}
}
server {
listen 82;
server_name 192.168.25.141;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-82;
index index.html index.htm;
}
}
基于域名的虛擬主機
最有用的虛擬主機配置方式。
一個域名只能綁定一個ip地址,一個ip地址可以被多個域名綁定。
可以修改host文件實現域名訪問。
設置域名和ip的映射關系
修改window的hosts文件:(C:\Windows\System32\drivers\etc)
基于域名的虛擬主機配置
server {
listen 80;
server_name www.itheima.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-www;
index index.html index.htm;
}
}
server {
listen 80;
server_name hehe.itheima.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html-hehe;
index index.html index.htm;
}
}
修改配置文件后,需要nginx重新加載配置文件。
Nginx的反向代理
什么是反向代理?
正向代理
反向代理
使用nginx實現反向代理
Nginx只做請求的轉發,后臺有多個http服務器提供服務,nginx的功能就是把請求轉發給后面的服務器,決定把請求轉發給誰。
安裝tomcat
在一個虛擬機上創建兩個tomcat實例,模擬多個服務器
需求
通過訪問不同的域名訪問運行在不同端口的tomcat
8080.hb.com 訪問運行8080端口的tomcat
8081.hb.com 訪問運行8081端口的tomcat
域名需要配置host文件:
Nginx的配置
upstream tomcatserver1 {
server 192.168.25.141:8080;
}
upstream tomcatserver2 {
server 192.168.25.141:8081;
}
server {
listen 80;
server_name 8080.itheima.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver1;
index index.html index.htm;
}
}
server {
listen 80;
server_name 8081.itheima.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver2;
index index.html index.htm;
}
}
如果在同一個域名下有多臺服務器提供服務,此時需要nginx負載均衡。
負載均衡
什么是負載均衡
負載均衡 建立在現有網絡結構之上,它提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增加吞吐量、加強網絡數據處理能力、提高網絡的靈活性和可用性。
負載均衡,英文名稱為Load Balance,其意思就是分攤到多個操作單元上進行執行,例如Web服務器、FTP服務器、企業關鍵應用服務器和其它關鍵任務服務器等,從而共同完成工作任務.
需求
nginx作為負載均衡服務器,用戶請求先到達nginx,再由nginx根據負載配置將請求轉發至 tomcat服務器。
nginx負載均衡服務器:192.168.25.141
tomcat1服務器:192.168.25.141:8080
tomcat2服務器:192.168.25.141:8081
配置nginx的負載均衡
配置負載均衡的權重
節點說明:
在http節點里添加:
#定義負載均衡設備的 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節點下添加
proxy_pass http://myServer;
upstream 每個設備的狀態:
down 表示單前的server暫時不參與負載
weight 默認為1.weight越大,負載的權重就越大。
max_fails :允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
fail_timeout:max_fails 次失敗后,暫停的時間。
backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這臺機器壓力會最輕。
Nginx的高可用
解決高可用的方案就是添加冗余