文章目錄
php
Nginx——安裝Nginx1.6.1
Nginx——工做模型
Nginx——配置文件詳解
Nginx——代理
Nginx——調優
Nginx——負載均衡策略
Nginx——Session共享
Nginx——動靜分離html
#---全局塊開始----#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;#----全局塊結束----#====events 塊開始====events {worker_connections 1024;}#====events 塊結束====#****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;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;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;#}}} #****http 塊結束****
從配置文件開始到 events 之間的內容,主要會設置一些影響 nginx 服務器總體運行的配 置參數。主要包括配置運行 Nginx 服務器的用戶(組)、容許生成的 worker process 數,進 程 PID 存放路徑、日誌存放路徑和類型以及配置文件的引入等。nginx
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;
worker_processes 是 Nginx 服務器併發處理服務的關鍵配置,值越大,能夠支持的併發處理 量也越多,可是會受到硬件、軟件等設備的制約。
error_log 配置 nginx 日誌文件的全路徑名
pid 配置進程 PID 存放路徑web
events { worker_connections 1024; }
- events 塊涉及的參數主要影響 Nginx 服務器與用戶的網絡鏈接,經常使用的設置包括是否開 啓對多 work process 下的網絡鏈接進行序列化,是否容許同時接受多個網絡鏈接,選取哪一種 事件驅動模型來處理鏈接請求,每一個 work process 能夠同時支持的最大鏈接數等。
- 上述的例子表示每一個 work process 支持的最大鏈接數爲 1024。這部分的配置對 Nginx 的性能影響比較大,在實際中應該靈活配置。
http 全局塊配置的指令包括文件引入、MIME-TYPE 定義、鏈接超時時間、單連接請求數上 限等。瀏覽器
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 80;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;#}}}
這塊和虛擬主機有密切關係,虛擬主機從用戶角度看,和一臺獨立的硬件主機是徹底一 樣的,該技術的產生是爲了節省互聯網服務器硬件成本。
每一個 http 塊能夠包括多個 server 塊,而每一個 server 塊就至關於一個虛擬主機。而每一個 server 塊也分爲全局 server 塊,以及能夠同時包含多個 location 塊。緩存
#gzip on; server { listen 80;#監聽的端口號 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; #}}
全局 server 塊bash
最多見的配置是本虛擬主機的監聽配置和本虛擬主機的名稱或 IP 配置。服務器
location 塊
一個 server 塊能夠配置多個 location 塊。網絡
這塊的主要做用是基於 Nginx 服務器接受到的請求字符串(例如 server_name/uri-string), 對虛擬主機名稱(也能夠是 IP 別名)以外的字符串(列如 前面的/uri-string)進行匹配,對 特定的請求進行處理。地址定向、數據緩存和應答控制等功能,還有許多第三方模塊的配置 也在這裏進行。併發
#user nobody; worker_processes 1; events { use epoll; worker_connections 1024; }
1. 用戶與工做進程
#user nobody; worker_processes 1; [root@nginx1 conf]# ps aux |grep nginx root 1170 0.0 0.0 22568 680 ? Ss 09:14 0:00 nginx: master process /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf nobody 1171 0.0 0.1 23020 1288 ? S 09:14 0:00 nginx: worker process root 1174 0.0 0.0 103264 876 pts/0 S+ 09:14 0:00 grep nginx [root@nginx1 conf]# ps aux |grep nginx root 1170 0.0 0.0 22568 680 ? Ss 09:14 0:00 nginx: master process /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf nobody 1171 0.0 0.1 23020 1288 ? S 09:14 0:00 nginx: worker process [root@nginx1 conf]# id nobody uid=99(nobody) gid=99(nobody) groups=99(nobody) [root@nginx1 conf]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash …… nobody:x:99:99:Nobody:/:/sbin/nologin
2. use epoll;
參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll 模型是 Linux 2.6 以上版本內核中的高性能網絡 I/O 模型,若是跑在 FreeBSD上面,就用 kqueue 模型。
3. worker_connections 1024;
單個後臺 worker process 進程的最大併發連接數。 併發總數是 worker_processes 和 worker_connections 的乘積,即 max_clients = worker_processes * worker_connections 在 設 置 了 反 向 代 理 的 情 況 下 , max_clients=worker_processes * worker_connections / 4 爲何上面反向代理要除以 4,應該說是一個經驗值 根據以上條件,正常狀況下的 Nginx Server 能夠應付的最大鏈接數爲:4 * 8000 = 32000 #worker_connections 值的設置跟物理內存大小有關 #由於併發受 IO 約束,max_clients 的值須小於系統能夠打開的最大文件數 系統能夠打開的最大文件數和內存大小成正比,通常 1GB 內存的機器上能夠打開的文件數 大約是 10 萬左右 #咱們來看看 360M 內存的 VPS 能夠打開的文件句柄數是多少: #$ cat /proc/sys/fs/file-max #輸出 34336 # 32000 < 34336,即併發鏈接總數小於系統能夠打開的文件句柄總數,這樣就在操 做系統能夠承受的範圍以內 #worker_connections 的值需根據 worker_processes 進程數目和系統能夠 打開的最大文件總數進行適當地進行設置 #使得併發總數小於操做系統能夠打開的最大文件數目 #其實質也就是根據主機的物理 CPU 和內存進行配置 #固然,理論上的併發總數可能會和實際有所誤差,由於主機還有其餘的工做進程需 要消耗系統資源。# ulimit -SHn 65535 設置能夠打開的文件數量
sendfile on; #tcp_nopush on;
sendfile 其實是 Linux2.0+之後的推出的一個系統調用,web 服務器能夠經過調整自 身的配置來決定是否利用 sendfile 這個系統調用。先來看一下不用 sendfile 的傳統網 絡傳輸過程:
read(file,tmp_buf, len); write(socket,tmp_buf, len); 硬盤 >> kernel buffer >> user buffer>> kernel socket buffer >>協議棧
一個基於 socket 的服務,首先讀硬盤數據,而後寫數據到 socket 來完成網絡傳輸的。
上面 2 行用代碼解釋了這一點,不過上面 2 行簡單的代碼掩蓋了底層的不少操做。來看看 底層是怎麼執行上面 2 行代碼的:
一、系統調用 read()產生一個上下文切換:從 user mode 切換到 kernel mode,然 後 DMA 執行拷貝,把文件數據從硬盤讀到一個 kernel buffer 裏。 二、數據從 kernel buffer 拷貝到 user buffer,而後系統調用 read() 返回,這時 又產生一個上下文切換:從 kernel mode 切換到 user mode。 三、 系統調用 write()產生一個上下文切換:從 user mode 切換到 kernel mode,然 後把步驟 2 讀到 user buffer 的數據拷貝到 kernel buffer (數據第 2 次拷貝到 kernel buffer),不過此次是個不一樣的 kernel buffer,這個 buffer 和 socket 相關聯。 四、系統調用 write()返回,產生一個上下文切換:從 kernel mode 切換到 user mode ,而後 DMA 從 kernel buffer 拷貝數據到協議棧。
上面 4 個步驟有 4 次上下文切換,有 4 次拷貝,咱們發現若是能減小切換次數和拷貝次數 將會有效提高性能。在 kernel2.0+ 版本中,系統調用 sendfile() 就是用來簡化上面 步驟提高性能的。sendfile() 不但能減小切換次數並且還能減小拷貝次數。
再來看一下用 sendfile()來進行網絡傳輸的過程:
sendfile(socket,file, len); 硬盤 >> kernel buffer (快速拷貝到 kernelsocket buffer) >>協議棧
一、 系統調用 sendfile()經過 DMA 把硬盤數據拷貝到 kernel buffer,而後數據被 kernel 直接拷貝到另一個與 socket 相關的 kernel buffer。 這裏沒有 user mode 和 kernel mode 之間的切換,在 kernel 中直接完成了從一個 buffer 到另外一個 buffer 的拷貝。 二、DMA 把數據從 kernelbuffer 直接拷貝給協議棧,沒有切換,也不須要數據從 user mode 拷貝到 kernel mode,由於數據就在 kernel 裏。
簡單說,sendfile 是個比 read 和 write 更高性能的系統接口, 不過須要注意的是, sendfile 是將 in_fd 的內容發送到 out_fd 。而 in_fd 不能是 socket , 也就是 只能文件句柄。 因此當 Nginx 是一個靜態文件服務器的時候,開啓 SENDFILE 配置項 能大大提升 Nginx 的性能。 可是當 Nginx 是做爲一個反向代理來使用的時候, SENDFILE 則沒什麼用了,由於 Nginx 是反向代理的時候。 in_fd 就不是文件句柄而 是 socket,此時就不符合 sendfile 函數的參數要求了。
keepalive_timeout 65;
測試時改成 0,便於看出負載切換的效果,部署到生產前進行優化來提升效率。
#gzip on;
壓縮能夠有效減小文件的大小,有利於網絡傳輸。
autoindex on; #開啓目錄列表訪問,合適下載服務器,默認關閉。
虛擬主機,就是將一臺物理服務器虛擬爲多個服務器來使用,從而實如今一臺服務器上 配置多個站點,便可以在一臺物理主機上配置多個域名。Nginx 中,一個 server 標籤 就是一臺虛擬主機,配置多個 server 標籤就虛擬出了多臺主機。Nginx 虛擬主機的實 現方式有兩種:域名虛擬方式與端口虛擬方式。域名虛擬方式是指不一樣的虛擬機使用不一樣的 域名,經過不一樣的域名虛擬出不一樣的主機;端口虛擬方式是指不一樣的虛擬機使用相同的域名 不一樣的端口號,經過不一樣的端口號虛擬出不一樣的主機。基於端口的虛擬方式不經常使用。
gzip on; server { listen 80; server_name www.sxthenhao.com; location / { root /mnt; autoindex on; } }server { listen 80; server_name www.123.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } ......
從新加載 nginx
[root@nginx1 conf]# service nginx reload
192.168.20.11 nginx1 www.123.com www.sxthenhao.com
[root@nginx1 conf]# mount /dev/cdrom /mnt
對比下圖
Nginx 還能夠做爲日誌服務器
[root@nginx1 logs]# pwd /opt/nginx/logs [root@nginx1 logs]# tail -f access.log
本地瀏覽器訪問:http://www.123.com/2019-12-03maxwd19
先無論 404 的問題,查看日誌多了一天記錄
修改一下 http://www.123.com/2019-12-04maxwd20,日誌又記錄一條
固然日誌格式咱們也能夠自定義
access_log 配置 http 下,多 server 公用,配置 http->某 server 下,僅對該 server 使用。
http://www.sxthenhao.com/2019-12-04maxwd20myfmt.log 日誌記錄多一條
http://www.123.com/2019-12-04maxwd20 日誌記錄 access.log 多一條(並無 使用 myfmt.log)
參考:
http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_m odule.html 語法 location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } 默認值 - 上下文 server, location
讓咱們用一個例子解釋上面的說法:
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* .(gif|jpg|jpeg)$ {
[ configuration E ]
}
請求" / 「匹配配置 A,
請求」 /index.html 「匹配配置 B,
請求」/documents/document.html"匹配配置 C,
請求"/images/1.gif"匹配配置 D,
請求「/documents/1.jpg」匹配配置 E。
- 修改 nginx.conf 配置文件
server { listen 80; server_name www.sxthenhao.com; access_log logs/myfmt.log myfmt; location / { root /mnt; autoindex on; } location /aabb { proxy_pass http://192.168.20.102/;#帶上/訪問該 url 對應的首頁, #不帶/ 訪問 http://192.168.20.102/aabb } }
- 從新加載 nginx
[root@nginx1 conf]# !ser- 訪問測試
http://www.sxthenhao.com/ooxx- 修改 nginx.conf
location /ooxx {
proxy_pass http://www.baidu.com/;
}- 重啓 nginx
- [root@nginx1 conf]# !ser
若是重啓沒有問題,直接跳步驟 7
若是出現下圖所示的錯誤:
找不到域名,也就是訪問不到域名解析服務器。
解決辦法:- 訪問測試 http://www.sxthenhao.com/ooxx
雖然訪問到了百度,可是確實經過重定向的方式,之後發生的事情和咱們的服務器就沒有半 毛錢關係了。
優化配置 nginx.conf:
#儘可能在服務器端跳轉,不要在客戶端跳轉
proxy_pass https://www.baidu.com/;
重啓 nginx,再次測試,地址欄沒有重定向,可是當咱們查詢(好比:ssd)時出現
修改 nginx.conf
location /ooxx { proxy_pass http://www.baidu.com/; }location ~* /s.* { proxy_pass https://www.baidu.com; }
[root@nginx1 conf]# service nginx reload nginx: [emerg] https protocol requires SSL support in /opt/nginx/conf/nginx.conf:45 nginx: configuration file /opt/nginx/conf/nginx.conf test failed
當初編譯的時候沒有啓用 SSL 支持,在配置反向代理到 https 的網站時,編輯配置文件報 錯,沒法啓動 nginx。
解決辦法:先將 nginx.conf 備份/root/目錄下,刪除/opt/nginx 和/opt/apps/ nginx-1.16.1,而後在解壓一份,最後編譯安裝。
[root@nginx1 nginx-1.16.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module [root@nginx1 nginx-1.16.1]# make && make install [root@nginx1 nginx-1.16.1]# cd /opt/nginx/conf/ [root@nginx1 conf]# cp /root/nginx.conf ./ cp: overwrite `./nginx.conf'? yes [root@nginx1 conf]# service nginx reload nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful Reloading nginx:
而後再訪問 http://www.sxthenhao.com/ooxx