Nginx 的功能實現——基於Rhel7.3系列

主機環境:rhel7.3 selinux and firewalld disabled

主機名 ip 服務
server1 172.25.26.1 nginx
server2 172.25.26.2 nginx

一、7系列的Nginx安裝

1.獲得資源,解壓(基礎配置)
在這裏插入圖片描述

[root@server1 ~]# tar zxf nginx-1.15.9.tar.gz 
[root@server1 ~]# ls

在這裏插入圖片描述

[root@server1 cc]# pwd
/root/nginx-1.15.9/auto/cc
[root@server1 cc]# vim gcc	#註釋日誌級別
[root@server1 core]# pwd
/root/nginx-1.15.9/src/core
[root@server1 core]# vim nginx.h	#屏蔽版本
14 #define NGINX_VER          "nginx/"
[root@server1 nginx-1.15.9]# yum install gcc pcre-devel openssl-devel -y 	#安裝編譯準備和解決依賴性

2.進行編譯,安裝

[root@server1 nginx-1.15.9]# ./configure --prefix=/usr/local/nginx  --with-http_realip_module --with-http_image_filter_module=dynamic --with-http_ssl_module

注意:會出現./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.報錯

[root@server1 ~]# yum list gd #查看版本(從而找到資源)
在這裏插入圖片描述

