学习Nging笔记
1. Nginx負(fù)載均衡配置
worker_processes 1;events {worker_connections 1024; } http {include mime.types;default_type application/octet-stream;sendfile on;#httpds命名要和proxy_pass代理的名一樣,下面就就是輪詢兩臺(tái)nginx服務(wù)器upstream httpds{server 192.168.3.11:80;server 192.168.3.12:80;}server {listen 80;server_name localhost;location / {proxy_pass http://httpds;root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} }2. 輪詢權(quán)重配置
worker_processes 1;events {worker_connections 1024; }http {include mime.types;default_type application/octet-stream;sendfile on;# httpds命名要和proxy_pass代理的名一樣,下面就就是輪詢兩臺(tái)nginx服務(wù)器# weight 代表權(quán)重# down 代表下線# backup備用機(jī),其他機(jī)器都下線或者宕機(jī)了,就用備用機(jī)upstream httpds{server 192.168.3.11:80 weight=8 down;server 192.168.3.12:80 weight=2 down;server 192.168.3.13:80 weight=1 backup;}server {listen 80;server_name localhost;location / {proxy_pass http://httpds;root html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} }3. nginx動(dòng)靜分離(把一些靜態(tài)資源放在nginx,而不是在后臺(tái)服務(wù)中)
在nginx.conf中配置多個(gè)location放在server塊中,把靜態(tài)資源放在html目錄下
# css配置location /css {root html;index index.html index.htm;}#js配置location /js {root html;index index.html index.htm;}# 圖片配置location /img {root html;index index.html index.htm;}合并配置(正則匹配)
location ~*/(js|img|css) {root html;index index.html index.htm;}4. URLrewrite 偽靜態(tài)配置(隱藏真實(shí)地址)
location / {#^/2.html是要訪問(wèn)的地址 # /index.jsp?pageNum=2 真實(shí)訪問(wèn)地址rewrite ^/2.html /index.jsp?pageNum=2 break;proxy_pass http://192.168.3.13;}正則匹配
location / {# 這樣就不用和上面一樣寫死rewrite ^/([0-9]+).html$ /index.jsp?pageNum=&1 break;proxy_pass http://192.168.3.13;}5. 開(kāi)啟防火墻,只能由指定的服務(wù)器訪問(wèn)
開(kāi)啟對(duì)應(yīng)的防火墻(不對(duì)外開(kāi)放的)
systemctl start firewalld添加指定端口和ip訪問(wèn)(添加之后記得重新啟動(dòng)防火墻)
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.3.10" port protocol="tcp" port="80" accept"移除規(guī)則
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.3.10" port protocol="tcp" port="80" accept"重啟防火墻
firewall-cmd --reload查看已配置規(guī)則
firewall-cmd --list-all6. 防盜鏈
下面防止圖片被盜
location ^~/images/ {valid_referers 192.168.3.10; #valid_referers 指令,配置是否允許 referer 頭部以及允許哪些 referer 訪問(wèn)。 192.168.3.10 不是ip而是域名(去掉http:// 前綴)if ($invalid_referer) { # 注意這里if后要加空格return 403; ## 返回錯(cuò)誤碼}root /www/resources;}參數(shù)值
none:允許沒(méi)有 referer 信息的請(qǐng)求訪問(wèn),即直接通過(guò)url訪問(wèn)。
blocked:請(qǐng)求頭Referer字段不為空(即存在Referer),但是值可以為空(值被代理或者防火墻刪除了),并且允許refer不以“http://”或“https://”開(kāi)頭,通俗點(diǎn)說(shuō)就是允許“http://”或"https//"以外的請(qǐng)求。
server_names:若 referer 中站點(diǎn)域名與 server_name 中本機(jī)域名某個(gè)匹配,則允許該請(qǐng)求訪問(wèn)
其他字符串類型:檢測(cè)referer與字符串是否匹配,如果匹配則允許訪問(wèn),可以采用通配符*
正則表達(dá)式:若 referer 的值匹配上了正則,就允許訪問(wèn)
7. 利用curl測(cè)試防盜鏈
下載curl
yum install -y curl測(cè)試curl 192.168.3.10
沒(méi)有referer詳情下
有referer詳情下
8. 防盜鏈資源返回頁(yè)面錯(cuò)誤提示和圖片提示
新建一個(gè)錯(cuò)誤提示頁(yè)面
圖片提示
valid_referers 192.168.3.10;if ($invalid_referer) { # rewrite 到需要提示的圖片上面rewrite ^/ /img/3.jpg break;# return 401; }9. nginx高可用及Keepalived實(shí)戰(zhàn)
可以ping通
把10關(guān)閉
還是可以ping通
通過(guò)頁(yè)面訪問(wèn)
10. nginx線上實(shí)戰(zhàn)
前面需要買域名和買服務(wù)器
申請(qǐng)證書
3. 申請(qǐng)完就可以下載對(duì)應(yīng)的證書了
4. 上傳到nginx
server {
listen 80;
server_name upczt.com www.upczt.com; #修改成自己的域名
access_log /data/wwwlogs/access_nginx.log combined;
return 301 https:// s e r v e r n a m e server_name servern?amerequest_uri;
root html;
}
6. 保存配置文件重啟nginx
systemctl reload nginx
7. 加上https:// 變?yōu)榘踩珔f(xié)議了
8. 安裝discuz.net
9. 上傳到服務(wù)器
10. 解壓
11. 修改名字為bbs
12. Nginx配置springboot項(xiàng)目使用httos協(xié)議
3. 配置服務(wù)器的安全組端口8080
總結(jié)
- 上一篇: Exchange的介绍及使用
- 下一篇: ehcache java 工具,Ehca