一、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;
?
user root;
- 這一配置項指定了 Nginx 工作進程所使用的用戶身份。
root
?是系統中的超級用戶,擁有最高權限。不過,從安全角度考慮,不建議讓 Nginx 以?root
?用戶身份運行,因為這會使 Nginx 擁有過高的權限,一旦出現安全漏洞,攻擊者可能會獲取系統的最高控制權。通常,建議創建一個專門的低權限用戶來運行 Nginx。worker_processes 4;
- 此配置項用于設置 Nginx 工作進程的數量。Nginx 采用多進程模型,一個主進程(master process)負責管理多個工作進程(worker processes),工作進程負責處理實際的客戶端請求。
4
?代表創建 4 個工作進程。一般而言,可以根據服務器的 CPU 核心數來設置該值,通常設置為 CPU 核心數或者核心數的兩倍,這樣能充分利用服務器的 CPU 資源。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
}