[root@server1 ~]# yum install gd-devel-2.0.35-26.el7.x86_64.rpm -y
重新進行編譯
[root@server1 nginx-1.15.9]# make && make install
[root@server1 nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

3.對配置文件進行基礎設置

root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
 2 user  nginx nginx;
 3 worker_processes  auto;
[root@server1 conf]# id nginx
id: nginx: no such user
[root@server1 conf]# useradd nginx
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx

二、如何給配置文件加高亮顯示

[root@server1 conf]# cd		#必須切換到主目錄下
[root@server1 ~]# mkdir .vim
[root@server1 contrib]# pwd
/root/nginx-1.15.9/contrib
[root@server1 contrib]# cp -r vim/* ~/.vim
[root@server1 contrib]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf

在這裏插入圖片描述

  • 如何查看nginx的版本和編輯加載過的模塊
[root@server1 ~]# nginx -V		#查看版本和加載過的模塊
nginx version: nginx/	#此處因爲安裝前屏蔽了版本
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_realip_module --with-http_image_filter_module=dynamic --with-http_ssl_module
[root@server1 ~]# nginx -v	#查看版本
nginx version: nginx/	#此處因爲安裝前屏蔽的版本
  • nginx的配置文件參數複習
#Nginx的worker進程運行用戶以及用戶組
user  nobody nobody;

#Nginx開啓的進程數,建議爲CPU的核數
worker_processes  1;
#worker_processes auto;

#以下參數指定了哪個cpu分配給哪個進程,一般來說不用特殊指定。如果一定要設的話,用01指定分配方式.
#這樣設就是給1-4個進程分配單獨的核來運行,出現第5個進程是就是隨機分配了。eg:
worker_processes 4     #4CPU 
worker_cpu_affinity 0001 0010 0100 1000

#定義全局錯誤日誌定義類型,[debug|info|notice|warn|crit]
error_log  logs/error.log  info;

#指定進程ID存儲文件位置
#pid        logs/nginx.pid;

#一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)與nginx進程數相除,但是nginx分配請求並不是那麼均勻,所以最好與ulimit -n的值保持一致。
#vim /etc/security/limits.conf
#  *                soft    nproc          65535
#  *                hard    nproc          65535
#  *                soft    nofile         65535
#  *                hard    nofile         65535
worker_rlimit_nofile 65535;  

三、nginx的日誌切割(日誌打包)

在nginx開啓的時候,日誌爲空
在這裏插入圖片描述

在真機進行壓測後查看日誌:

[root@foundation26 ~]# ab -c 1 -n 10000 http://172.25.26.1/index.html

[root@server1 logs]# cat access.log #會發現日誌內容(全是200)
在這裏插入圖片描述

注意:200表示正常; 503表示服務器端不可用

  • 對日誌進行切割
  • 如何實現對企業中前一天的日誌進行打包?
[root@server1 logs]# mv access.log `date +%F -d -1day`_access.log
[root@server1 logs]# ls
2019-04-23_access.log  error.log  nginx.pid
[root@server1 logs]# nginx -s reopen	#會重新生成日誌
[root@server1 logs]# ls
2019-04-23_access.log  access.log  error.log  nginx.pid
[root@server1 logs]# cat access.log		#爲空
注意:當再次在真機進行壓測時,日誌會重新生成在access.log

四、Nginx 的 IO 多路複用

概念及其原理:
參考:https://www.cnblogs.com/mingaixin/p/5432716.html
https://www.jianshu.com/p/6a6845464770

1.修改配置文件

root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
 12 events {
 13     use epoll;	#io多路複用
 14     worker_connections  1024;
 15 }
	#limit_conn_zone 用來限制同一時間連接數,即併發限制
	#limit_req_zone 用來限制單位時間內的請求數,即速率限制,採用的漏桶算法"leakyt bucket"
	#zone=one:10m表示生成一個大小爲10M,名字爲one的內存區域,用來存儲訪問的頻次信息
	#定義一個名爲addr的limit_conn_zone $binary_remote_addr是限制同一客戶端的IP地址
 35         limit_conn_zone $binary_remote_addr zone=addr:10m;
 36         limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
 48         location /download {
 49                 limit_conn addr 1;	#同一個時候只能1個併發
 50 }

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload
[root@server1 logs]# >access.log 
[root@server1 logs]# cat access.log	#空
測試:在真機進行壓力測試(日誌查看會發現同一IP訪問的某些會503):
[root@foundation26 ~]# ab -c 10 -n 1000 http://172.25.26.1/download/vim.jpg

在這裏插入圖片描述

注意:因爲限制併發數爲1,所以此處以10個作實驗時,會出現報錯
在這裏插入圖片描述

五、圖片壓縮

[root@server1 modules]# pwd
/usr/local/nginx/modules
[root@server1 modules]# ls
ngx_http_image_filter_module.so		#編譯的時候安裝的過濾模塊路徑
[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
  1 load_module modules/ngx_http_image_filter_module.so;
 49         location /download {
 50                 limit_conn addr 1;
 51                 image_filter resize 150 100;
 52 }

在這裏插入圖片描述
在這裏插入圖片描述

[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

在這裏插入圖片描述

測試:

瀏覽器輸入172.25.26.1/download/vim.jpg(F12操作下進入Network標籤下F5刷新會發現圖片大小顯示12.88kb)
在這裏插入圖片描述

六、訪問限速

1.修改配置文件

root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
 49         location /download {
 50                 limit_conn addr 1;
 51                 limit_rate 50k;
 52                 image_filter resize 150 100;
 53 }
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

測試:

在真機裏面進行壓測,會發現速度受到限制
[root@foundation26 ~]# ab -c 10 -n 1000 http://172.25.26.1/download/vim.jpg
在這裏插入圖片描述

七、Nginx 反向代理的實現

server2作爲反向代理服務器(安裝nginx)
在這裏插入圖片描述

搭建步驟:

1.修改作爲web服務器server1的nginx

[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
 37     server {
 38         listen       80;
 39         server_name  localhost;
 40         set_real_ip_from 172.25.26.2;	#設置可以獲得真實IP的代理端地址
 41         real_ip_header X-Forwarded-For;	#獲得客戶端訪問代理的請求報文頭獲得真實IP
 42         real_ip_recursive on;		#打開獲取功能

在這裏插入圖片描述

2.修改用來做反代的server2的nginx配置文件

[root@server2 conf]# pwd
/usr/local/nginx/conf
[root@server2 conf]# vim nginx.conf
 17 http {
 18     include       mime.types;
 19     default_type  application/octet-stream;
 20         upstream westos {
 21                 server 172.25.26.1:80;
 22 }
119 server {			#添加虛擬主機
120         listen 80;
121         server_name www.westos.org;
122 
123         location / {
124                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
125                 proxy_pass http://westos;
126         }
127 }

在這裏插入圖片描述
在這裏插入圖片描述

3.檢測語法錯誤,重新加載配置

[root@server2 conf]# nginx -t
[root@server2 conf]# nginx -s reload
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload
[root@server1 html]# pwd
/usr/local/nginx/html
[root@server1 html]# rm -rf index.html 
[root@server1 html]# vim index.html
[root@server1 html]# cat index.html 
server1

測試:

在真機裏寫入解析,curl www.westos.org會發現訪問的是代理,但獲得的內容是server1:nginx作爲web服務器
[root@foundation26 ~]# vim /etc/hosts
172.25.26.2 www.westos.org
在這裏插入圖片描述

注意:也可以在server1的nginx的日誌裏看到訪問信息
在這裏插入圖片描述

八、Nginx 實現https加密

1.修改配置文件,將註釋的加密模板打開,進行修改

[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
 40         #set_real_ip_from 172.25.26.2;
 41         #real_ip_header X-Forwarded-For;
 42         #real_ip_recursive on;
107     server {
108         listen       443 ssl;
109         server_name  localhost;
110 
111         ssl_certificate      cert.pem;
112         ssl_certificate_key  cert.pem;
113 
114         ssl_session_cache    shared:SSL:1m;
115         ssl_session_timeout  5m;
116 
117         ssl_ciphers  HIGH:!aNULL:!MD5;
118         ssl_prefer_server_ciphers  on;
119 
120         location / {
121             root   /web;
122             index  index.html index.htm;
123         }
124     }
125 
126 }

在這裏插入圖片描述
在這裏插入圖片描述

2.創建配置文件裏添加的信息文件
在這裏插入圖片描述

3.生成加密證書

[root@server1 certs]# pwd
/etc/pki/tls/certs
[root@server1 certs]# ls
ca-bundle.crt  ca-bundle.trust.crt  make-dummy-cert  Makefile  renew-dummy-cert
[root@server1 certs]# make cert.pem
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:Linux
Common Name (eg, your name or your server's hostname) []:server1	#加密主機hostname
Email Address []:root@www.westos.org
[root@server1 certs]# cp cert.pem /usr/local/nginx/conf/	#將證書複製到nginx的配置文件目錄下
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

測試:

在真機裏寫入解析
[root@foundation26 ~]# vim /etc/hosts
#172.25.26.2 www.westos.org
172.25.26.1 www.westos.org
瀏覽器裏輸入https://www.westos.org/在添加證書後可以看到信息
在這裏插入圖片描述
在這裏插入圖片描述
[root@server1 conf]# netstat -antlp #可以看到443端口被打開
在這裏插入圖片描述

九、Nginx 的重定向

目前訪問www.westos.org需要加密時,需要手動https://www.westos.org
目的:讓訪問www.westos.org自動被加密

1.修改配置文件

[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
109         server_name  localhost;
120         location / {
121             root   /web;
122             index  index.html index.htm;
123         }
124     }
125 server {
126         listen 80;
127         server_name www.westos.org;
128         rewrite ^/(.*)$ https://www.westos.org/$1;
129 }
130 }

在這裏插入圖片描述

[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

瀏覽器輸入www.westos.org會自動加密,定向到https://www.westos.org
在這裏插入圖片描述
[root@foundation26 ~]# curl -I www.westos.org #302臨時重定向
在這裏插入圖片描述

  • 永久重定向的設置
[root@server1 conf]# vim nginx.conf
128         rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

在這裏插入圖片描述

測試:
[root@foundation26 ~]# curl -I www.westos.org
在這裏插入圖片描述

  • 不同域名的重定向
[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
109         server_name  www.westos.org bbs.westos.org;
121             root   /bbs;
128         #rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
129         rewrite ^/bbs$ https://bbs.westos.org/index.html permanent;		#以bbs結尾的重定向
[root@server1 conf]# mkdir /bbs
[root@server1 conf]# vim /bbs/index.html
[root@server1 conf]# cat /bbs/index.html 
<h1>bbs.westos.org</h1>
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

在這裏插入圖片描述

在這裏插入圖片描述

測試:真機裏添加解析
[root@foundation26 ~]# vim /etc/hosts
172.25.26.1 www.westos.org bbs.westos.org

瀏覽器輸入www.westos.org/bbs後添加證書,會重定向到https://bbs.westos.org/index.html會看到/bbs裏的內容
在這裏插入圖片描述

在這裏插入圖片描述

  • 不同域名訪問同一資源
[root@server1 conf]# vim nginx.conf
125 server {
126         listen 80;
127         server_name www.westos.org bbs.westos.org;
128         #rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
129         #rewrite ^/bbs$ https://bbs.westos.org/index.html permanent;
130         if ($host = "bbs.westos.org"){
131                 rewrite ^(/.*)$ http://www.westos.org/bbs/$1 permanent;
132 }
133         location / {
134         root /web;
135         index index.html;
136 }
133 }
134 }

在這裏插入圖片描述

[root@server1 web]# cp -r /bbs/ .
[root@server1 web]# ls
bbs  index.html
[root@server1 web]# nginx -t
[root@server1 web]# nginx -s reload

在這裏插入圖片描述

測試:
訪問bbs.westos.org會定向到http://www.westos.org/bbs//
在這裏插入圖片描述

訪問www.westos.org會訪問/web下
在這裏插入圖片描述

十、盜鏈現象及其防止措施

環境設置:讓server2作爲web服務器盜鏈server1的資源

1.修改配置文件

[root@server2 conf]# vim nginx.conf
 20 #       upstream westos {
 21 #               server 172.25.26.1:80;
 22 #}
119 server {
120         listen 80;
121         server_name daolian.westos.org;
122         charset utf-8;
123         location / {
124                 root /web;
125                 index index.html;
126         }
127 }
128 }

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

2.將資源創建,修改配置文件

[root@server1 conf]# cd /usr/local/nginx/html/download/
[root@server1 download]# ls
vim.jpg
[root@server1 download]# mv vim.jpg /web/
[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
130 #       if ($host = "bbs.westos.org"){
131 #               rewrite ^(/.*)$ http://www.westos.org/bbs/$1 permanent;
132 #}

在這裏插入圖片描述
3.兩邊同時重新加載配置

[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload
[root@server2 conf]# nginx -t
[root@server2 conf]# nginx -s reload

測試:修改真機解析
[root@foundation26 ~]# vim /etc/hosts
172.25.26.2 daolian.westos.org
172.25.26.1 www.westos.org bbs.westos.org

瀏覽器輸入daolian.westos.org會盜鏈到server1的資源
在這裏插入圖片描述

  • 如何防止盜鏈?

1.在server1上修改配置文件

[root@server1 conf]# vim nginx.conf
133         location / {
134         root /web;
135         index index.html;
136 }
137 location ~* \.(gif|jpg|png|jpeg)$ {
138                 root    /web;
139                 valid_referers none blocked www.westos.org;
140                 if ($invalid_referer) {
141                         return 403;
142                 }
143         }
144 }
145 }
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

在這裏插入圖片描述

測試:瀏覽器輸入daolian.westos.org此時會看不見內容
在這裏插入圖片描述

  • 如何讓盜鏈者看到自己指定的內容
[root@server1 conf]# vim nginx.conf
125 server {
126         listen 80;
127         server_name www.westos.org;
137 location ~* \.(gif|jpg|png|jpeg)$ {
138                 root    /web;
139                 valid_referers none blocked www.westos.org;
140                 if ($invalid_referer) {
141                         #return 403;
142                         rewrite ^/ http://bbs.westos.org/daolian.jpg;
143                 }
144         }
145 }
146 server {
147         listen 80;
148         server_name bbs.westos.org;
149         location / {
150                 root /bbs;
151                 index index.html;
152 }
153 }
154 }

在這裏插入圖片描述

[root@server1 bbs]# pwd
/bbs
[root@server1 bbs]# ls
hello.jpg  index.html
[root@server1 bbs]# mv hello.jpg daolian.jpg
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload

測試:瀏覽器輸入daolian.westos.org會出現放置的圖片
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章