1、Nginx簡介:
Nginx是一個高性能WEB服務器,除它之外Apache、Tomcat、Jetty、IIS,它們都是Web服務器,或者叫做WWW(World Wide Web)服務器,相應地也都具備Web服務器的基本功能。Nginx 相對基它WEB服務有什麼優勢呢?
1、 Tomcat、Jetty 面向java語言,先天就是重量級的WEB服務器,其性能與Nginx沒有可比性。
2、 IIS只能在Windows操作系統上運行。Windows作爲服務器在穩定性與其他一些性能上都不如類UNIX操作系統,因此,在需要高性能Web服務器的場合下IIS並不佔優。
3、 Apache的發展時期很長,而且是目前毫無爭議的世界第一大Web服務器,其有許多優點,如穩定、開源、跨平臺等,但它出現的時間太長了,在它興起的年代,互聯網的產業規模遠遠比不上今天,所以它被設計成了一個重量級的、不支持高併發的Web服務器。在Apache服務器上,如果有數以萬計的併發HTTP請求同時訪問,就會導致服務器上消耗大量內存,操作系統內核對成百上千的Apache進程做進程間切換也會消耗大量CPU資源,並導致HTTP請求的平均響應速度降低,這些都決定了Apache不可能成爲高性能Web服務器,這也促使了Lighttpd和Nginx的出現。 下圖可以看出07年到17 年強勁增長勢頭。
2、編譯與安裝
安裝環境準備:
(1)linux 內核2.6及以上版本:
只有2.6之後才支持epool ,在此之前使用select或pool多路複用的IO模型,無法解決高併發壓力的問題。通過命令uname -a 即可查看。
#查看 linux 內核
uname -a
(2)GCC編譯器
GCC(GNU Compiler Collection)可用來編譯C語言程序。Nginx不會直接提供二進制可執行程序,只能下載源碼進行編譯。
(3)PCRE庫
PCRE(Perl Compatible Regular Expressions,Perl兼容正則表達式)是由Philip Hazel開發的函數庫,目前爲很多軟件所使用,該庫支持正則表達式。
(4)zlib庫
zlib庫用於對HTTP包的內容做gzip格式的壓縮,如果我們在nginx.conf裏配置了gzip on,並指定對於某些類型(content-type)的HTTP響應使用gzip來進行壓縮以減少網絡傳輸量。
(5)OpenSSL開發庫
如果我們的服務器不只是要支持HTTP,還需要在更安全的SSL協議上傳輸HTTP,那麼就需要擁有OpenSSL了。另外,如果我們想使用MD5、SHA1等散列函數,那麼也需要安裝它。
上面幾個庫都是Nginx 基礎功能所必需的,爲簡單起見我們可以通過yum 命令統一安裝。
#yum 安裝nginx 環境
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
源碼獲取:
nginx 下載頁:http://nginx.org/en/download.html
# 下載nginx 最新穩定版本
wget http://nginx.org/download/nginx-1.19.0.tar.gz
#解壓
tar -zxvf nginx-1.19.0.tar.gz
最簡單的安裝:
# 全部採用默認安裝
./configure
make && make install
執行完成之後 nginx 運行文件 就會被安裝在 /usr/local/nginx 下。
基於參數構建
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-debug
控制命令:
#默認方式啓動:
./sbin/nginx
#指定配置文件啓動
./sbing/nginx -c /tmp/nginx.conf
#指定nginx程序目錄啓動
./sbin/nginx -p /usr/local/nginx/
#快速停止
./sbin/nginx -s stop
#優雅停止
./sbin/nginx -s quit
# 熱裝載配置文件
./sbin/nginx -s reload
# 重新打開日誌文件
./sbin/nginx -s reopen
Nginx 架構圖:
架構說明:
1)nginx啓動時,會生成兩種類型的進程,一個是主進程(Master),一個(windows版本的目前只有一個)和多個工作進程(Worker)。主進程並不處理網絡請求,主要負責調度工作進程,也就是圖示的三項:加載配置、啓動工作進程及非停升級。所以,nginx啓動以後,查看操作系統的進程列表,我們就能看到至少有兩個nginx進程。
2)服務器實際處理網絡請求及響應的是工作進程(worker),在類unix系統上,nginx可以配置多個worker,而每個worker進程都可以同時處理數以千計的網絡請求。
3)模塊化設計。nginx的worker,包括核心和功能性模塊,核心模塊負責維持一個運行循環(run-loop),執行網絡請求處理的不同階段的模塊功能,如網絡讀寫、存儲讀寫、內容傳輸、外出過濾,以及將請求發往上游服務器等。而其代碼的模塊化設計,也使得我們可以根據需要對功能模塊進行適當的選擇和修改,編譯成具有特定功能的服務器。
4)事件驅動、異步及非阻塞,可以說是nginx得以獲得高併發、高性能的關鍵因素,同時也得益於對Linux、Solaris及類BSD等操作系統內核中事件通知及I/O性能增強功能的採用,如kqueue、epoll及event ports。
Nginx 核心模塊:
Nginx 配置與使用
1、配置文件的語法格式:
先來看一個簡單的nginx 配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /nginx_status {
stub_status on;
access_log off;
}
}
}
上述配置中的events、http、server、location、upstream等屬於配置項塊。而worker_processes 、worker_connections、include、listen 屬於配置項塊中的屬性。 /nginx_status 屬於配置塊的特定參數參數。其中server塊嵌套於http塊,其可以直接繼承訪問Http塊當中的參數。
配置塊 | 名稱開頭用大口號包裹其對應屬性 |
---|---|
屬性 | 基於空格切分屬性名與屬性值,屬性值可能有多個項 都以空格進行切分 如: access_log logs/host.access.log main |
參數 | 其配置在 塊名稱與大括號間,其值如果有多個也是通過空格進行拆 |
注意 如果配置項值中包括語法符號,比如空格符,那麼需要使用單引號或雙引號括住配置項值,否則Nginx會報語法錯誤。例如:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
2、配置第一個靜態WEB服務
基礎站點演示:
- 創建站點目錄 mkdir -p /usr/www/burst
- 編寫靜態文件
- 配置 nginx.conf
- 配置server
- 配置location
基本配置介紹說明:
(1)監聽端口
語法:listen address:
默認:listen 80;
配置塊:server
(2)主機名稱
語法:server_name name[……];
默認:server_name “”;
配置塊:server
server_name後可以跟多個主機名稱,如server_name www.testweb.com、download.testweb.com;。 支持通配符與正則
(3)location
語法:location[=|~|~*|^~|@]/uri/{……}
配置塊:server
- / 基於uri目錄匹配
- =表示把URI作爲字符串,以便與參數中的uri做完全匹配。
- ~表示正則匹配URI時是字母大小寫敏感的。
- ~*表示正則匹配URI時忽略字母大小寫問題。
- ^~表示正則匹配URI時只需要其前半部分與uri參數匹配即可。
動靜分離演示:
- 創建靜態站點
- 配置 location /static
- 配置 ~* .(gif|png|css|js)$
基於目錄動靜分離
server {
listen 80;
server_name *.burst.com;
root /usr/www/burst;
location / {
index burst.html;
}
location /static {
alias /usr/www/static;
}
}
基於正則動靜分離
location ~* \.(gif|jpg|png|css|js)$ {
root /usr/www/static;
}
防盜鏈配置演示:
# 加入至指定location 即可實現
valid_referers none blocked *.burst.com;
if ($invalid_referer) {
return 403;
}
下載限速:
location /download {
limit_rate 1m;
limit_rate_after 30m;
}
創建IP黑名單
# 創建黑名單文件
echo 'deny 192.168.0.132;' >> balck.ip
#http 配置塊中引入 黑名單文件
include black.ip;
3、日誌配置:
日誌格式:
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;
#基於域名打印日誌
access_log logs/$host.access.log main;
error日誌的設置
語法:error_log /path/file level;
默認:error_log logs/error.log error;
level是日誌的輸出級別,取值範圍是debug、info、notice、warn、error、crit、alert、emerg
針對指定的客戶端輸出debug級別的日誌
語法:debug_connection[IP|CIDR]
events {
debug_connection 192.168.0.02;
debug_connection 10.224.57.0/200;
}
user root;
worker_processes 2;
worker_cpu_affinity 10 01;
worker_priority -10;
worker_rlimit_nofile 20;
#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;
debug_connection 192.168.0.132;
use epoll;
}
http {
include mime.types;
include black.ip;
default_type application/octet-stream;
log_format main '$host $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 *.burst.com default;
root /usr/www/burst;
#access_log logs/$server_name.access.log main;
access_log logs/$host.access.log main;
location / {
index burst.html;
}
location /static {
alias /usr/www/static;
}
location ~* \.(gif|jpg|png|css|js)$ {
root /usr/www/static;
valid_referers none blocked *.burst.com;
if ($invalid_referer) {
return 403;
}
}
location /download {
limit_rate 1m;
limit_rate_after 30m;
}
}
server {
listen *:80;
server_name www.tl.com ;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ \.(gif|jpg|jpeg|png|bmp|ico|css)$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8010;
}
location =/baidu.html {
proxy_pass http://www.baidu.com;
}
location /nginx_status {
stub_status on;
access_log off;
}
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}