Nginx中nginx.conf配置結(jié)構(gòu)示例詳解
一、nginx.conf 配置結(jié)構(gòu)
函數(shù) | 說明 |
main | 全局配置 |
event | 配置工作模式以及連接數(shù) |
http | http模塊相關(guān)配置 |
server | 虛擬主機(jī)配置,可以有多個 |
location | 路由規(guī)則,表達(dá)式 |
upstream | 集群、內(nèi)網(wǎng)服務(wù)器(負(fù)載均衡也在這里邊配) |
二、Nginx配置語法
基本的語法:
指令集組成:每個指令單獨(dú)寫一行,每個指令分號 ";" 分開,每個指令塊用大括號 "{ ... }" 分開,大括號的后方?jīng)]有分號。注釋用#號分開。
$符號:$符號為nginx內(nèi)部提供的一些參數(shù)變量。
三、nginx.conf 核心配置文件詳解
函數(shù) | 說明 |
main | 全局配置 |
event | 配置工作模式以及連接數(shù) |
http | http模塊相關(guān)配置 |
server | 虛擬主機(jī)配置,可以有多個 |
location | 路由規(guī)則,表達(dá)式 |
upstream | 集群、內(nèi)網(wǎng)服務(wù)器(負(fù)載均衡也在這里邊配) |
主配置文件詳解
#user nobody; #表示當(dāng)系統(tǒng)在執(zhí)行worker進(jìn)程的時候由哪個用戶去執(zhí)行,(默認(rèn)為nobody) worker_processes 10; #邏輯CPU的個數(shù)設(shè)置的值為:(n-1) # nginx的日志級別:debug info notice warn error crit 等級逐漸升高。 #error_log logs/error.log; #錯誤的日志,在編譯的時候已經(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; #每個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)中的超級用戶,擁有最高權(quán)限。不過,從安全角度考慮,不建議讓 Nginx 以root
用戶身份運(yùn)行,因?yàn)檫@會使 Nginx 擁有過高的權(quán)限,一旦出現(xiàn)安全漏洞,攻擊者可能會獲取系統(tǒng)的最高控制權(quán)。通常,建議創(chuàng)建一個專門的低權(quán)限用戶來運(yùn)行 Nginx。worker_processes 4;
- 此配置項(xiàng)用于設(shè)置 Nginx 工作進(jìn)程的數(shù)量。Nginx 采用多進(jìn)程模型,一個主進(jìn)程(master process)負(fù)責(zé)管理多個工作進(jìn)程(worker processes),工作進(jìn)程負(fù)責(zé)處理實(shí)際的客戶端請求。
4
代表創(chuàng)建 4 個工作進(jìn)程。一般而言,可以根據(jù)服務(wù)器的 CPU 核心數(shù)來設(shè)置該值,通常設(shè)置為 CPU 核心數(shù)或者核心數(shù)的兩倍,這樣能充分利用服務(wù)器的 CPU 資源。worker_rlimit_nofile 65535;
- 該配置項(xiàng)設(shè)定了每個 Nginx 工作進(jìn)程能夠打開的最大文件描述符數(shù)量。在 Linux 系統(tǒng)里,一切皆文件,包括網(wǎng)絡(luò)連接、磁盤文件等,每個打開的文件或者連接都會占用一個文件描述符。
65535
意味著每個工作進(jìn)程最多可以同時打開 65535 個文件描述符。當(dāng)服務(wù)器需要處理大量并發(fā)連接時,就需要增大這個值,防止出現(xiàn) “too many open files” 的錯誤。
2、 nginx日志路徑設(shè)置
#error_log logs/error.log; #錯誤的日志,在編譯的時候已經(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)程號存在的路徑,在編譯的時候已經(jīng)設(shè)置相關(guān)的路徑放:/var/run/nginx/
(二)、events配置工作模式以及連接數(shù)
events { #默認(rèn)使用epoll use epoll; #每個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] 訪問時間 # $request 請求的內(nèi)容包括:URL 請求的方法GET、POST # $status 響應(yīng)的狀態(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 響應(yīng)的狀態(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ù)男阅軙玫教嵘?,nginx的性能也得到相應(yīng)的提升。 #tcp_nopush on; #和sendfile一起使用,表示當(dāng)我們的數(shù)據(jù)包累積到一定的大小之后再發(fā)送,可以提高傳輸?shù)男?。先取?shù)據(jù)在進(jìn)行統(tǒng)一分發(fā)。
3.3、客戶端連接服務(wù)器的超時時間(傳輸完成后保持的時間)
keepalive_timeout 65; #以秒為單位,http有keepalive機(jī)制,當(dāng)數(shù)據(jù)傳輸完成之后會保持一定時間的連接處于打開狀態(tài),如果客戶端有新的請求會用此連接去處理。不用創(chuàng)建新的連接,節(jié)省資源的開銷。
3.4、gzip壓縮
#gzip on; #內(nèi)容的傳輸經(jīng)過壓縮之后體積變小,提升的傳輸速率,減少了帶寬的產(chǎn)生,但是在壓縮的過程中會消耗我們系統(tǒng)上CPU的性能。
3.5、server模塊,虛擬主機(jī)相關(guān)配置
server { listen 8080; #服務(wù)端口號 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; #訪問錯誤的時候會返回相應(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模塊中不然會報(bào)錯);這里的include是指包含/usr/local/nginx/conf/conf.d/tcp路徑下所有的.conf。 http { ... include conf.d/http/*.conf; #HTTP相關(guān)配置(需要放在HTTP模塊中不然會報(bào)錯);這里的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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx本地目錄映射實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Nginx本地目錄映射實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10Nginx服務(wù)器的反向代理proxy_pass配置方法講解
這篇文章主要介紹了Nginx服務(wù)器的反向代理proxy_pass配置方法講解,包括經(jīng)常被提到的url的/問題的相關(guān)說明,需要的朋友可以參考下2016-01-01nginx找到默認(rèn)根目錄(root?html)的方法
這篇文章主要給大家介紹了nginx如何找到默認(rèn)根目錄(root?html),文中給出詳細(xì)的解決方法,通過代碼示例講解的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2023-11-11uwsgi+nginx代理Django無法訪問靜態(tài)資源的解決
這篇文章主要介紹了uwsgi+nginx代理Django無法訪問靜態(tài)資源,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Nginx報(bào)錯“Too many open files”的問題解決
本文主要介紹了Nginx報(bào)錯“Too many open files”的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05