首頁 運維 Nginx Nginx常用功能是什麼

Nginx常用功能是什麼

May 18, 2023 pm 10:31 PM
nginx

nginx常用功能

1、http代理,反向代理:作為web伺服器最常用的功能之一,尤其是反向代理。

這裡我給來2張圖,對正向代理與反響代理做個詮釋,具體細節,大家可以翻閱下資料。

Nginx常用功能是什麼

nginx在做反向代理時,提供效能穩定,並且能夠提供配置靈活的轉送功能。 nginx可以根據不同的正規匹配,採取不同的轉發策略,例如圖片檔案結尾的走文件伺服器,動態頁面走web伺服器,只要你正則寫的沒問題,又有相對應的伺服器解決方案,你就可以隨心所欲的玩。且nginx對回傳結果進行錯誤頁跳轉,異常判斷等。如果被分發的伺服器有異常,他可以將請求重新轉發給另外一台伺服器,然後自動移除異常伺服器。

2、負載平衡

nginx提供的負載平衡策略有2種:內建策略和擴充策略。內建策略為輪詢,加權輪詢,ip hash。擴展策略,就天馬行空,只有你想不到的沒有他做不到的啦,你可以參考所有的負載平衡演算法,給他一一找出來做下實現。

上3個圖,理解這三種負載平衡演算法的實作

Nginx常用功能是什麼

ip hash演算法,對客戶端請求的ip進行hash操作,然後根據hash結果將同一個客戶端ip的請求分發給同一台伺服器處理,可以解決session不共享的問題。

Nginx常用功能是什麼

3、web快取

nginx可以對不同的檔案做不同的快取處理,配置靈活,並且支援fastcgi_cache,主要用於對fastcgi的動態程式進行快取。配合第三方的ngx_cache_purge,對制定的url快取內容可以的進行增刪管理。

4、nginx相關位址

原始碼:

官網:

nginx設定檔結構

##如果你下載好啦,你的安裝文件,不妨打開conf資料夾的nginx.conf文件,nginx伺服器的基礎配置,預設的配置也存放在此。

在nginx.conf的註解符號位元

#nginx檔案的結構,這個對剛入門的同學,可以多看兩眼。

預設的config

#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;
 # }
 #}
}
登入後複製

nginx檔案結構

...    #全局块
events {   #events块
 ...
}
http  #http块
{
 ... #http全局块
 server  #server块
 { 
  ...  #server全局块
  location [pattern] #location块
  {
   ...
  }
  location [pattern] 
  {
   ...
  }
 }
 server
 {
  ...
 }
 ...  #http全局块
}
登入後複製

1、全域區塊:設定影響nginx全域的指令。一般有運行nginx伺服器的使用者群組,nginx進程pid存放路徑,日誌存放路徑,設定檔引入,允許產生worker process數等。

2、events區塊:配置影響nginx伺服器或與使用者的網路連線。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啟多個網路連接序列化等。

3、http區塊:可以嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模組的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。

4、server區塊:配置虛擬主機的相關參數,一個http中可以有多個server。

5、location區塊:設定請求的路由,以及各種頁面的處理情況。

下面給大家上一個設定文件,作為理解,同時也配入我搭建的一台測試機中,給大家範例。

########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
 accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
 multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
 #use epoll;  #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
 worker_connections 1024; #最大连接数,默认为512
}
http {
 include  mime.types; #文件扩展名与文件类型映射表
 default_type application/octet-stream; #默认文件类型,默认为text/plain
 #access_log off; #取消服务日志 
 log_format myformat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
 access_log log/access.log myformat; #combined为日志格式的默认值
 sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
 sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
 keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
 upstream mysvr { 
  server 127.0.0.1:7878;
  server 192.168.10.121:3333 backup; #热备
 }
 error_page 404 https://www.baidu.com; #错误页
 server {
  keepalive_requests 120; #单连接请求上限次数。
  listen  4545; #监听端口
  server_name 127.0.0.1; #监听地址  
  location ~*^.+$ {  #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
   #root path; #根目录
   #index vv.txt; #设置默认页
   proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
   deny 127.0.0.1; #拒绝的ip
   allow 172.18.5.54; #允许的ip   
  } 
 }
}
登入後複製

上面是nginx的基本配置,需要注意的有以下幾點:

1、1.$remote_addr 與$http_x_forwarded_for 用來記錄客戶端的ip位址;2.$remote_user :用來記錄客戶端使用者名稱;3.$time_local : 用來記錄存取時間與時區;4.$request : 用來記錄請求的url與http協定;

  5. $status : 用來記錄請求狀態;成功是200, 6.$body_bytes_s ent :記錄發送給客戶端文件主體內容大小;7.$http_referer :用來記錄從那個頁面鏈接訪問過來的; 8.$http_user_agent :記錄客戶端瀏覽器的相關資訊;

2、驚群現象:一個網路連接到來,多個睡眠的進程被同事叫醒,但只有一個進程能獲得鏈接,這樣會影響系統性能。

3、每個指令必須有分號結束。

以上是Nginx常用功能是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

nginx403怎麼解決 nginx403怎麼解決 Apr 14, 2025 am 10:33 AM

如何解決 Nginx 403 Forbidden 錯誤?檢查文件或目錄權限;2. 檢查 .htaccess 文件;3. 檢查 Nginx 配置文件;4. 重啟 Nginx。其他可能原因還包括防火牆規則、SELinux 設置或應用程序問題。

nginx403錯誤怎麼解決 nginx403錯誤怎麼解決 Apr 14, 2025 pm 12:54 PM

服務器無權訪問所請求的資源,導致 nginx 403 錯誤。解決方法包括:檢查文件權限。檢查 .htaccess 配置。檢查 nginx 配置。配置 SELinux 權限。檢查防火牆規則。排除其他原因,如瀏覽器問題、服務器故障或其他可能的錯誤。

linux怎麼啟動nginx linux怎麼啟動nginx Apr 14, 2025 pm 12:51 PM

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

nginx304錯誤怎麼解決 nginx304錯誤怎麼解決 Apr 14, 2025 pm 12:45 PM

問題的答案:304 Not Modified 錯誤表示瀏覽器已緩存客戶端請求的最新資源版本。解決方案:1. 清除瀏覽器緩存;2. 禁用瀏覽器緩存;3. 配置 Nginx 允許客戶端緩存;4. 檢查文件權限;5. 檢查文件哈希;6. 禁用 CDN 或反向代理緩存;7. 重啟 Nginx。

nginx在windows中怎麼配置 nginx在windows中怎麼配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

怎麼查看nginx是否啟動 怎麼查看nginx是否啟動 Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎麼查看運行狀態 nginx怎麼查看運行狀態 Apr 14, 2025 am 11:48 AM

查看 Nginx 運行狀態的方法有:使用 ps 命令查看進程狀態;查看 Nginx 配置文件 /etc/nginx/nginx.conf;使用 Nginx 狀態模塊啟用狀態端點;使用 Prometheus、Zabbix 或 Nagios 等監控工具。

nginx如何配置負載均衡 nginx如何配置負載均衡 Apr 14, 2025 am 08:33 AM

如何配置 Nginx 進行負載均衡?定義上游服務器池,指定服務器 IP 和端口。定義虛擬主機,監聽連接並轉發到上游池。指定位置,匹配請求並轉發到上游池。

See all articles