文章目錄
1.Nginx的介紹
Nginx (engine x) 是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務。
Nginx的三個主要功能:
<1>Nginx作爲靜態資源服務器:可以處理靜態文件,索引文件以及自動索引;打開文件描述符緩衝。(缺點:不能處理動態頁面)
<2>Nginx作爲反向代理服務器:可以緩存和負載均衡服務。部署在不同的服務器上,但是通過統一的域名進入,nginx則對請求進行分發,減輕了服務器的壓力。
<3>Nginx作爲應用程序接口(API)
Nginx的優點:
Nginx具有高併發、高性能,可擴展性好,內存消耗少,配置文件簡單,成本低(低於F5等硬件負載均衡),支持重寫功能(http->https),具有健康檢測功能,節省帶寬,支持gzip壓縮,模塊化設計的特點。
2.Nginx的安裝
環境:測試機中的firewalld、selinux、NetworkManager已關閉
tar zxf nginx-1.18.0.tar.gz
解壓包
使配置文件字體有顏色:
mkdir ~/.vim
cp -r /soft/nginx-1.18.0/contrib/vim/* ~/.vim
解決依賴性
yum install -y gcc
yum install -y pcre-devel
yum install -y openssl-devel
編譯安裝
./configure --prefix=/usr/local/nginx --with-http_ssl_module
預編譯
make
編譯
make install
安裝
運行Nginx
cd /usr/local/nginx/sbin/
./nginx
3.Nginx版本的熱部署
Ngnix中的進程分爲兩類,一類是master進程,一類是worker進程。
其中master進程使用來管理監控控制其下邊的worker進程的主進程,這個進程由root發起。。
worker進程是master進程的子進程,是真正處理請求的進程。
當通知ngnix重讀配置文件的時候,master進程會進行語法錯誤的判斷。如果存在語法錯誤的話,返回錯誤,不進行裝載;如果配置文件沒有語法錯誤,那麼ngnix也不會將新的配置調整到所有worker中。而是,先不改變已經建立連接的worker,等待worker將所有請求結束之後,將原先在舊的配置下啓動的worker殺死,然後使用新的配置創建新的worker。所以nginx 的熱部署 ,是在不打斷用戶請求的情況下更新版本。
3.1 Nginx版本的查看
cd /usr/local/nginx/sbin/
./nginx -V
3.2 Nginx的版本的更新
實驗環境:將nginx-1.18.0更新爲nginx-1.17.10
1.備份舊版本(以訪更新失敗)
cd /usr/local/nginx/sbin/
cp nginx nginx.old
2.對新版本重新編譯
./configure --prefix=/usr/local/nginx --with-http_ssl_module
對新版預編譯時功能不變,參數不變
make
編譯
注意:此時不需要再make installl,否則會覆蓋原來的二進制程序,只有在第一次按裝使需要
3.把新編好的二進制程序放安裝目錄覆蓋原來的文件
cp -f objs/nginx /usr/local/nginx/sbin/nginx
如果程序正常運行強制複製
4.將原先在舊的配置下啓動的worker殺死,然後使用新的配置創建新的worker
ps -ef |grep nginx
查看nginx進程
kill -USR2 20986
舊版本停止接收請求,並開啓新的master產生新的worker
kill -WINCH 20986
等待舊版本處理完關閉worker進程
3.3 Nginx版本的回退
1.舊版本備份的二進制程序覆蓋新版本
cp -f nginx.old nginx
2.關閉新版本的進程,開啓舊版本進程
kill -HUP 20986
啓動舊版本的worker進程
kill -USR2 21225
新版本不再接收請求
kill -WINCH 21225
關閉新版本的worker進程
kill -9 21225
關閉新版本的masterr進程
4.Nginx的日誌切割
access.log: 記錄哪些用戶,哪些頁面以及用戶瀏覽器,IP等訪問信息;
error.log: 記錄服務器錯誤的日誌
工作中 Nginx 日誌基本爲 access_log,配置啓動後就開始產生日誌文件,日積月累的,這個日誌文件會越來越大。針對這種情況,採用日誌切割防止日誌過大。
用戶端增加訪問測試:
ab -c 1 -n 100000 http://192.168.43.10/index.html
一個併發十萬次請求
日誌切割
1.對日誌備份
mv access.log `date +%F -d -1day`_access.log ###備份前一天日誌
2.重新記錄新日誌
../sbin/nginx -s reopen ##重新打開一個access.log記錄新日誌
5.Nginx的gzip壓縮
壓縮前
設置gzip壓縮
gzip on;
gzip_min_length 1;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript test/css application/xml text/javascript application/x-httpd-php image/gif image/png;
壓縮後
6.設置nginx的systemctl啓動方式
vim /usr/lib/systemd/system/nginx.service
設置nginx的systemctl啓動方式
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
重新加載systemctl命令
7.Nginx的配置參數
7.1 指定開啓用戶
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; ##指定開啓用戶
useradd nginx
建立nginx用戶
systemctl restart nginx.service
重啓nginx服務
ps -ef | grep nginx
查看nginx運行用戶
7.2 指定worker進程數
vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;
systemctl restart nginx.service
ps -ef | grep nginx
7.3 指定最大連接數
vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 65535;
}
7.4 限制併發連接數/速率/帶寬
vim /usr/local/nginx/conf/nginx.conf
mkdir /usr/local/nginx/html/download/
建立目錄並放置訪問內容
systemctl restart nginx.service
user nginx nginx;
worker_processes 2;
events {
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /download/ {
limit_conn addr 1;
limit_req zone=one burst=5;
limit_rate 50k;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
測試:
1.限制併發數
2.限制傳輸速率
3.限制帶寬
7.5 Nginx日誌的設置
vim /usr/local/nginx/conf/nginx.conf
systemctl restart nginx.service
測試:
[root@rhel7 ~]# curl 192.168.43.10
8.Nginx獲取真正的用戶ip
1.編譯real_ip模塊
cd /soft/nginx-1.18.0/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module
重新預編譯
make
編譯
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/nginx.old
備份二進制程序
cp -f nginx /usr/local/nginx/sbin/nginx
新的二進制程序覆蓋舊的
2.配置nginx模塊
vim /usr/local/nginx/conf/nginx.conf
http {
.......
server {
listen 80;
server_name servera.rhel7.com;
set_real_ip_from 192.168.43.10;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
location / {
return 200 "client real ip: $remote_addr\n";
}
}
}
systemctl restart nginx.service
測試
9.Nginx的圖片壓縮
1.編譯圖片過濾模塊
yum install -y gd-devel-2.0.35-26.el7.x86_64.rpm
解決模塊依賴性
cd /soft/nginx-1.18.0/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module=dynamic
重新預編譯
make
編譯
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/nginx.old
備份二進制程序
cp -f nginx /usr/local/nginx/sbin/nginx
新的二進制程序覆蓋舊的
2.建立目錄存放模塊
mkdir /usr/local/nginx/modules
建立目錄
cp /soft/nginx-1.18.0/objs/ngx_http_image_filter_module.so /usr/local/nginx/modules
3.編輯nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
systemctl restart nginx.service
load_module modules/ngx_http_image_filter_module.so;
user nginx nginx;
worker_processes 2;
events {
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /download/ {
limit_conn addr 1;
limit_req zone=one burst=5;
#limit_rate 50k;
image_filter resize 150 100;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
測試
10.Nginx實現https訪問
1.支持ssl模塊
2.生成證書
cd /etc/pki/tls/certs/
make cert.pem
生成證書
3.配置文件
# HTTPS server
#
server {
listen 443 ssl;
server_name servera.rhel7.com;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /web;
index index.html index.htm;
}
}
4.建立目錄及發佈內容
mkdir /web
vim /web/index.html
systemctl resatr nginx
測試
11.Nginx的重新功能
實現訪問自動跳轉到https
vim /usr/local/nginx/conf/nginx.conf
http {
...............
# HTTPS server
#
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key cert.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /web;
index index.html index.htm;
}
}
server {
listen 80;
server_name servera.rhel7.com;
#rewrite ^/(.*)$ https://servera.rhel7.com/$1;
#rewrite ^/(.*)$ https://servera.rhel7.com/$1 permanent;
rewrite ^/bbs$ https://bbs.rhel7.com/$1 permanent;
}
server {
listen 80;
server_name bbs.rhel7.com;
location / {
root /bbs;
index index.html;
}
}
}
測試
1.臨時重寫
2.永久重寫
3.訪問以bbs結尾時重定向
11.Nginx防止盜鏈
實驗環境:配置兩臺Nginx服務器(servera/serverd)
11.1 盜鏈的實現
servera:提供訪問資源
serverd:進行盜鏈操作
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name daolian.rhel7.com;
charset utf-8;
location / {
root /web;
index index.html;
}
}
}
mkdir /web
vim /web/index.html
<html>
<body>
<br>盜鏈圖片</br>
<img src="http://servera.rhel7.com/test.jpeg">
</body>
</html>
/usr/local/nginx/sbin/nginx -s reload
盜鏈測試
圖片資源在servera,但是訪問serverd可以訪問到
11.2 防止盜鏈
在server中設置
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name servera.rhel7.com;
location / {
root /web;
index index.html;
}
location ~*\.(gif|jpg|png|jpeg)$ {
root /web;
valid_referers none blocked servera.rhel7.com;
if ($invalid_referer) {
#return 403;
rewrite ^/ http://bbs.rhel7.com/daolian.jpg;
}
}
}
測試
12.Nginx圖形化監控工具
解決依賴性
yum install -y GeoIP-1.5.0-14.el7.x86_64.rpm GeoIP-devel-1.5.0-14.el7.x86_64.rpm geoipupdate-2.5.0-1.el7.x86_64.rpm
yum install ncurses-devel -y
解壓goaccess並安裝
tar zxf goaccess-1.3.tar.gz
cd goaccess-1.3/
./configure --prefix=/usr/local/goaccess --enable-utf8 --enable-geoip=legacy --with-openssl
make && make install
做軟連接
ln -s /usr/local/goaccess/bin/goaccess /usr/local/bin
配置nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
location / {
alias /usr/local/nginx/html/report.html;
}
啓動goaccess
goaccess access.log -o /usr/local/nginx/html/report.html --real-time-html --time-format='%H:%M:%S' --date-format='%d/%b/%Y' --log-format=COMBINED
可打入後臺
訪問測試: