最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx中nginx.conf配置結構示例詳解

 更新時間:2025年09月18日 11:01:00   作者:裁二尺秋風  
Nginx?是一款高性能的?Web?服務器和反向代理服務器,其靈活的配置語法和模塊化設計使其成為現(xiàn)代?Web?架構的核心組件,這篇文章主要介紹了Nginx中nginx.conf配置結構的相關資料,需要的朋友可以參考下

一、nginx.conf 配置結構

函數(shù)

說明

main

全局配置

event

配置工作模式以及連接數(shù)

http

http模塊相關配置

server

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

location

路由規(guī)則,表達式

upstream

集群、內(nèi)網(wǎng)服務器(負載均衡也在這里邊配)

二、Nginx配置語法

基本的語法:

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

$符號:$符號為nginx內(nèi)部提供的一些參數(shù)變量。

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

函數(shù)

說明

main

全局配置

event

配置工作模式以及連接數(shù)

http

http模塊相關配置

server

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

location

路由規(guī)則,表達式

upstream

集群、內(nèi)網(wǎng)服務器(負載均衡也在這里邊配)

主配置文件詳解

#user  nobody;                   #表示當系統(tǒng)在執(zhí)行worker進程的時候由哪個用戶去執(zhí)行,(默認為nobody)
worker_processes  10;            #邏輯CPU的個數(shù)設置的值為:(n-1)

 # nginx的日志級別:debug info notice warn error crit 等級逐漸升高。

#error_log  logs/error.log;      #錯誤的日志,在編譯的時候已經(jīng)設置相關的路徑。
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


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

2、 nginx日志路徑設置

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

 3、存放pid的地方

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

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

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

(三)、http相關網(wǎng)絡傳輸?shù)哪K(包含了很多的配置內(nèi)容)

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;
    ***參數(shù)注解區(qū)****
    # $remote_addr               客戶端的IP地址
    # $remote_user               用戶名稱,可以是 "-"
    # [$time_local]              訪問時間
    # $request                   請求的內(nèi)容包括:URL 請求的方法GET、POST
    # $status                    響應的狀態(tài)碼
    # $body_bytes_sent           客戶端發(fā)送的文件主體所包含內(nèi)容的字節(jié)數(shù)
    # $http_referer              記錄著用戶從哪個訪問鏈接跳轉(zhuǎn)過來的,我們在做日志分析的時候會用到。
    # $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;
    ***參數(shù)注解區(qū)****
    # $remote_addr               客戶端的IP地址
    # $remote_user               用戶名稱,可以是 "-"
    # [$time_local]              訪問時間
    # $request                   請求的內(nèi)容包括:URL 請求的方法GET、POST
    # $status                    響應的狀態(tài)碼
    # $body_bytes_sent           客戶端發(fā)送的文件主體所包含內(nèi)容的字節(jié)數(shù)
    # $http_referer              記錄著用戶從哪個訪問鏈接跳轉(zhuǎn)過來的,我們在做日志分析的時候會用到。
    # $http_user_agent           用戶的代理
    # $http_x_forwarded_for      可以記錄客戶端的IP

3.2、文件的高效傳輸

    sendfile        on;          #打開,表示文件傳輸?shù)男阅軙玫教嵘琻ginx的性能也得到相應的提升。
    #tcp_nopush     on;          #和sendfile一起使用,表示當我們的數(shù)據(jù)包累積到一定的大小之后再發(fā)送,可以提高傳輸?shù)男?。先取?shù)據(jù)在進行統(tǒng)一分發(fā)。

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

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

3.4、gzip壓縮

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

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

    server {
        listen       8080;                #服務端口號
        server_name  localhost;           #服務IP、域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                      #配置頁面顯示的路由: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;         #訪問錯誤的時候會返回相應的狀態(tài)值。
        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;               #指定的服務端口為8888
        server_name  127.0.0.1;          #指定的服務器的名稱是127.0.0.1

        location / {
            root   html;
            index  test.html index.htm;  #訪問到的內(nèi)容為test.html文件            
        }  

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

3.6、通過include函數(shù)的調(diào)用server模塊的配置,提高文件的可讀性。

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

可用統(tǒng)一將配置文件放在/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
}

總結 

到此這篇關于Nginx中nginx.conf配置結構的文章就介紹到這了,更多相關Nginx nginx.conf配置結構內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • nginx mirror 流量鏡像的具體使用

    nginx mirror 流量鏡像的具體使用

    流量鏡像可以將實時流量的副本發(fā)送給被鏡像的服務,本文主要介紹了nginx mirror 流量鏡像的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08
  • nginx常見內(nèi)置變量$uri和$request_uri的使用

    nginx常見內(nèi)置變量$uri和$request_uri的使用

    本文主要介紹了nginx常見內(nèi)置變量$uri和$request_uri的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-07-07
  • nginx配置代理多個前端資源

    nginx配置代理多個前端資源

    本文主要介紹了nginx配置代理多個前端資源,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • 解決nginx報錯信息 client intended to send too large body: 1331696 bytes

    解決nginx報錯信息 client intended to send too large body: 1331696

    這篇文章主要介紹了解決nginx報錯 client intended to send too large body: 1331696 bytes的相關資料,需要的朋友可以參考下
    2017-02-02
  • 如何利用nginx通過正則攔截指定url請求詳解

    如何利用nginx通過正則攔截指定url請求詳解

    這篇文章主要介紹了如何利用nginx通過正則攔截指定url請求的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用nginx具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2020-05-05
  • Nginx http升級到https的完整步驟

    Nginx http升級到https的完整步驟

    這篇文章主要給大家介紹了關于Nginx http升級到https的完整步驟,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Nginx具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • 解決Nginx 配置 proxy_pass 后 返回404問題

    解決Nginx 配置 proxy_pass 后 返回404問題

    這篇文章主要介紹了Nginx 配置 proxy_pass 后 返回404問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 修改nginx服務器類型實現(xiàn)簡單偽裝(隱藏nginx類型與版本等)

    修改nginx服務器類型實現(xiàn)簡單偽裝(隱藏nginx類型與版本等)

    這篇文章主要介紹了修改nginx服務器類型實現(xiàn)簡單偽裝(隱藏nginx類型與版本等),需要的朋友可以參考下
    2016-03-03
  • 關于Nginx動靜分離詳解以及配置

    關于Nginx動靜分離詳解以及配置

    這篇文章主要介紹了關于Nginx動靜分離詳解以及配置,動靜分離是通過中間件將動態(tài)請求和靜態(tài)請求進行分離,分離資源,減少不必要的請求消耗,減少請求延時,需要的朋友可以參考下
    2023-04-04
  • Nginx配置ssl證書(https)的全過程

    Nginx配置ssl證書(https)的全過程

    這篇文章主要介紹了Nginx配置ssl證書(https)的過程,在文中大家需要特別注意,如果有防火墻的話,記得開通443端口,本文給大家介紹的非常詳細,需要的朋友可以參考下
    2022-10-10

最新評論

丽水市| 武义县| 缙云县| 石柱| 柯坪县| 永兴县| 多伦县| 富裕县| 西吉县| 靖安县| 博客| 绥滨县| 开封市| 宜州市| 延吉市| 桐城市| 临朐县| 尚志市| 尖扎县| 曲麻莱县| 泸西县| 公安县| 桐城市| 苗栗市| 昌邑市| 隆化县| 龙岩市| 紫云| 饶阳县| 苏尼特右旗| 新源县| 贺兰县| 尉犁县| 通州区| 合阳县| 铁力市| 蓝山县| 北流市| 合水县| 偃师市| 托克托县|