Nginx是一款開源代碼的高性能HTTP服務器和反向代理服務器,同時支持IMAP/POP3/SMTP代理服務javascript
Nginx由內核和模塊組成,完成工做是經過查找配置文件將客戶端請求映射到一個location block(location是用於URL匹配的命令),location配置的命令會啓動不一樣模塊完成工做。Nginx模塊分爲核心模塊,基礎模塊和第三方模塊。
- 核心模塊:HTTP模塊、EVENT模塊(事件)、MAIL模塊。
- 基礎模塊:HTTP Access模塊、HTTP FastCGI模塊、HTTP Proxy模塊、HTTP Rewrite模塊。
- 第三方模塊:HTTP Upstream Request Hash模塊、Notice模塊、HTTP Access Key模塊。php
sudo apt-get update
sudo apt-get install nginx
css
配置文件在 /etc/nginx/nginx.conf. 大致佈局以下:
html
配置文件主要由四個部分組成:main,server(主機配置),upstream(負載均衡服務器設置),location(URL匹配特定位置設置)前端
#Nginx的worker進程運行用戶以及用戶組 #user nobody nobody; #Nginx開啓的進程數 worker_processes 1; #worker_processes auto; #如下參數指定了哪一個cpu分配給哪一個進程,通常來講不用特殊指定。若是必定要設的話,用0和1指定分配方式. #這樣設就是給1-4個進程分配單獨的核來運行,出現第5個進程是就是隨機分配了。eg: #worker_processes 4 #4核CPU #worker_cpu_affinity 0001 0010 0100 1000 #定義全局錯誤日誌定義類型,[debug|info|notice|warn|crit] #error_log logs/error.log info; #指定進程ID存儲文件位置 #pid logs/nginx.pid; #一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(ulimit -n)與nginx進程數相除,可是nginx分配請求並非那麼均勻,因此最好與ulimit -n的值保持一致。 #vim /etc/security/limits.conf # * soft nproc 65535 # * hard nproc 65535 # * soft nofile 65535 # * hard nofile 65535
events { #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,若是跑在FreeBSD上面,就用kqueue模型。 use epoll; #每一個進程能夠處理的最大鏈接數,理論上每臺nginx服務器的最大鏈接數爲worker_processes*worker_connections。理論值:worker_rlimit_nofile/worker_processes #注意:最大客戶數也由系統的可用socket鏈接數限制(~ 64K),因此設置不切實際的高沒什麼好處 worker_connections 65535; #worker工做方式:串行(必定程度下降負載,但服務器吞吐量大時,關閉使用並行方式) #multi_accept on; }
#文件擴展名與文件類型映射表 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"'; #定義日誌的格式。後面定義要輸出的內容。 #1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址; #2.$remote_user :用來記錄客戶端用戶名稱; #3.$time_local :用來記錄訪問時間與時區; #4.$request :用來記錄請求的url與http協議; #5.$status :用來記錄請求狀態; #6.$body_bytes_sent :記錄發送給客戶端文件主體內容大小; #7.$http_referer :用來記錄從那個頁面連接訪問過來的; #8.$http_user_agent :記錄客戶端瀏覽器的相關信息 #鏈接日誌的路徑,指定的日誌格式放在最後。 #access_log logs/access.log main; #只記錄更爲嚴重的錯誤日誌,減小IO壓力 error_log logs/error.log crit; #關閉日誌 #access_log off; #默認編碼 #charset utf-8; #服務器名字的hash表大小 server_names_hash_bucket_size 128; #客戶端請求單個文件的最大字節數 client_max_body_size 8m; #指定來自客戶端請求頭的hearerbuffer大小 client_header_buffer_size 32k; #指定客戶端請求中較大的消息頭的緩存最大數量和大小。 large_client_header_buffers 4 64k; #開啓高效傳輸模式。 sendfile on; #防止網絡阻塞 tcp_nopush on; tcp_nodelay on; #客戶端鏈接超時時間,單位是秒 keepalive_timeout 60; #客戶端請求頭讀取超時時間 client_header_timeout 10; #設置客戶端請求主體讀取超時時間 client_body_timeout 10; #響應客戶端超時時間 send_timeout 10; #FastCGI相關參數是爲了改善網站的性能:減小資源佔用,提升訪問速度。 fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; #gzip模塊設置 #開啓gzip壓縮輸出 gzip on; #最小壓縮文件大小 gzip_min_length 1k; #壓縮緩衝區 gzip_buffers 4 16k; #壓縮版本(默認1.1,前端若是是squid2.5請使用1.0) gzip_http_version 1.0; #壓縮等級 1-9 等級越高,壓縮效果越好,節約寬帶,但CPU消耗大 gzip_comp_level 2; #壓縮類型,默認就已經包含text/html,因此下面就不用再寫了,寫上去也不會有問題,可是會有一個warn。 gzip_types text/plain application/x-javascript text/css application/xml; #前端緩存服務器緩存通過壓縮的頁面 gzip_vary on;
#虛擬主機定義 server { #監聽端口 listen 80; #訪問域名 server_name localhost; #編碼格式,若網頁格式與此不一樣,將被自動轉碼 #charset koi8-r; #虛擬主機訪問日誌定義 #access_log logs/host.access.log main; #對URL進行匹配 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; } #訪問URL以.php結尾則自動轉交給127.0.0.1 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} #php腳本請求所有轉發給FastCGI處理 # 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; #} #禁止訪問.ht頁面 (需ngx_http_access_module模塊) # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } #HTTPS虛擬主機定義 # 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; # } #}
大概用到的就這些,先碼一下。還有反向代理負載均衡等,之後要用再看Nginx配置文件詳解java