日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

志宇-Nginx学习

發(fā)布時間:2024/3/26 Nginx 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 志宇-Nginx学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Nginx

  • nginx如何去處理一個請求
  • Nginx可用性探測
  • Nginx搭建靜態(tài)資源服務器
  • Nginx配置https服務
  • Nginx流量統(tǒng)計
  • Nginx黑白名單攔截
  • 異常兜底返回
  • OpenResty的學習
  • LVS + KeepLived了解

nginx如何去處理一個請求

How nginx processes a request
首先根據請求的域名向server節(jié)點尋找匹配的server_name,找到最匹配的server后,然后在此server節(jié)點中根據請求的url尋找一個最匹配的location,然后在location找到對應要訪問的upstream,location中設定了可以攜帶的請求頭,帶著可以攜帶的請求頭以upstream 中配置的策略訪問節(jié)點
location中對的url匹配規(guī)則(尋找最精準的進行匹配)

location [ = | ~ | ~* | ^~ ] = 精準匹配 location =/api {...} ~ 區(qū)分大小寫匹配 location ~/api {...} ~* 不區(qū)分大小寫匹配 location ~*/api {...} ^~/aaa(區(qū)分大小寫)為開頭的都匹配 location ^~/aaa {...}location ^~*/aaa/bbb$ {...} 必須以三個/aaa開頭,bbb結尾

nginx中可以使用哪些變量

名稱 說明 $arg_name 請求中的name參數 $args 請求中的參數 $content_length HTTP 請求信息里的"Content-Length" $content_type 請求信息里的"Content-Type" $host 請求信息中的"Host",如果請求中沒有Host行,則等于設置的服務器名 $hostname 機器名使用 gethostname系統(tǒng)調用的值 $http_cookie cookie 信息 $http_referer 引用地址 $http_user_agent 客戶端代理信息 $http_via 最后一個訪問服務器的Ip地址。 $http_x_forwarded_for 相當于網絡訪問路徑 $is_args 如果請求行帶有參數,返回“?”,否則返回空字符串 $limit_rate 對連接速率的限制 $nginx_version 當前運行的nginx版本號 $pid worker進程的PID $query_string 與$args相同 $remote_addr 客戶端IP地址 $remote_port 客戶端端口號 $request 用戶請求 $request_method 請求的方法,比如"GET""POST"等 $request_uri 請求的URI,帶參數 $scheme 所用的協(xié)議,比如http或者是https $server_name 請求到達的服務器名 $server_port 請求到達的服務器端口號 $server_protocol 請求的協(xié)議版本,"HTTP/1.0""HTTP/1.1" $uri 請求的URI,可能和最初的值有不同,比如經過重定向之類的

簡單使用如下

