Nginx的配置文件的組成部分: 主配置文件:nginx.conf,子配置文件 include conf.d/*.conf
默認配置文件
[root@s2 ~]# grep -v "#" /apps/nginx/conf/nginx.conf | grep -v "^$" #全局配置端,對全局生效,主要設置nginx的啟動用戶/組,啟動的工作進程數量,工作模式,Nginx的PID路徑,日志路徑等。 user nginx nginx; worker_processes 4 |auto; #啟動工作進程數數量,一般設置和CPU核心數一致 events { #events設置快,主要影響nginx服務器與用戶的網絡連接,比如是否允許同時接受多個網絡連接,使用哪種事件驅動模型處理請求,每個工作進程可以同時支持的最大連接數,是否開啟對多工作進程下的網絡連接進行序列化等。 worker_connections 1024; #設置單個nginx工作進程可以接受的最大並發,作為web服務器的時候最大並發數為worker_connections * worker_processes,作為反向代理的時候為(worker_connections * worker_processes)/2 } http { #http塊是Nginx服務器配置中的重要部分,緩存、代理和日志格式定義等絕大多數功能和第三方模塊都可以在這設置,http塊可以包含多個server塊,而一個server塊中又可以包含多個location塊,server塊可以配置文件引入、MIME-Type定義、日志自定義、是否啟用sendfile、連接超時時間和單個鏈接的請求上限等。 include mime.types; default_type application/octet-stream; sendfile on; #作為web服務器的時候打開sendfile加快靜態文件傳輸,指定是否使用sendfile系統調用來傳輸文件,sendfile系統調用在兩個文件描述符之間直接傳遞數據(完全在內核中操作),從而避免了數據在內核緩沖區和用戶緩沖區之間的拷貝,操作效率很高,被稱之為零拷貝,硬盤 >> kernel buffer (快速拷貝到kernelsocket buffer) >>協議棧。 keepalive_timeout 65; #長連接超時時間,單位是秒 server { #設置一個虛擬機主機,可以包含自己的全局快,同時也可以包含多個location模塊。比如本虛擬機監聽的端口、本虛擬機的名稱和IP配置,多個server 可以使用一個端口,比如都使用80端口提供web服務、 listen 80; #配置server監聽的端口 server_name localhost; 本server的名稱,當訪問此名稱的時候nginx會調用當前serevr內部的配置進程匹配。 location / { #location其實是server的一個指令,為nginx服務器提供比較多而且靈活的指令,都是在location中提現的,主要是基於nginx接受到的請求字符串,對用戶請求的UIL進行匹配,並對特定的指令進行處理,包括地址重定向、數據緩存和應答控制等功能都是在這部分實現,另外很多第三方模塊的配置也是在location模塊中配置。 root html; #相當於默認頁面的目錄名稱,默認是相對路徑,可以使用絕對路徑配置。 index index.html index.htm; #默認的頁面文件名稱 } error_page 500 502 503 504 /50x.html; #錯誤頁面的文件名稱 location = /50x.html { #location處理對應的不同錯誤碼的頁面定義到/50x.html,這個跟對應其server中定義的目錄下。 root html; #定義默認頁面所在的目錄 } } #和郵件相關的配置 #mail { # ... # } mail 協議相關配置段 #tcp代理配置,1.9版本以上支持 #stream { # ... # } stream 服務器相關配置段 #導入其他路徑的配置文件 #include /apps/nginx/conf.d/*.conf }
Nginx 核心配置詳解
全局配置
user nginx nginx; #啟動Nginx工作進程的用戶和組 worker_processes [number | auto]; #啟動Nginx工作進程的數量 worker_cpu_affinity 00000001 00000010 00000100 00001000; #將Nginx工作進程綁定到指定的CPU核心,默認Nginx是不進行進程綁定的,綁定並不是意味着當前nginx進程獨占以一核心CPU,但是可以保證此進程不會運行在其他核心上,這就極大減少了nginx的工作進程在不同的cpu核心上的來回跳轉,減少了CPU對進程的資源分配與回收以及內存管理等,因此可以有效的提升nginx服務器的性能。 worker_cpu_affinity auto; #也支持自動檢測
# 未做CPU與進程綁定前,查看進程運行在哪個CPU
~]# ps axo pid,cmd,psr,user | grep nginx
1057 nginx: master process /usr/ 0 root
2086 nginx: worker process 3 nginx
2087 nginx: worker process 1 nginx
2088 nginx: worker process 2 nginx
2089 nginx: worker process 0 nginx
#錯誤日志記錄配置,日志級別語法:error_log file [debug | info | notice | warn | error | crit | alert | emerg] #error_log logs/error.log; #error_log logs/error.log notice; error_log /apps/nginx/logs/error.log error; #pid文件保存路徑 pid /apps/nginx/logs/nginx.pid; worker_priority 0; #工作進程nice值,-20~19 worker_rlimit_nofile 65536; #這個數字包括Nginx的所有連接(例如與代理服務器的連接等),而不僅僅是與客戶端的連接,另一個考慮因素是實際的並發連接數不能超過系統級別的最大打開文件數的限制. [root@s2 ~]# watch -n1 'ps -axo pid,cmd,nice | grep nginx' #驗證進程優先級 daemon off; #前台運行Nginx服務用於測試、docker等環境。 master_process off|on; #是否開啟Nginx的master-woker工作模式,僅用於開發調試場景。 events { #事件模型配置參數 worker_connections 65536; #設置單個工作進程的最大並發連接數 use epoll; #使用epoll事件驅動,Nginx支持眾多的事件驅動,比如select、poll、epoll,只能設置在events模塊中設置。 accept_mutex on; #優化同一時刻只有一個請求而避免多個睡眠進程被喚醒的設置,on為防止被同時喚醒默認為off,全部喚醒的過程也成為"驚群",因此nginx剛安裝完以后要進行適當的優化。 multi_accept on; Nginx服務器的每個工作進程可以同時接受多個新的網絡連接,但是需要在配置文件中配置,此指令默認為關閉,即默認為一個工作進程只能一次接受一個新的網絡連接,打開后幾個同時接受多個。 }
http詳細配置
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; #在開啟了sendfile的情況下,合並請求后統一發送給客戶端。 #tcp_nodelay off; #在開啟了keepalived模式下的連接是否啟用TCP_NODELAY選項,當為off時,延遲0.2s發送,默認On時,不延遲發送,立即發送用戶相應報文。 #keepalive_timeout 0; keepalive_timeout 65 65; #設置會話保持時間 #gzip on; #開啟文件壓縮 server { listen 80; #設置監聽地址和端口 server_name localhost; #設置server name,可以以空格隔開寫多個並支持正則表達式,如*.magedu.com www.magedu.* www.(site\d+)\.magedu\.com$ default_server #charset koi8-r; #設置編碼格式,默認是俄語格式,可以改為utf-8 #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$ { #以http的方式轉發php請求到指定web服務器 # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #以fastcgi的方式轉發php請求到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 { #拒絕web形式訪問指定文件,如很多的網站都是通過.htaccess文件來改變自己的重定向等功能。 # deny all; #} location ~ /passwd.html { deny all; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { #自定義虛擬server # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; #指定默認網頁文件,此指令由ngx_http_index_module模塊提供 # } #} # HTTPS server # #server { #https服務器配置 # 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; # }
location /linux38/passwd.ht { #拒絕訪問某個文件,會返回403 deny all; } #}
核心配置示例
基於不同的IP、不同的端口以及不用得域名實現不同的虛擬主機,依賴於核心模塊ngx_http_core_module實現。
新建一個PC web站點
[root@s2 ~]# mkdir /apps/nginx/conf/conf.d -p [root@s2 ~]# cat /apps/nginx/conf/conf.d/pc.conf server { listen 80; server_name www.magedu.net;
location / { root /data/nginx/html/pc; } }
[root@s2 ~]# mkdir /data/nginx/html/pc -p [root@s2 ~]# echo "pc web" > /data/nginx/html/pc/index.html
[root@s2 ~]# vim /apps/nginx/conf/nginx.conf #主配置文件http塊include子配置文件 include /apps/nginx/conf/conf.d/*.conf; [root@s2 ~]# systemctl reload nginx 訪問測試
新建一個Mobile web站點
[root@s2 ~]# cat /apps/nginx/conf/conf.d/mobile.conf server { listen 80; server_name mobile.magedu.net; location / { root /data/nginx/html/mobile; } }
[root@s2 ~]# mkdir /data/nginx/html/mobile -p [root@s2 ~]# echo "mobile web" >> /data/nginx/html/mobile/index.html [root@s2 ~]# systemctl reload nginx
root與alias:
root:指定web的家目錄,在定義location的時候,文件的絕對路徑等於 root+location,如:
server { listen 80; server_name www.magedu.net;
location / { root /data/nginx/html/pc; }
location /about { root /data/nginx/html/pc; #必須要在html目錄中創建一個about目錄才可以訪問,否則報錯。 index index.html; } }
[root@s2 ~]# mkdir /data/nginx/html/pc/about [root@s2 ~]# echo about > /data/nginx/html/pc/about/index.html
alias:定義路徑別名,會把訪問的路徑重新定義到其指定的路徑,如:
server { listen 80; server_name www.magedu.net;
location / { root /data/nginx/html/pc; }
location /about { #使用alias的時候uri后面如果加了斜杠則下面的路徑配置必須加斜杠,否則403 alias /data/nginx/html/pc; #當訪問about的時候,會顯示alias定義的/data/nginx/html/pc里面的內容。 index index.html; } } 重啟Nginx並訪問測試 http://www.magedu.net/about/index.html #訪問指定文件資源
測試訪問
[root@nginx-2 ~]# curl http://www.magedu.net/about/index.html pc web [root@nginx-2 ~]# curl http://www.magedu.net/about <html> <head><title>301 Moved Permanently</title></head> <body> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.16.1</center> </body> </html> [root@nginx-2 ~]# curl http://www.magedu.net pc web [root@nginx-2 ~]# curl http://mobile.magedu.net mobile web