目錄結構
編譯后會有:conf html logs sbin 四個文件 (其他兩個是之前下載的安裝包)
- conf:配置文件
- html:頁面資源
- logs:日志
- sbin:啟動文件,nginx主程序
運行后多了文件:<font style="color:rgb(51,51,51);">client_body_temp fastcgi_temp proxy_temp scgi_temp</font>
這些都是臨時文件,可忽略
運行原理
master主進程:
Worker為子進程
nginx配置基礎
- 基礎配置
# 工作worker子進程(小于cpu數:并行 -- 大于CPU數:并發)
worker_processes 1;# worker的最大連接
events {worker_connections 1024;
}http {# include:引入其他配置文件# mime.types:請求頭(發送類型)# default_type:默認發送類型include mime.types;default_type application/octet-stream;# 數據零拷貝sendfile on;# 長連接 時間keepalive_timeout 65;# 虛擬主機vhost(nginx可配置多個主機)server {# nginx端口號listen 80;# 主機名server_name localhost;# http://localhost:80/【localtion中的uri】location / {root html;index index.html index.htm;}# 服務器端發生錯誤的時候,轉到/50x.html地址頁面# 轉到/50x.html的時候后,會在html文件夾中,尋找50x.html頁面error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
}
<font style="color:rgb(38, 38, 38);background-color:rgb(245, 245, 245);">mime.types</font>
請求頭類型
types {text/html html htm shtml;text/css css;text/xml xml;image/gif gif;image/jpeg jpeg jpg;application/javascript js;application/atom+xml atom;application/rss+xml rss;text/mathml mml;text/plain txt;text/vnd.sun.j2me.app-descriptor jad;text/vnd.wap.wml wml;text/x-component htc;image/avif avif;image/png png;image/svg+xml svg svgz;image/tiff tif tiff;image/vnd.wap.wbmp wbmp;image/webp webp;image/x-icon ico;image/x-jng jng;image/x-ms-bmp bmp;font/woff woff;font/woff2 woff2;application/java-archive jar war ear;application/json json;application/mac-binhex40 hqx;application/msword doc;application/pdf pdf;application/postscript ps eps ai;application/rtf rtf;application/vnd.apple.mpegurl m3u8;application/vnd.google-earth.kml+xml kml;application/vnd.google-earth.kmz kmz;application/vnd.ms-excel xls;application/vnd.ms-fontobject eot;application/vnd.ms-powerpoint ppt;application/vnd.oasis.opendocument.graphics odg;application/vnd.oasis.opendocument.presentation odp;application/vnd.oasis.opendocument.spreadsheet ods;application/vnd.oasis.opendocument.text odt;application/vnd.openxmlformats-officedocument.presentationml.presentationpptx;application/vnd.openxmlformats-officedocument.spreadsheetml.sheetxlsx;application/vnd.openxmlformats-officedocument.wordprocessingml.documentdocx;application/vnd.wap.wmlc wmlc;application/wasm wasm;application/x-7z-compressed 7z;application/x-cocoa cco;application/x-java-archive-diff jardiff;application/x-java-jnlp-file jnlp;application/x-makeself run;application/x-perl pl pm;application/x-pilot prc pdb;application/x-rar-compressed rar;application/x-redhat-package-manager rpm;application/x-sea sea;application/x-shockwave-flash swf;application/x-stuffit sit;application/x-tcl tcl tk;application/x-x509-ca-cert der pem crt;application/x-xpinstall xpi;application/xhtml+xml xhtml;application/xspf+xml xspf;application/zip zip;application/octet-stream bin exe dll;application/octet-stream deb;application/octet-stream dmg;application/octet-stream iso img;application/octet-stream msi msp msm;audio/midi mid midi kar;audio/mpeg mp3;audio/ogg ogg;audio/x-m4a m4a;audio/x-realaudio ra;video/3gpp 3gpp 3gp;video/mp2t ts;video/mp4 mp4;video/mpeg mpeg mpg;video/quicktime mov;video/webm webm;video/x-flv flv;video/x-m4v m4v;video/x-mng mng;video/x-ms-asf asx asf;video/x-ms-wmv wmv;video/x-msvideo avi;
}
- 開啟數據零拷貝工作比較
沒開啟sendfile:nginx讀取到html,然后存入緩存,接著將緩存內容發給網絡接口,網絡接口發送給互聯網
開啟sendfile:nginx不緩存html了,直接給網絡接口發送信號,讓網絡接口直接來讀取html,然后發送到互聯網
- 全部配置
#user nobody;
worker_processes 1;#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;
}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;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;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;# }#}
虛擬主機配置
域名配置
將域名綁定ip
不同端口訪問不同資源
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 /www/video;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 81;server_name localhost;location / {root /www/www;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}
不同域名訪問同一資源
匹配
不同的域名,都會匹配到80端口
server {listen 80;server_name www.xingheng.cn a.xingheng.cn;location / {root /www/video;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
匹配規則
- 通配符匹配
server_name *.mmban.com
- 通配符結束匹配
server_name vod.*;
- 正則匹配
server_name ~^[0-9]+\.mmban\.com$;
反向代理
正向代理與反向代理
主要區別在于這兩種方式的作用不同,他們的本質其實是一樣的(請求轉發),代理代理,就是代替某個東西去做什么
- 一個是代理客戶端,就是代理用戶,去訪問外網服務器
- 一個是代理服務器,就是代理服務器,讓用戶可以訪問
代理規則
proxy_pass:代理路徑 – 有了proxy_pass就不會訪問root /www/video; index index.html index.html;
了
location / {root /www/video;index index.html index.htm;proxy_pass http://xingheng.com/;
}
基于反向代理的負載均衡
使用
# httpd是負載均衡組的名字,可自定義
upstream httpd {server 192.168.44.102:80;server 192.168.43.103:80;
}location / {root /www/video;index index.html index.htm;proxy_pass http://httpd # 有這個上面的靜態資源就不訪問了}
常用策略
upstream httpd {server 127.0.0.1:8050 weight=10 down;server 127.0.0.1:8060 weight=1;server 127.0.0.1:8060 weight=1 backup;
}
- weight:默認為1 weight越大,負載的權重就越大。
- down:表示當前的server暫時不參與負載
- backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。(備用服務器)
了解策略
- ip_hash:根據客戶端的ip地址轉發同一臺服務器,可以保持回話。
- least_conn:最少連接訪問
- url_hash:根據用戶訪問的url定向轉發請求
- fair:根據后端服務器響應時間轉發請求
動靜分離
location配置靜態資源
server {listen 80;server_name localhost;# / 的匹配優先級最低location / {proxy_pass http://127.0.0.1:8080;}# 匹配路徑是/js的路徑location /js {root htmlindex index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
}
路徑匹配規則
location前綴規則
/ 通用匹配,任何請求都會匹配到。
= 精準匹配,不是以指定模式開頭
~ 正則匹配,區分大小寫
~* 正則匹配,不區分大小寫
^~ 非正則匹配,匹配以指定模式開頭的location
location匹配順序
- 多個正則location直接按書寫順序匹配,成功后就不會繼續往后面匹配
- 普通(非正則)location會一直往下,直到找到匹配度最高的(最大前綴匹配)
- 當普通location與正則location同時存在,如果正則匹配成功,則不會再執行普通匹配
- 所有類型location存在時,“=”匹配 > “^~”匹配 > 正則匹配 > 普通(最大前綴匹配)
location ~*/(css|img|js) {root /usr/local/nginx/static;index index.html index.htm;
}
alias與root
location /css/ {alias /usr/local/nginx/static/css/;index index.html index.htm;
}
location /css {alias /usr/local/nginx/static;index index.html index.htm;
}
alias和root指令功能類似,都是指定訪問的資源,不過配置上有些區別:
**root**
** 是“加在 URI 前面的前綴”
**alias**
**是“把整個 location 路徑替換掉”**root**
** 通常用于匹配路徑不變的情況
**alias**
**用于重寫某段 URL 到另一路徑**root**
對/
不敏感
**alias**
的路徑末尾最好加/
,否則容易匹配失敗
例如:代碼中的代碼路徑,現在要訪問/css/style.css
root是直接拼接:/usr/local/nginx/static + /css/style.css
alias是裁剪:/css/style.css
匹配,裁剪location
后的路徑/css/
,變為style.css
,然后將style.css
拼接到/usr/local/nginx/static/css/
后面
UrlRewrite
入門案例
location / {rewrite ^/xingheng/(.*)$ /index.jsp?pageName=$1 break;proxy_pass http://127.0.0.1:8080;
}
如果訪問呢的路徑匹配到了/xingheng
,那么就轉向訪問http://127.0.0.1:8080/index.jsp?pageName=?
路徑
也就是說,如果匹配到了rewirte終點額正則表達式,那么就會講后面的路徑拼接到proxy_pass中設置的路徑后面
規則
rewrite是實現URL重寫的關鍵指令,根據regex (正則表達式)部分內容,重定向到replacement,結尾是flag標記。
格式:
rewrite <regex> <replacement> [flag];
關鍵字:正則(regex) 替代內容(replacement) flag標記
關鍵字:其中關鍵字error_log不能改變
- 正則:perl兼容正則表達式語句進行規則匹配
- 替代內容:將正則匹配的內容替換成replacement
- flag標記:rewrite支持的flag標記
rewrite參數的標簽段位置: <font style="color:rgb(51,51,51);">server,location,if </font>
flag標記說明:
- last :本條規則匹配完成后,繼續向下匹配新的location URI規則
- break :本條規則匹配完成即終止,不再匹配后面的任何規則
- redirect :返回302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址(就是真實地址會帶出來)
- permanent :返回301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址
防盜鏈
防盜鏈概念
- 所謂盜鏈,就是別人直接在他們的網站上引用你網站的資源,比如這樣:
<img src="http://yourdomain.com/images/logo.png">
這樣一來,對方的網頁訪問者會去訪問你的服務器資源,消耗你的帶寬資源,卻不給你任何訪問量或廣告收益,所以我們需要防盜鏈
- referer
假設你在 https://a.com/index.html
這個頁面上,有一張圖片:
<img src="https://b.com/images/logo.png">
當用戶訪問 a.com/index.html
頁面時,瀏覽器會自動去加載圖片,這個時候對 b.com/images/logo.png
的請求里,HTTP 請求頭中會自動帶上:
Referer: https://a.com/index.html
也就是說,服務器 **b.com**
可以知道:訪問這張圖的是從 **a.com**
頁面跳過來的。
那么什么時候沒有refer呢?直接在瀏覽器頂部地址輸入對應的地址訪問就沒有referer啦
配置防盜鏈
- 配置案例
valid_referers 192.168.44.101;
if ($invalid_referer) {return 403;
}
- 詳細規則
valid_referers none | blocked | server_names | strings ....;
- `**<font style="color:#DF2A3F;">none</font>**`<font style="color:rgb(51,51,51);">, 檢測 Referer 頭域不存在的情況,如果不存在,那么就可以訪問(即使是本網站訪問也不行)</font>
- `<font style="color:rgb(51,51,51);">blocked</font>`<font style="color:rgb(51,51,51);">,檢測 Referer 頭域的值被防火墻或者代理服務器刪除或偽裝的情況。這種情況該頭域的值不以 “http://” 或 “https://” 開頭。 </font>
- `<font style="color:rgb(51,51,51);">server_names</font>`<font style="color:rgb(51,51,51);"> ,設置一個或多個 URL ,檢測 Referer 頭域的值是否是這些 URL 中的某一個。</font>
使用curl測試
安裝
yum install -y curl
常用命令
- 看某個地址能否訪問
curl -I http://192.168.44.101/img/logo.png
- 帶referer
# http://baidu.com就是refer
curl -e "http://baidu.com" -I http://192.168.44.101/img/logo.png
高可用配置
keepalive高可用原理
keepalive安裝
yum install -y keepalived
keepalive配置
- 打開文件
# 打開keeplived.conf
cd /etc/keepalived
vi keepalived.conf
- 配置文件
第一臺機器
! Configuration File for keepalivedglobal_defs {router_id lb111
}vrrp_instance VI_1 {# 當前機器是masterstate MASTER# 網卡名稱(改)interface enp0s5virtual_router_id 51# 優先級,競選成功的優先級priority 100# 間隔檢測時間advert_int 1# 多個keepalived通信配置(相同即可)authentication {auth_type PASSauth_pass 1111}# 虛擬地址(改)virtual_ipaddress {192.168.33.200}
}
注:interface需要改成自己的網卡地址,通過ip addr
獲取
第二臺機器
! Configuration File for keepalivedglobal_defs {router_id lb111
}vrrp_instance VI_1 {# 當前機器是backupstate BACKUP# 網卡名稱interface enp0s5virtual_router_id 51# 優先級,競選成功的優先級priority 100# 間隔檢測時間advert_int 1# 多個keepalived通信配置(相同即可)authentication {auth_type PASSauth_pass 1111}# 虛擬地址virtual_ipaddress {192.168.33.200}
}
- 啟動服務
systemctl start keepalived
查看狀態:
systemctl status keepalived
查看現有ip:
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.200.16192.168.200.17192.168.200.18}
}virtual_server 192.168.200.100 443 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50protocol TCPreal_server 192.168.201.100 443 {weight 1SSL_GET {url {path /digest ff20ad2481f97b1754ef3e12ecd3a9cc}url {path /mrtg/digest 9b3a0c85a887a256d6939da88aabd8cd}connect_timeout 3retry 3delay_before_retry 3}}
}virtual_server 10.10.10.2 1358 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50protocol TCPsorry_server 192.168.200.200 1358real_server 192.168.200.2 1358 {weight 1HTTP_GET {url {path /testurl/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl2/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl3/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}connect_timeout 3retry 3delay_before_retry 3}}real_server 192.168.200.3 1358 {weight 1HTTP_GET {url {path /testurl/test.jspdigest 640205b7b0fc66c1ea91c463fac6334c}url {path /testurl2/test.jspdigest 640205b7b0fc66c1ea91c463fac6334c}connect_timeout 3retry 3delay_before_retry 3}}
}virtual_server 10.10.10.3 1358 {delay_loop 3lb_algo rrlb_kind NATpersistence_timeout 50protocol TCPreal_server 192.168.200.4 1358 {weight 1HTTP_GET {url {path /testurl/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl2/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl3/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}connect_timeout 3retry 3delay_before_retry 3}}real_server 192.168.200.5 1358 {weight 1HTTP_GET {url {path /testurl/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl2/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}url {path /testurl3/test.jspdigest 640205b7b0fc66c1ea91c463fac6334d}connect_timeout 3retry 3delay_before_retry 3}}
}
Https證書配置
server
{listen 443 ssl http2 ;server_name www.xinghengdati.cn;ssl_certificate /www/server/panel/vhost/cert/www.xinghengdati.cn/fullchain.pem;ssl_certificate_key /www/server/panel/vhost/cert/www.xinghengdati.cn/privkey.pem;
}