Nginx—nginx.conf 配置結構詳解

一、nginx.conf 配置結構

函數

說明

main

全局配置

event

配置工作模式以及連接數

http

http模塊相關配置

server

虛擬主機配置,可以有多個

location

路由規則,表達式

upstream

集群、內網服務器(負載均衡也在這里邊配)

二、Nginx配置語法

基本的語法:

指令集組成:每個指令單獨寫一行,每個指令分號 ";" 分開,每個指令塊用大括號 "{ ... }" 分開,大括號的后方沒有分號。注釋用#號分開。

$符號:$符號為nginx內部提供的一些參數變量。

三、nginx.conf 核心配置文件詳解

?

函數

說明

main

全局配置

event

配置工作模式以及連接數

http

http模塊相關配置

server

虛擬主機配置,可以有多個

location

路由規則,表達式

upstream

集群、內網服務器(負載均衡也在這里邊配)

?

主配置文件詳解

#user  nobody;                   #表示當系統在執行worker進程的時候由哪個用戶去執行,(默認為nobody)
worker_processes  10;            #邏輯CPU的個數設置的值為:(n-1)# nginx的日志級別:debug info notice warn error crit 等級逐漸升高。#error_log  logs/error.log;      #錯誤的日志,在編譯的時候已經設置相關的路徑。
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {#默認使用epolluse epoll;#每個worker允許的客端最大連接數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       8080;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;#    }#}}

(一)main 全局配置模塊

1、進程用戶設置

user root;
worker_processes 10;
worker_rlimit_nofile 65535;

?

  1. user root;
    • 這一配置項指定了 Nginx 工作進程所使用的用戶身份。root?是系統中的超級用戶,擁有最高權限。不過,從安全角度考慮,不建議讓 Nginx 以?root?用戶身份運行,因為這會使 Nginx 擁有過高的權限,一旦出現安全漏洞,攻擊者可能會獲取系統的最高控制權。通常,建議創建一個專門的低權限用戶來運行 Nginx。
  2. worker_processes 4;
    • 此配置項用于設置 Nginx 工作進程的數量。Nginx 采用多進程模型,一個主進程(master process)負責管理多個工作進程(worker processes),工作進程負責處理實際的客戶端請求。4?代表創建 4 個工作進程。一般而言,可以根據服務器的 CPU 核心數來設置該值,通常設置為 CPU 核心數或者核心數的兩倍,這樣能充分利用服務器的 CPU 資源。
  3. worker_rlimit_nofile 65535;
    • 該配置項設定了每個 Nginx 工作進程能夠打開的最大文件描述符數量。在 Linux 系統里,一切皆文件,包括網絡連接、磁盤文件等,每個打開的文件或者連接都會占用一個文件描述符。65535?意味著每個工作進程最多可以同時打開 65535 個文件描述符。當服務器需要處理大量并發連接時,就需要增大這個值,防止出現 “too many open files” 的錯誤。

2、?nginx日志路徑設置

#error_log  logs/error.log;      #錯誤的日志,在編譯的時候已經設置相關的路徑放:/var/log/nginx/
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

?3、存放pid的地方

#pid        logs/nginx.pid;     #進程號存在的路徑,在編譯的時候已經設置相關的路徑放:/var/run/nginx/

?(二)、events配置工作模式以及連接數

events {#默認使用epolluse epoll;#每個worker允許客端連接的最大連接數,根據硬件的配置來選值的大小。worker_connections  1024;
}

(三)、http相關網絡傳輸的模塊(包含了很多的配置內容)

http {include       mime.types;   #導入外部的文件,文件中為指令塊,當前目錄的mime.types文件。default_type  application/octet-stream;    #默認的type類型。*********************************************日志模塊分析**********************************************************#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  #access_log 日志的格式,可以自定義格式。#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;***參數注解區****# $remote_addr               客戶端的IP地址# $remote_user               用戶名稱,可以是 "-"# [$time_local]              訪問時間# $request                   請求的內容包括:URL 請求的方法GET、POST# $status                    響應的狀態碼# $body_bytes_sent           客戶端發送的文件主體所包含內容的字節數# $http_referer              記錄著用戶從哪個訪問鏈接跳轉過來的,我們在做日志分析的時候會用到。# $http_user_agent           用戶的代理# $http_x_forwarded_for      可以記錄客戶端的IP**********************************************************************************************************************************sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       8080;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;#    }#}}

mime.types文件

3.1、日志格式

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  #access_log 日志的格式,可以自定義格式。#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;***參數注解區****# $remote_addr               客戶端的IP地址# $remote_user               用戶名稱,可以是 "-"# [$time_local]              訪問時間# $request                   請求的內容包括:URL 請求的方法GET、POST# $status                    響應的狀態碼# $body_bytes_sent           客戶端發送的文件主體所包含內容的字節數# $http_referer              記錄著用戶從哪個訪問鏈接跳轉過來的,我們在做日志分析的時候會用到。# $http_user_agent           用戶的代理# $http_x_forwarded_for      可以記錄客戶端的IP

3.2、文件的高效傳輸

    sendfile        on;          #打開,表示文件傳輸的性能會得到提升,nginx的性能也得到相應的提升。#tcp_nopush     on;          #和sendfile一起使用,表示當我們的數據包累積到一定的大小之后再發送,可以提高傳輸的效率。先取數據在進行統一分發。

3.3、客戶端連接服務器的超時時間(傳輸完成后保持的時間)

   keepalive_timeout  65;            #以秒為單位,http有keepalive機制,當數據傳輸完成之后會保持一定時間的連接處于打開狀態,如果客戶端有新的請求會用此連接去處理。不用創建新的連接,節省資源的開銷。

3.4、gzip壓縮

#gzip  on;      #內容的傳輸經過壓縮之后體積變小,提升的傳輸速率,減少了帶寬的產生,但是在壓縮的過程中會消耗我們系統上CPU的性能。

3.5、server模塊,虛擬主機相關配置

    server {listen       8080;                #服務端口號server_name  localhost;           #服務IP、域名#charset koi8-r;#access_log  logs/host.access.log  main;location / {                      #配置頁面顯示的路由:locationroot   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;#    }#}}

3.5.1?在nginx.conf文件中添加新的server模塊。
    server {listen       8888;               #指定的服務端口為8888server_name  127.0.0.1;          #指定的服務器的名稱是127.0.0.1location / {root   html;index  test.html index.htm;  #訪問到的內容為test.html文件            }  

3.5.2 添加test.html文件:/usr/local/nginx/html/test.html?

?

?

3.6、通過include函數的調用server模塊的配置,提高文件的可讀性。

3.6.1?在nginx.conf文件中定義include調用server模塊(支持正則匹配)

可用統一將配置文件放在/usr/local/nginx/conf/conf.d指定的路徑下這樣方便管理,如:

  • HTTP相關的配置放在:/usr/local/nginx/conf/conf.d/http 目錄下
  • TCP相關的配置放在:/usr/local/nginx/conf/conf.d/tcp 目錄下
user root;
worker_processes 4;
worker_rlimit_nofile 65535;events {...
}include conf.d/tcp/*.conf;  #TCP相關配置(不能放在下邊的HTTP模塊中不然會報錯);這里的include是指包含/usr/local/nginx/conf/conf.d/tcp路徑下所有的.conf。http {...include conf.d/http/*.conf; #HTTP相關配置(需要放在HTTP模塊中不然會報錯);這里的include是指包含/usr/local/nginx/conf/conf.d/http 路徑下所有的.conf
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/74019.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/74019.shtml
英文地址,請注明出處:http://en.pswp.cn/web/74019.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

斐波那契數列----C語言

關于斐波那契 已知: 問題背景:一對兔子從第3個月開始每月生一對新兔子,新兔子同樣在第3個月開始繁殖。 關鍵觀察: 第1個月:1對(初始兔子)。 第2個月:1對(未成熟&#…

vulhub靶場—— Tomcat8

目錄 一、漏洞描述 二、靶場搭建 三、漏洞復現 1、弱密碼 2、文件上傳 一、漏洞描述 環境描述: Tomcat 支持后臺部署 war 文件,可以直接將 webshell 部署到 web 目錄下。tomcat 默認的管理頁面 manager 使用 basic 認證用戶名和密碼登錄&#xff0…

使用 Spring AI Aliabab Module RAG 構建 Web Search 應用

使用 Spring AI Alibaba 構建大模型聯網搜索應用 Spring AI 實現了模塊化 RAG 架構,架構的靈感來自于論文“模塊化 RAG:將 RAG 系統轉變為類似樂高的可重構框架”中詳述的模塊化概念。 Spring AI 模塊化 RAG 體系 總體上分為以下幾個步驟: …

一些練習 C 語言的小游戲

一些練習 C 語言的小游戲 — 1. 猜數字游戲 描述:程序隨機生成一個數字,玩家需要猜測這個數字,并根據提示(太高或太低)調整猜測,直到猜中為止。 功能點: 隨機數生成 (rand() 函數)。循環和…

關于中文編程的一些思考

隨著信息化與數字化的發展,工業4.0時代亦將徐徐到來。當計算機的普及程度越來越高,數據的產生、傳輸、處理等變得越來越快、越來越大量的時候,人們想要自動化辦公的愿望也越來越強烈,希望能將自身從耗費腦力但是重復繁瑣的工作中解…

golang 日志log與logrus

目錄 一、Go 標準庫 log 詳解 1. 功能特點 2. 常用函數 3. 示例代碼 4. 優勢和局限 二、第三方庫 logrus 詳解 1. 功能特點 2. 核心功能 3. 示例代碼 4. 優勢和擴展性 三、總結 1. 何時選擇 log? 2. 何時選擇 logrus? 3. 對比總結 一、Go 標…

消費品行業創新創業中品類創新與數字化工具的融合:以開源 AI 智能客服、AI 智能名片及 S2B2C 商城小程序為例

摘要: 本文聚焦于消費品行業的創新與創業,深入探討“選擇大于努力”這一觀點,強調品類選擇在品牌發展中的關鍵作用。同時,詳細分析了品類創新對于新消費品牌崛起以及傳統品牌轉型的重要意義。在此基礎上,引入開源 AI 智…

Razer macOS v0.4.10快速安裝

鏈接點這里下載最新的 .dmg 文件。將下載的 .dmg 映像文件拖入 應用程序 文件夾中。若首次打開時出現安全警告【什么扔到廢紙簍】,這時候點擊 Mac 的“系統偏好設置”-> “安全性與隱私”-> “通用”,然后點擊底部的 “打開”。【或者仍然打開】 對…

Flask項目部署:Flask + uWSGI + Nginx

目錄 1,網絡架構 2,環境安裝 2.1,安裝yum:Shell軟件包管理器 2.2 安裝python 2.3 安裝uWSGI 2.4 安裝Flask 3,上傳工程包到服務器,打包Flask項目 4,創建和配置 uwsgi 配置文件 uwsgi.ini 4.1配置文件 4.2配置文件注釋詳解 5,啟動服務 6,安裝nginx 7,nginx配置 8,…

[FPGA基礎學習]實現流水燈與按鍵暫停

FPGA實現LED流水燈 1.vscode的安裝和使用 vscode下載 Visual Studio Code - Code Editing. Redefined vscode插件(Verilog-HDL/SystemVerilog)下載 quartus綁定vscode 2.用6個LED完成周期為1秒的跑馬燈效果 流水燈模塊設計 時鐘輸入 DE2-115開發板…

【TensorRT】TensorRT從安裝到推理——Python 環境下 MobileNetV4 三分類任務

我想開發一個基于深度學習的分類小軟件,逐漸了解到了TensorRT在模型推理速度上的優勢,經過一下午資料的查找實現了將onnx模型轉為TensorRT格式模型的推理及測試過程。將實現過程記錄下來方便日后查看。 本文實驗設備是MX350顯卡 2G顯存 一 、安裝Tenso…

1.兩數之和(Java)

1. 題目描述 LeetCode 1. 兩數之和(Two Sum) 給定一個整數數組 nums 和一個目標值 target,請你在該數組中找出和為目標值的那兩個整數,并返回它們的索引。 示例 1: 輸入:nums [2,7,11,15], target 9 …

《深入探索 Python 數據分析:用 Pandas 高效處理與可視化大型數據集》

《深入探索 Python 數據分析:用 Pandas 高效處理與可視化大型數據集》 引言:從零到分析高手 數據是當代社會最寶貴的資源,而數據分析技能是現代職業人不可或缺的一部分。在數據科學的領域中,Python 已成為當之無愧的“首選語言”,其強大的生態系統和簡潔的語法讓人如虎添…

將樹莓派5當做Ollama服務器,C#調用generate的API的示例

其實完全沒這個必要,性能用腳后跟想都會很差。但基于上一篇文章的成果,來都來了就先簡單試試吧。 先來看看這個拼夕夕上五百多塊錢能達到的效果: 只要對速度沒要求,那感覺就還行。 Ollama默認只在本地回環(127.0.0…

python基礎學習二(列表及字典的使用)

文章目錄 列表列表的創建獲取列表中的多個元素判斷列表中元素是否存在列表元素的添加操作列表元素的刪除操作列表元素的修改列表的排序列表生成式 字典字典的創建字典的常規操作字典的常用操作字典的視圖操作字典元素的遍歷字典的特點字典的生成式 列表 一個對象由id&#xff0…

Android設計模式之代理模式

一、定義: 為其他對象提供一種代理以控制對這個對象的訪問。 二、角色組成: Subject抽象主題:聲明真是主題與代理的共同接口方法,可以是一個抽象類或接口。 RealSubject真實主題:定義了代理表示的真實對象&#xff0c…

國外計算機證書推薦(考證)(6 Sigma、AWS、APICS、IIA、Microsoft、Oracle、PMI、Red Hat)

文章目錄 證書推薦1. 六西格瑪 (6 Sigma)2. 亞馬遜網絡服務 (AWS)3. 美國生產與庫存控制學會 (APICS)4. 內部審計師協會 (IIA)5. 微軟 (Microsoft)6. 甲骨文 (Oracle)7. 項目管理協會 (PMI)8. 紅帽 (Red Hat) 證書推薦 1. 六西格瑪 (6 Sigma) 介紹:六西格瑪是一種…

用mkdocs寫文檔#自動更新github-page

https://wuyisheng.github.io/blog 背景是上一篇博客 使用mkdocs,最后提及可以部署github page。這里說明下怎么自動部署。 當然,這篇博客主要的目的還是提供下github page的鏈接 :) 我是這樣做的: step 1: pip3 i…

QT五 文件系統,QFile,QfileInfo

總覽 QIODevice:所有 I/O 設備類的父類,提供了字節塊讀寫的通用操作以及基本接口;QFileDevice:Qt5新增加的類,提供了有關文件操作的通用實現。QFlie:訪問本地文件或者嵌入資源;QTemporaryFile&a…

EF Core表達式樹

文章目錄 前言一、表達式樹與委托的區別二、動態構建表達式樹示例1示例2示例3高級技巧:表達式合并 三、ExpressionTreeToString安裝方法基本用法支持的格式化風格 四、注意事項總結 前言 在 Entity Framework Core 中,表達式樹(Expression T…