events { #在linux系統(tǒng)上使用這種模式會提高很大效率use epoll;#連接最大數為(worker_connections * worker_processes)worker_connections 1024; } ? server{...#5簡單防爬配置if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot") {return 403;}location / { # 1 允許代理節(jié)點獲得請求頭中的信息# 用戶的真實ip,如果不配置此信息代理節(jié)點獲得不到用戶的ipproxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#2 解決跨域配置#允許跨域請求的域,*和$http_origin 代表所有#不然會報No 'Access-Control-Allow-Origin' header is present on #the requested resource. Origin 'null' is therefore not allowed access.add_header 'Access-Control-Allow-Origin' $http_origin;#允許帶上cookie請求add_header 'Access-Control-Allow-Credentials' 'true';#允許攜帶哪些headeradd_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';#允許請求的方法,比如 GET/POST/PUT/DELETEadd_header Access-Control-Allow-Methods 'GET,POST,OPTIONS'; ?#3 如果預檢請求則返回成功,不需要轉發(fā)到后端,節(jié)約資源if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 200;}#4 防盜鏈設置valid_referers none blocked www.lizhiyu.xyz;if ($invalid_referer) {#或者永久重定向本網站首頁return 403;}#valid_referers: 指定資源訪問是通過以下幾種方式為合法,即白名單。#none:允許缺失的頭部訪問。#blocked:允許referer沒有對應值的請求。#server_names:若referer站點域名與server_name中本機配的域名一樣允許訪問。} }

Nginx可用性探測

Nginx可以配置一些策略,來實現反向代理,如果反向代理的節(jié)點不可用,Nginx則在固定間內不會再去訪問這個節(jié)點
這個固定時間和定義代理節(jié)點不可用配置如下

proxy_next_upstream說明
max_fails,fail_timeout,backup, down說明

upstream test {#配置的代理節(jié)點#此節(jié)點如果訪問max_fails次出錯則在fail_timeout時間內不再訪問#出現錯誤后過了fail_timeout會再次訪問#max_fails如果配置成0則代表不管出現多少次錯誤都會訪問server localhost:8080 max_fails=2 fail_timeout=60s;server localhost:8081 max_fails=2 fail_timeout=60s; } server{?location /api/ {proxy_pass http://test;#定義怎樣才算次服務器不可用#當請求連接后臺出錯、連接響應出錯、連接超時、http狀態(tài)碼#當fail_timeout時間內出現max_fails次此錯誤則代表此節(jié)點不可用proxy_next_upstream error timeout http_500 http_503 http_404;} }

Nginx搭建靜態(tài)資源服務器

將1.jpg文件放到/software/img目錄下
然后訪問https://www.lizhiyu.xyz/img/1.jpg即可下載圖片
配置文件如下

location /img {alias /software/img/;}

Nginx配置https服務

安裝依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
刪除安裝過的包,然后重新安裝nginx(因為需要安裝ssl模塊)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
配置文件如下

http {server {listen 80;server_name localhost;location / {# 配置http 永久重定向到httpsrewrite ^/(.*) https://www.lizhiyu.xyz/$1 permanent;}}# HTTPS serverserver {# https使用443端口,記得將開啟端口listen 443 ssl;# 配置域名server_name www.lizhiyu.xyz;#證書位置ssl_certificate /usr/local/https/5148543_www.lizhiyu.xyz.pem;#密鑰位置ssl_certificate_key /usr/local/https/5148543_www.lizhiyu.xyz.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;}} }

Nginx流量統(tǒng)計

access.log 日志記錄格式如下
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' $request_time;
access日志記錄格式選為為main
access_log logs/access.log main;
查看訪問最頻繁的前100個IP
awk '{print $1}' access.log | sort -n |uniq -c | sort -rn | head -n 100
統(tǒng)計訪問最多的url 前20名
cat access.log |awk '{print $7}'| sort|uniq -c| sort -rn| head -20 | more
統(tǒng)計耗時接口, 列出傳輸時間超過 2 秒的接口,顯示前5條
cat access.log|awk '($NF > 2){print $7}'|sort -n|uniq -c|sort -nr|head -5
?備注:$NF 表示最后一列, awk ‘{print $NF}’

Nginx黑白名單攔截

nginx.conf配置文件中配置如下
全局異常兜底數據返回

{#配置為引入黑名單配置include blacklist.conf;server {..} }

nginx.conf和blacklist.conf在同級目錄下,blacklist.conf配置如下

{#配置禁止訪問的ip(再次訪問會返回403狀態(tài)碼)deny 192.168.8.113;deny 106.121.65.84; }

配置完成后執(zhí)行./nginx.conf -s reload命令
執(zhí)行完這個命令master進程會通知 worker進程執(zhí)行完當前任務后關閉
master去加載完配置文件再創(chuàng)建新的 worker進程執(zhí)行任務

異常兜底返回

為了防止后端出現錯誤返回錯誤信息,在nginx層面對錯誤信息進行攔截
配置信息如下

server{location /img {alias /software/img/;#異常返回兜底開啟proxy_intercept_errors on;}#當后端返回狀態(tài)碼為 404 500 .... 會返回200的狀態(tài)碼error_page 404 500 502 503 504 =200 /default_api;#當出現錯誤碼就會訪問這個locationlocation = /default_api {#返回數據格式default_type application/json;#返回的數據信息return 200 '{"code":"-1","msg":"invoke fail, not found "}';} }

OpenResty的學習

由于Nginx是用c語言編寫,想要擴展Nginx就要書寫c語言(不易于擴展),使用OpenResty可以對Nginx通過lua腳本進行擴展,例如lua腳本操作redis等
介紹: OpenResty中自帶Nginx,同時提供了一些插件
安裝
使用
openResty會分為多個階段按順序處理每一個請求,主要包括如下階段
init_by_lua_file 初始化階段
init_worker_by_lua_file 初始化worker階段
rewrite_by_lua_file 重定向階段
access_by_lua_file 訪問控制階段
content_by_lua_file內容生成階段
簡單的使用

查看版本 resty -V (能夠查看到openresty版本 和 安裝過的模塊) 編輯:/usr/local/openresty/nginx/conf/nginx.conf http{#生產環(huán)境要開啟on,本地測試可以使用off達到時時刷新lua_code_cache off;# 虛擬機主機塊server{# 監(jiān)聽端口listen 80;# 配置請求的路由location /{#指定格式為htmldefault_type text/html;#content_by_lua_block是openResty中接收請求的一個階段#是生成內容的階段content_by_lua_block{#輸出一句話ngx.say("hello world");}}} }#使用其他方式 http{#設置lua擴展庫的位置 (位置就固定寫這個)lua_package_path "$prefix/lualib/?.lua;;";#設置c擴展庫的位置lua_package_cpath "$prefix/lualib/?.so;;";server{# 監(jiān)聽端口listen 80;# 配置請求的路由location /{#格式default_type text/html;#執(zhí)行l(wèi)earn.lua腳本content_by_lua_file lua/learn.lua;}} } #限速使用例子location /download {#access_by_lua_block 是openResty接收請求的一個階段#這個階段用來控制訪問access_by_lua_block {#limit_rate 是nginx中可以使用的變量(上面有說寫)、#在openResty中可以通過ngx.var.variable使用此變量ngx.var.limit_rate = "300K"}alias /usr/local/software/app;}#黑名單使用例子 location / {#訪問控制階段access_by_lua_file lua/blackList.lua;proxy_pass http://upstreamName; } blackList.lua內容如下 local black_ips = {["127.0.0.1"]=true} ? local ip = ngx.var.remote_addr if true == black_ips[ip] then#返回一個403狀態(tài)碼ngx.exit(ngx.HTTP_FORBIDDEN)return; end

停止
./nginx -s stop
啟動
./nginx -c /usr/local/openresty/nginx/conf/nginx.conf
重新加載
./nginx -s reload

LVS + KeepLived了解

總結

以上是生活随笔為你收集整理的志宇-Nginx学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。