阻塞調用:事件沒有準備好,那就只能等了,等事件準備好了,你再繼續吧。
阻塞調用會進入核心等待,cpu就會讓出去給別人用了,對單線程的worker來說,顯然不合適,當網路事件越多時,大家都在等待呢,cpu空閑下來沒人用,cpu利用率自然上不去了,更別談高並發了。
非同步非阻塞:非同步非阻塞的事件處理機制,具體到系統調用就是像select/poll/epoll/kqueue這樣的系統調用。
它們提供了一種機制,讓你可以同時監控多個事件,調用他們是阻塞的,但可以設定逾時時間,在逾時時間之內,如果有事件準備好了,就返回。
tar -zxvf nginx.tar.gz
./configure
linux 安裝gcc,gcc-c++
yum -y install gcc gcc-c++ autoconf automake
安裝pcre
yum -y install pcre pcre-devel
安裝zlib
yum -y install zlib zlib-devel
make
make install
啟動nginx 伺服器
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
停止nginx伺服器
1.從容停止
kill -QUIT 15369
2.快速停止
kill -TERM 15417
kill -INT 15417
3.強制停止
pkill -9 nginx
4.驗證設定檔的正確性
./nginx -t
./nginx -t -c /usr/local/nginx/conf/nginx.conf
5.nginx 重啟
./nginx -s reload(進入到所在目錄)
kill -HUP 15446
6.USR1:切換記錄檔
USR2:平滑升級可執行進程
WINCH:從容關閉背景工作處理序(kill -WINCH 2255)
7.查看版本
./nginx -V
#設定使用者
#user nobody
#工作繁衍的處理序數(等於cpu的核心數或2倍的核心數)
worker_processes 6;
#設定pid存放的路徑
#pid logs/nginx.pid;
//最大串連數
events {
worker_connections 1024;
}
#開啟gzip 開啟(使用者訪問的是壓縮過的檔案(原來的30%))
#gzip on
#設定字元編碼
charset koi8-r;
charset gb2312;
nginx設定檔的抽象(執行個體)
user nobody;
worker_processes 4;
events{
worker_connections 1024;
}
http{
server{
listen 192.168.1.7:80;
server_name 192.168.1.7;
access_log logs/server1.access.log.combined;
location /
{
index index.html index.htm;
root html/server1;
}
}
server{
listen 192.168.1.8:80;
server_name 192.168.1.17;
access_log logs/server2.access.log.combined;
location /
{
index index.html index.htm;
root html/server2;
}
}
}
設定主機的ip地址和子網路遮罩
ifconfig eth1 192.168.1.10 netmask 255.255.255.0
設定虛擬機器主機(子伺服器)的ip地址和廣播位址和子網路遮罩
ifconfig eth1:1 192.168.1.7 broadcast 192.168.1.255 netmask 255.255.255.0
虛擬機器主機的配置:在配置好了ip地址之後,我們需要將對應的ip地址與對應的虛擬機器主機建立聯絡
log_format指令是用來設定nginx伺服器的記錄檔的記錄格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
remote_addr : ip地址
remote_user : 使用者
request : 請求的網址
status : 狀態
body_bytes_sent : 傳送的位元組數
http_referer: 原頁面
http_user_agent : 瀏覽器(用戶端)
http_x_forwarded_for:類似ip
//修改nginx預設的設定檔
vi /usr/local/nginx/conf/nginx.conf
access_log 儲存路徑
/*****************nginx 實現負載平衡*********************/
user nobody;
worker_processes 4;
events{
worker_connections 1024;
}
http{
upstream myproject{
ip_hash;
server 115.239.210.27;
server 180.96.12.1;
server 42.156.140.7;
server 140.205.230.49;
server 122.225.67.253;
}
server{
listen 8080;
location /
{
proxy_pass http://myproject;
}
}
}
/*****************nginx 實現負載平衡*********************/
以上就介紹了nginx的安裝及使用,包括了nginx方面的內容,希望對PHP教程有興趣的朋友有所協助。