1.2 WEB服務器
nginx 開源的,支持高性能,高並發的
apache nginx他父親
IIS(windows下面的WEB Server)
1.3 查看WEB服務器信息
使用curl -I 命令查看taobao和JD的WEB服務器
1.4 nginx的優點:
1.4.1 占有內存少,並發能力強
1.4.2 處理靜態文件
靜態文件與動態文件的區別
靜態文件: css js jpg png mp4
動態數據: 網站會請求后端的數據庫接口,獲取最新的數據,這些數據就是動態數據
1.4.3 百度、京東、新浪、網易、騰訊、淘寶都在用nginx
1.4.4 一台機器只有一個80端口,假如我們想要跑多個WEB服務器呢?
2 安裝nginx步驟
2.0 首先卸載掉之前安裝的nginx
yum remove nginx
2.1 安裝nginx需要的依賴庫
yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
2.2 下載安裝nginx源碼包
cd /opt
wget -c https://nginx.org/download/nginx-1.16.0.tar.gz
2.3.解壓縮源碼(還是在/opt目錄下)
tar -zxvf nginx-1.16.0.tar.gz
2.4.釋放編譯文件 開啟nginx狀態監測功能
cd /opt/nginx-1.16.0
./configure --prefix=/opt/nginx116 --with-http_ssl_module --with-http_stub_status_module
2.5 編譯安裝
make && make install
2.6.啟動nginx,進入sbin目錄,找到nginx啟動命令
cd /opt/nginx116/sbin
./nginx #啟動
./nginx -s stop #關閉
./nginx -s reload # 平滑重啟 ,修改了nginx.conf之后,可以不重啟服務,加載新的配置
2.7 查看nginx運行狀態
查看端口是否運行: netstat -tunlp
查看進程是否運行: ps -ef | grep nginx
2.8 nginx目錄下的文件
conf 存放nginx所有配置文件的目錄,主要nginx.conf
html 存放nginx默認站點的目錄,如index.html、error.html等
logs 存放nginx默認日志的目錄,如error.log access.log
sbin 存放nginx主命令的目錄,sbin/nginx
2.9 nginx.conf配置文件解析
注意: 在修改配置文件之前,最好是把配置文件備份一份!!!!!
注意: 在修改配置文件之前,最好是把配置文件備份一份!!!!!
注意: 在修改配置文件之前,最好是把配置文件備份一份!!!!!
#定義nginx工作進程數
worker_processes 5;
#錯誤日志
#error_log logs/error.log;
#http定義代碼主區域
http {
include mime.types;
default_type application/octet-stream;
#定義nginx的訪問日志功能
#nginx會有一個accses.log功能,查看用戶訪問的記錄
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;
keepalive_timeout 65;
#開啟gzip壓縮傳輸
gzip on;
#虛擬主機1 定義一個 斗魚網站
server {
#定義nginx的訪問入口端口,訪問地址是 192.168.11.37:80
listen 80;
#定義網站的域名www.woshidouyu.tv
#如果沒有域名,就填寫服務器的ip地址 192.168.11.37
server_name www.woshidouyu.tv;
#nginx的url域名匹配
#只要請求來自於www.woshidouyu.tv/111111111
#只要請求來自於www.woshidouyu.tv/qweqwewqe
#最低級的匹配,只要來自於www.woshidouyu.tv這個域名,都會走到這個location
location / {
#這個root參數,也是關鍵字,定義網頁的根目錄
#以nginx安裝的目錄為相對路徑 /opt/nginx112/html
#可以自由修改這個root定義的網頁根目錄
root html;
#index參數定義網站的首頁文件名,默認的文件名
index index.html index.htm;
}
#錯誤頁面的優化(只要是遇到前面4系列的錯誤,就會直接跳轉到相對目錄下的40x.html頁面)
error_page 400 401 402 403 404 /40x.html;
}
}
2.10 nginx啟動出現錯誤的情況
如果你在平滑重啟nginx時出現以下情況的話,說明你的nginx沒有啟動
解決方案:
# 只需要將nginx啟動起來即可
/opt/nginx196/sbin/nginx
3 nginx錯誤頁面
3.1 配置nginx錯誤頁面
vim /opt/nginx112/conf/nginx.conf
修改server代碼塊
server {
listen 80;
server_name www.qishihuya.com;
location / {
root /opt/www/qishihuya;
index index.html index.htm;
}
#error_page 500 502 503 504 /50x.html;
error_page 400 402 403 404 /40x.html;
location = /40x.html {
root html;
}
}
平滑重啟nginx
/opt/nginx112/sbin/nginx -s reload
4 nginx錯誤頁面優化
打開百度或其他網站,隨便輸入一個不存在的鏈接,就可以訪問到錯誤頁面
右鍵點擊頁面空白處,查看網頁源代碼
拷貝源代碼,粘貼到/opt/nginx112/html/40x.html下面去
再訪問我們的網站,隨便輸入一個不存在的鏈接, 就可以訪問到這個錯誤頁面
http://192.168.1.40/asldfjasd
5 nginx訪問日志功能
6 nginx限制網站來源IP訪問
如果想要在本地訪問一個域名, 可以更改本機hosts文件
windows下
c:\\windows\system32\drivers\etc\hosts
linux下
/etc/hosts
nginx狀態檢測功能
1 在虎牙或者其他server代碼塊添加以下配置
location /status {
# 開啟nginx狀態檢測功能
stub_status on;
}
2 平滑重啟nginx
/opt/nginx196/sbin/nginx -s reload
7 配置nginx多虛擬主機
7.1 效果:
(1) 訪問三個不同的域名,顯示三個不同的網站
(2) 三個網站互不影響
7.2 修改配置文件
worker_processes 5;
#error_log logs/error.log;
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;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.qishi5douyu.com;
location / {
root /opt/web_server/douyu;
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.qishi5huya.com;
location /{
root /opt/web_server/huya;
index index.html;
}
}
server {
listen 80;
server_name www.qishi5zhanqi.com;
location /{
root /opt/web_server/zhanqi;
index index.html;
}
}
}
7.3 在服務器上創建三個目錄
cd /opt
rm -rf web_server
mkdir -p web_server/huya
mkdir -p web_server/douyu
mkdir -p web_server/zhanqi
touch web_server/huya/index.html
touch web_server/huya/index.html
touch web_server/huya/index.html
分別打開這三個index.html文件
vim index.html
分別添加三個直播網站的源代碼內容
然后保存退出
7.4 修改本機hosts文件
c:\\windows\system32\drivers\etc\hosts
添加以下三行解析記錄
10.0.3.156 www.qishi8.com
10.0.3.156 www.qishi8huya.com
10.0.3.156 www.qishi8zhanqi.com
7.5 平滑重啟nginx
/opt/nginx196/sbin/nginx -s reload
8 Nginx代理功能
8.1 生活中的代理有哪些
租客租房 ————> 中介(Q房網,鏈家, 家家順,中原地產, 泊寓, 自如, 蛋殼, 貝殼) ————> 房東
買家 ————> 代購(微商, 服務器代理商, 等等) ————> 店鋪(香港的店鋪, 供應商, 老寶)
NAT轉換 (Network Address Translation,網絡地址轉換)
局域網IP(只能在局域網進行通信的IP地址)
A類:10.0.0.0-10.255.255.255
B類:172.16.0.0-172.31.255.255
C類:192.168.0.0-192.168.255.255
8.2 實現一個反向代理
8.2.1 實驗效果:
在windows中訪問代理服務器,然后讓代理服務器去拿web服務器的數據
請求數據: windows ——> 10.0.3.156(假如公網IPwww.qishi8huya.com) ——> 10.0.3.2(由它返回數據) 返回數據: windows <—— 10.0.3.156 <—— 10.0.3.2
機器准備,兩台服務器
反向代理服務器 10.0.3.156
淘寶服務器 10.0.3.2
8.2.2 修改代理服務器10.0.3.156的配置文件
vim /opt/nginx196/conf/nginx.conf 在location代碼塊下添加一行數據 proxy_pass http://10.0.3.2:8060;
8.2.3 重啟代理服務器的nginx
/opt/nginx196/sbin/nginx -s reload
8.2.4 訪問代理服務器的IP: 192.168.12.139
訪問的結果是192.168.12.200返回的數據即可
8.2.5 分別在代理服務器和斗魚服務器上查看日志
在代理服務器上查看
在斗魚服務器上查看
9 Nginx負載均衡
9.1 負載均衡原理
9.2 配置負載均衡服務器
9.2.1 實驗背景
有三台機器 一台為nginx代理服務器(負載均衡調度器), 另外兩台為WEB服務器
10.0.3.156 # 負載均衡調度器
10.0.3.2 # WEB服務器1
10.0.3.212 # WEB服務器2
# WEB服務器3
9.2.2 完成效果
用戶訪問10.0.3.156,由nginx代理服務器通過負載均衡調度器分別分配到兩個WEB服務器,實現負載均衡
9.2.3 准備三台機器
10.0.3.156 # 負載均衡調度器
10.0.3.2 # WEB服務器1
10.0.3.212 # WEB服務器2
9.2.4 兩個WEB服務器可以正常訪問
訪問web服務器1返回“代噶好,我系帥帥劉 我真的是1.40這台機器” 訪問web服務器2返回“MMP 我TM是文龍”
9.2.5 在nginx代理服務器(負載均衡調度器)上面做如下配置:
# 在http代碼塊里面,添加配置
upstream qishi8_pool {
server 10.0.3.2:8060;
server 10.0.3.212;
}
在location代碼塊內,添加
location / {
proxy_pass http://qishi8_pool;
root html;
index index.html index.htm;
}
9.2.6 分別平滑重啟三台機器的nginx服務
/opt/nginx196/sbin/nginx -s reload
9.2.7 訪問10.0.3.156就可以看到,WEB1和WEB2交替返回數據
9.2.8 配置權重
在upstream里面配置weight的數值,可以調整服務器的請求權重
可以通過以下幾種方式進行權重的配置
調度算法 概述
輪詢 按時間順序逐一分配到不同的后端服務器(默認)
weight 加權輪詢,weight值越大,分配到的訪問幾率越高
ip_hash 每個請求按訪問IP的hash結果分配,這樣來自同一IP的固定訪問一個后端服務器
url_hash 按照訪問URL的hash結果來分配請求,是每個URL定向到同一個后端服務器
least_conn 最少鏈接數,那個機器鏈接數少就分發