是一個Web服務器批狱,也可以用作反向代理,負載平衡器和HTTP緩存霎挟。
安裝
- 編譯安裝
linux下安裝方式:
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel //下載依賴庫
configure --prefix=/opt/nginx/ //進行環(huán)境配置,設置安裝位置
maks && make install // 編譯安裝
mac下安裝方式:
brew install nginx // 會自動安裝需要的依賴
提示信息:
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /usr/local/etc/nginx/servers/.
To have launchd start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
nginx
HomeBrew 是一款自由即開放源代碼的軟件包管理系統夷陋,用于Mac OS X系統上的軟件安裝過程落蝙。
依賴列表:
gcc | 編譯程序 |
---|---|
pcre pcre-devel | configure過程需要的正則表達式 |
zlib zlib-devel | 傳輸內容壓縮 |
openssl openssl-devel | 開啟https支持需要 |
注意:devel包含普通包,且多了頭文件治泥,編譯時需要筹煮。
補充:Https、SSL與OpenSSL三者關系
HTTPS | Hyper Text Transfer Protocol over Secure Socket Layer | HTTP的加密版本居夹,底層使用SSL作為加密協議 |
---|---|---|
SSL | Secure Socket Layer(安全套接字層) | 在客戶端和服務器之間建立一條SSL安全通道的加密協議 |
OpenSSL | -- | TLS/SSL協議的開源實現败潦,提供開放庫和命令行程序 |
TLS | Transport Layer Security(傳輸層安全協議) | 用于兩個應用程序之間提供保密性和數據完整性 |
啟動與關閉
進入nginx安裝目錄的sbin
文件夾
執(zhí)行nginx
,將啟動兩個nginx進程准脂,分別是:
master 進程:守護進程
work進程 :用于響應請求劫扒。
執(zhí)行nginx -s stop
關閉進程
執(zhí)行nginx -s reload
重啟進程
自啟動:編輯系統啟動自動執(zhí)行的腳本文件,在centos下是/etc/rc.d/rc.local 狸膏,增加nginx啟動命令即可沟饥。
配置
編輯 nginx.conf 文件
# 頂層配置信息,管理服務器級別行為
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# event指令與事件模型有關湾戳,配置處理連接有關信息
events {
worker_connections 1024;
}
# http指令處理http請求
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指令指定nginx是否調用sendfile函數來輸出文件,對于普通應用設為 on院塞,如果用來進行下載等應用磁盤IO重負載應用遮晚,可設置為off,以平衡磁盤與網絡I/O處理速度拦止,降低系統的負載县遣。注意:如果圖片顯示不正常把這個改 成off糜颠。
sendfile on;
#防止網絡阻塞
#tcp_nopush on;
#長連接超時時間,單位是秒
#keepalive_timeout 0;
keepalive_timeout 65;
#開啟gzip壓縮輸出
#gzip on;
#虛擬主機的配置萧求,根據server_name進行匹配其兴,當匹配不到時將請求發(fā)給第一個server
server {
#監(jiān)聽端口
listen 8080;
#域名可以有多個,用空格隔開
server_name localhost;
#默認編碼
charset utf-8;
#定義本虛擬主機的訪問日志
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
allow 192.168.10.100;
allow 172.29.73.0/24;
deny all;
}
#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;
# }
#}
include servers/*;
}
location 表達式
syntax:location [=|~|~*|^~|@] /uri/ {…}
分為兩種匹配模式 :
- 普通字符串匹配:無開頭引導字符或以=開頭表示普通字符串的匹配
- 正則匹配:以
~
或~*
開頭表示正則匹配~*
表示不區(qū)分大小寫
多個location時匹配規(guī)則:先普通后正則夸政,只識別URI部分元旬。例如請求為 /test/1/abc.do?arg=xxx
:
先查找是否有=開頭的精確匹配,即
location = /test/1/abc.do {...}
-
再查找普通匹配守问,以 最大前綴 為規(guī)則匀归,如有以下兩個location
location /test/ {...} location /test/1 {...}
則匹配后一項
匹配到一個普通格式后,搜索并未結束耗帕,而是暫存當前結果穆端,并繼續(xù)搜索正則模式
在所有正則模式location中找到第一個匹配項后,以此匹配項為最終結果仿便。所有正則匹配項匹配規(guī)則受定義前后順序影響体啰,但普通匹配不會
如果未找到正則匹配項,則以3中緩存的結果為最終結果
如果一個匹配都沒有嗽仪,返回404
location =/ {…}
與 location / {...}
的差別:
前一個是精確匹配荒勇,只響應/
請求,所有/xxx
類請求不會以前綴匹配形式匹配到它
而后一個正相反闻坚,所有請求必然以/
開頭沽翔,所以沒有其他匹配結果時一定會執(zhí)行到它。
location ^~ / {...}
^~
表示非正則鲤氢,表示匹配到此模式后不再繼續(xù)正則搜索搀擂。因為一個請求在普通匹配規(guī)則下沒得到其它普通匹配結果時,最終會匹配到這里卷玉,而這里又不允許正則哨颂,相當于匹配到此為止。 /test/abc.jsp
deny all;
拒絕請求相种,返回403
allow 192.168.10.0/24;
允許指定IP段通過
proxy_pass http://192.168.1.61:8080;
將匹配的請求代理到另一地址進行處理
@類似于變量定義威恼,比如:
error_page 403 @page403;
locaion @page403 {
proxy_pass http://www.xxx.com
}