Nginx中nginx.conf配置結(jié)構(gòu)示例詳解
一、nginx.conf 配置結(jié)構(gòu)

函數(shù) | 說明 |
main | 全局配置 |
event | 配置工作模式以及連接數(shù) |
http | http模塊相關(guān)配置 |
server | 虛擬主機(jī)配置,可以有多個(gè) |
location | 路由規(guī)則,表達(dá)式 |
upstream | 集群、內(nèi)網(wǎng)服務(wù)器(負(fù)載均衡也在這里邊配) |
二、Nginx配置語法

基本的語法:
指令集組成:每個(gè)指令單獨(dú)寫一行,每個(gè)指令分號(hào) ";" 分開,每個(gè)指令塊用大括號(hào) "{ ... }" 分開,大括號(hào)的后方?jīng)]有分號(hào)。注釋用#號(hào)分開。

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

三、nginx.conf 核心配置文件詳解
函數(shù) | 說明 |
main | 全局配置 |
event | 配置工作模式以及連接數(shù) |
http | http模塊相關(guān)配置 |
server | 虛擬主機(jī)配置,可以有多個(gè) |
location | 路由規(guī)則,表達(dá)式 |
upstream | 集群、內(nèi)網(wǎng)服務(wù)器(負(fù)載均衡也在這里邊配) |
主配置文件詳解
#user nobody; #表示當(dāng)系統(tǒng)在執(zhí)行worker進(jìn)程的時(shí)候由哪個(gè)用戶去執(zhí)行,(默認(rèn)為nobody)
worker_processes 10; #邏輯CPU的個(gè)數(shù)設(shè)置的值為:(n-1)
# nginx的日志級(jí)別:debug info notice warn error crit 等級(jí)逐漸升高。
#error_log logs/error.log; #錯(cuò)誤的日志,在編譯的時(shí)候已經(jīng)設(shè)置相關(guān)的路徑。
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#默認(rèn)使用epoll
use epoll;
#每個(gè)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、進(jìn)程用戶設(shè)置
user root; worker_processes 10; worker_rlimit_nofile 65535;
user root;
- 這一配置項(xiàng)指定了 Nginx 工作進(jìn)程所使用的用戶身份。
root是系統(tǒng)中的超級(jí)用戶,擁有最高權(quán)限。不過,從安全角度考慮,不建議讓 Nginx 以root用戶身份運(yùn)行,因?yàn)檫@會(huì)使 Nginx 擁有過高的權(quán)限,一旦出現(xiàn)安全漏洞,攻擊者可能會(huì)獲取系統(tǒng)的最高控制權(quán)。通常,建議創(chuàng)建一個(gè)專門的低權(quán)限用戶來運(yùn)行 Nginx。worker_processes 4;
- 此配置項(xiàng)用于設(shè)置 Nginx 工作進(jìn)程的數(shù)量。Nginx 采用多進(jìn)程模型,一個(gè)主進(jìn)程(master process)負(fù)責(zé)管理多個(gè)工作進(jìn)程(worker processes),工作進(jìn)程負(fù)責(zé)處理實(shí)際的客戶端請(qǐng)求。
4代表創(chuàng)建 4 個(gè)工作進(jìn)程。一般而言,可以根據(jù)服務(wù)器的 CPU 核心數(shù)來設(shè)置該值,通常設(shè)置為 CPU 核心數(shù)或者核心數(shù)的兩倍,這樣能充分利用服務(wù)器的 CPU 資源。worker_rlimit_nofile 65535;
- 該配置項(xiàng)設(shè)定了每個(gè) Nginx 工作進(jìn)程能夠打開的最大文件描述符數(shù)量。在 Linux 系統(tǒng)里,一切皆文件,包括網(wǎng)絡(luò)連接、磁盤文件等,每個(gè)打開的文件或者連接都會(huì)占用一個(gè)文件描述符。
65535意味著每個(gè)工作進(jìn)程最多可以同時(shí)打開 65535 個(gè)文件描述符。當(dāng)服務(wù)器需要處理大量并發(fā)連接時(shí),就需要增大這個(gè)值,防止出現(xiàn) “too many open files” 的錯(cuò)誤。



2、 nginx日志路徑設(shè)置
#error_log logs/error.log; #錯(cuò)誤的日志,在編譯的時(shí)候已經(jīng)設(shè)置相關(guān)的路徑放:/var/log/nginx/ #error_log logs/error.log notice; #error_log logs/error.log info;

3、存放pid的地方
#pid logs/nginx.pid; #進(jìn)程號(hào)存在的路徑,在編譯的時(shí)候已經(jīng)設(shè)置相關(guān)的路徑放:/var/run/nginx/

