4.2 event事件
events {worker_connections 65536; #設置單個工作進程的最大并發連接數use epoll;#使用epoll事件驅動,Nginx支持眾多的事件驅動,比如:select、poll、epoll,只能設置在events模塊中設置。accept_mutex on; #on為同一時刻一個請求輪流由work進程處理,而防止被同時喚醒所有worker,避免多個睡眠進程被喚醒的設置,默認為off,新請求會喚醒所有worker進程,此過程也稱為"驚群",因此nginx剛安裝完以后要進行適當的優化。建議設置為onmulti_accept on; #ON時Nginx服務器的每個工作進程可以同時接受多個新的網絡連接,此指令默認為off,即默認為一個工作進程只能一次接受一個新的網絡連接,打開后幾個同時接受多個。建議設置為on
}
面試題:一個服務端最多能接受多少客戶端?
服務器的硬件配置、操作系統的設置、網絡帶寬等。在理論上,一個服務器可以接受的最大客戶端數量是有限的,這個數量通常被稱為"最大并發連接數"。然而,這個數量可能會受到服務器硬件的限制,例如 CPU、內存和網絡帶寬的限制。此外,操作系統的設置也可能會影響這個數量,例如操作系統可能會限制一個進程可以打開的文件描述符的數量。
4.3 http設置
http 是一個大的語句塊,包含若干個小的語句塊(比如server語句塊)
http {...... #各server的公共配置server { #每個server用于定義一個虛擬主機,第一個server為默認虛擬服務器...}server { ...server_name #虛擬主機名root #主目錄alias #路徑別名location [OPERATOR] URL { #指定URL的特性...if CONDITION {...}}}
}
?http 協議配置說明
http {include mime.types; #導入支持的文件類型,是相對于/apps/nginx/conf的目錄default_type application/octet-stream; #除mime.types中文件類型外,設置其它文件默認類型,訪問其它類型時會提示下載不匹配的類型文件
#日志配置部分#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; #設置會話保持時間,第二個值為響應首部:keepAlived:timeout=65,可以和第一個值不同#gzip on; #開啟文件壓縮server {listen 80; #設置監聽地址和端口server_name localhost; #設置server name,可以以空格隔開寫多個并支持正則表達式,如:*.kgc.com www.kgc.* ~^www\d+\.kgc\.com$ default_server #charset koi8-r; #設置編碼格式,默認是俄語格式,建議改為utf-8#access_log logs/host.access.log main;location /fxj { www.ky31.com/fsj /apps/nginx/html root /data;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
3.3.1 MIME
范例: 識別php文件為text/html# 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;# }#}
4.3.1mime
此項為支持的 文件格式,如果不支持的格式 會自動幫你下載,如果支持 就會顯示在網頁上
[root@localhost ~]#vim /etc/nginx/mime.types
types {text/html html htm shtml;.....................................................................image/png png;image/svg+xml svg svgz;image/tiff tif tiff;image/vnd.wap.wbmp wbmp;image/webp webp;image/x-icon ico;image/x-jng jng;image/x-ms-bmp bmp;
4.3.2sever 下的 root
root指定了主頁文件的位置
root路徑格式 指定文件的路徑 url
Syntax: root path;
Default:
root html;
Context: http, server, location, 指明 你軟件的根目錄注意:root在 http,server,location都可以寫,可以寫入哪里,可以參考nginx官網
寫在server中
?
也可以這樣玩[root@localhost conf.d]#vim pc.conf
#分別編寫配置文件
server {listen 80;server_name localhost;root /data/nginx/html/pc/;
}
#也可以使用location 模塊
server{listen 192.168.91.100:80;server_name www.pc.com;location / {root /data/nginx/html/pc;}
}
?也可以寫在location中