(二)、events配置工作模式以及連接數(shù)
events {
#默認(rèn)使用epoll
use epoll;
#每個(gè)worker允許客端連接的最大連接數(shù),根據(jù)硬件的配置來選值的大小。
worker_connections 1024;
}
(三)、http相關(guān)網(wǎng)絡(luò)傳輸?shù)哪K(包含了很多的配置內(nèi)容)
http {
include mime.types; #導(dǎo)入外部的文件,文件中為指令塊,當(dāng)前目錄的mime.types文件。
default_type application/octet-stream; #默認(rèn)的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] 訪問時(shí)間
# $request 請(qǐng)求的內(nèi)容包括:URL 請(qǐng)求的方法GET、POST
# $status 響應(yīng)的狀態(tài)碼
# $body_bytes_sent 客戶端發(fā)送的文件主體所包含內(nèi)容的字節(jié)數(shù)
# $http_referer 記錄著用戶從哪個(gè)訪問鏈接跳轉(zhuǎn)過來的,我們?cè)谧鋈罩痉治龅臅r(shí)候會(huì)用到。
# $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] 訪問時(shí)間
# $request 請(qǐng)求的內(nèi)容包括:URL 請(qǐng)求的方法GET、POST
# $status 響應(yīng)的狀態(tài)碼
# $body_bytes_sent 客戶端發(fā)送的文件主體所包含內(nèi)容的字節(jié)數(shù)
# $http_referer 記錄著用戶從哪個(gè)訪問鏈接跳轉(zhuǎn)過來的,我們?cè)谧鋈罩痉治龅臅r(shí)候會(huì)用到。
# $http_user_agent 用戶的代理
# $http_x_forwarded_for 可以記錄客戶端的IP
3.2、文件的高效傳輸
sendfile on; #打開,表示文件傳輸?shù)男阅軙?huì)得到提升,nginx的性能也得到相應(yīng)的提升。
#tcp_nopush on; #和sendfile一起使用,表示當(dāng)我們的數(shù)據(jù)包累積到一定的大小之后再發(fā)送,可以提高傳輸?shù)男?。先取?shù)據(jù)在進(jìn)行統(tǒng)一分發(fā)。3.3、客戶端連接服務(wù)器的超時(shí)時(shí)間(傳輸完成后保持的時(shí)間)
keepalive_timeout 65; #以秒為單位,http有keepalive機(jī)制,當(dāng)數(shù)據(jù)傳輸完成之后會(huì)保持一定時(shí)間的連接處于打開狀態(tài),如果客戶端有新的請(qǐng)求會(huì)用此連接去處理。不用創(chuàng)建新的連接,節(jié)省資源的開銷。
3.4、gzip壓縮
#gzip on; #內(nèi)容的傳輸經(jīng)過壓縮之后體積變小,提升的傳輸速率,減少了帶寬的產(chǎn)生,但是在壓縮的過程中會(huì)消耗我們系統(tǒng)上CPU的性能。
3.5、server模塊,虛擬主機(jī)相關(guān)配置
server {
listen 8080; #服務(wù)端口號(hào)
server_name localhost; #服務(wù)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; #訪問錯(cuò)誤的時(shí)候會(huì)返回相應(yīng)的狀態(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; #指定的服務(wù)端口為8888
server_name 127.0.0.1; #指定的服務(wù)器的名稱是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相關(guān)的配置放在:/usr/local/nginx/conf/conf.d/http 目錄下
- TCP相關(guān)的配置放在:/usr/local/nginx/conf/conf.d/tcp 目錄下
user root;
worker_processes 4;
worker_rlimit_nofile 65535;
events {
...
}
include conf.d/tcp/*.conf; #TCP相關(guān)配置(不能放在下邊的HTTP模塊中不然會(huì)報(bào)錯(cuò));這里的include是指包含/usr/local/nginx/conf/conf.d/tcp路徑下所有的.conf。
http {
...
include conf.d/http/*.conf; #HTTP相關(guān)配置(需要放在HTTP模塊中不然會(huì)報(bào)錯(cuò));這里的include是指包含/usr/local/nginx/conf/conf.d/http 路徑下所有的.conf
}

總結(jié)
到此這篇關(guān)于Nginx中nginx.conf配置結(jié)構(gòu)的文章就介紹到這了,更多相關(guān)Nginx nginx.conf配置結(jié)構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx常見內(nèi)置變量$uri和$request_uri的使用
本文主要介紹了nginx常見內(nèi)置變量$uri和$request_uri的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
解決nginx報(bào)錯(cuò)信息 client intended to send too large body: 1331696
這篇文章主要介紹了解決nginx報(bào)錯(cuò) client intended to send too large body: 1331696 bytes的相關(guān)資料,需要的朋友可以參考下2017-02-02
如何利用nginx通過正則攔截指定url請(qǐng)求詳解
這篇文章主要介紹了如何利用nginx通過正則攔截指定url請(qǐng)求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用nginx具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
解決Nginx 配置 proxy_pass 后 返回404問題
這篇文章主要介紹了Nginx 配置 proxy_pass 后 返回404問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
修改nginx服務(wù)器類型實(shí)現(xiàn)簡(jiǎn)單偽裝(隱藏nginx類型與版本等)
這篇文章主要介紹了修改nginx服務(wù)器類型實(shí)現(xiàn)簡(jiǎn)單偽裝(隱藏nginx類型與版本等),需要的朋友可以參考下2016-03-03

