nginx優勢 select,epoll模型
對於一次IO訪問(以read舉例),數據會先被拷貝到操作系統內核的緩沖區中,然后才會從操作系統內核的緩沖區拷貝到應用程序的地址空間。所以說。當一個read操作發生時,它會經歷兩個階段:
1.等待數據准備(waiting for the data to be ready) 2.將數據從內核拷貝到進程中,正是因為這兩個階段,Linux系統產生了下面的五種網絡模: - 阻塞IO(blocking IO) - 非阻塞IO(nonblocking IO) - IO多路復用(IO multplexing) - 信號驅動IO(signal driven IO) - 異步IO(asynchronous IO) 注:由於signal driven IO在實際中並不常用,所以只提剩下的四種IO模型
阻塞IO(blocking IO)
在Linux中,默認情況下所有的socket都是blocking,一個典型的讀操作流程大概是這樣的:
當用戶進程調用了recvfrom這個系統調用,kernel就開始了IO的第一個階段:准備數據(對於網絡IO來說,很多時候啥數據在一開始還沒有到達。比如,還沒有收到一個完整的udp包。這個時候kernel就要等待足夠的數據到來)。這個過程需要等待,也就是說數據被拷貝到操作系統內核的緩沖區中是需要一個過程的。而在用戶進程這邊,整個進程都會被阻塞(當然,這個事進程自己選擇的阻塞)。當kernel一直等到數據准備好了。他就會將數據從kernel中拷貝到用戶內存,然后kernel返回結果。用戶進程才接觸blocking狀態,重新運行起來。
所以,blocking IO 的特點就是在IO執行的兩個階段都被block了。
非阻塞IO(nonblocking IO)
Linux下,可以通過設置socket使其變為non-blocking。當對一個non-blocing socket執行讀操作時,整個流程是這個樣子的:
當用戶進程發出read操作時,如果kernel中的數據還沒有准備好,那么它並不會block用戶進程,而是立刻返回一個error,從用戶進程角度講,它發起一個read操作后,並不需要等待,二十馬上就得到了一個結果。用戶進程判斷結果是一個error時,他就知道啥數據還沒有准備好,於是它可以再次發送read操作。一旦kernel中的數據准備好了,並且又再次收到了用戶進程的system call,那么它馬上就將數據拷貝到了用戶內存,然后返回。
所以,nonblocking IO的特點是用戶進程需要不斷的主動詢問kernel數據准備好了沒有。
IO多路復用(IO multiplexing)
IO multiplexing 就是我們說的select,poll,epoll,有些地方也稱這總IO方式為事件驅動IO(event driven IO)。select/epoll的好處就在於單個process就可以同時處理多個網絡連接的IO。他的基本原理就是select,poll,epoll這個function會不斷的輪詢所負責的所有socket,當某個socket有數據到達了,就通知用戶進程。
當用戶進程調用了select,那么這個進程會被block,而同時,kernel會“監視”所有select負責的socket,當任何一個socket中的數據准備好了,select就會返回,這個會后用戶進程再調用read操作。將數據從kernel拷貝到用戶進程。
所以,IO多路復用的特點是通過一種機制,一個進程能同時等待多個文件描述符,而這些文件描述符(套接字描述符)其中的任意一個進入讀就緒狀態,select()函數就會返回。
這個圖和blocking IO的圖其實並沒有太大的不同,事實上,還更差一些,因為這里需要使用兩個system call (select 和recvform),而blocking IO 只調用了一個system call(recvform),但是,用select的有事在於它可以同時處理多個connection。
所以,如果處理的鏈接數不是很高的話,使用select/epoll 的web server 不一定比使用multi-threading + blocking IO 的web server性能更好,可能延遲還更大。
select/epoll 的優勢並不是對於單個連接能處理的更快,而是在於能處理更多的鏈接
在IO multiplexing model中,實際中,對於每一個socket,一般都設置成為nonblocking,但是,如上圖所示,這個用戶的process其實是一直被block的,只不過process是被select這個函數block,而不是被socket IO給block。
異步IO(asynchronous IO)
Linux下的asynchronous IO其實用得很少,先看一下它的流程:
用戶進程發起read操作之后,立刻就可以開始做其它的事。而另一方面,從kernel的角度,當它收到一個asynchronous之后,首先它會立刻返回,所以不會對用戶進程產生任何的block。然后,kernel會等待數據准備完成,然后將數據拷貝到用戶內存,當這一切都完成之后,kernel會給用戶進程發送一個signal,告訴它read操作完成了。
--------------------------------------------------------------------------------------------------------------------------------
nginx安裝
可以采用源碼安裝或者yum安裝,具體方式請百度
nginx需要依賴gcc等依賴,建議先安裝依賴
剛剛遇到一個問題,centos7安裝nginx竟然報錯了。。
[root@localhost ~]# yum install nginx -y 已加載插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.cn99.com * updates: mirrors.163.com 沒有可用軟件包 nginx。 錯誤:無須任何處理 [root@localhost ~]#
這個問題比較容易解決,可以采用epel的方式安裝nginx
[root@localhost ~]# yum install epel-rpm-macros [root@localhost ~]# yum update [root@localhost ~]# yum install nginx [root@localhost ~]#
安裝就到這里了,遇到問題自行百度就好。
[root@localhost ~]# systemctl stop nginx # 停止服務 [root@localhost ~]# systemctl start nginx # 啟動服務 [root@localhost ~]# systemctl status nginx # 查看狀態 [root@localhost ~]# systemctl restart nginx # 重新加載 [root@localhost ~]#
[root@localhost ~]# systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: inactive (dead) 9月 18 11:36:42 localhost.localdomain systemd[1]: Unit nginx.service cannot be reloaded because it is...ve. Hint: Some lines were ellipsized, use -l to show in full. [root@localhost ~]# systemctl stop nginx [root@localhost ~]# systemctl start nginx [root@localhost ~]# systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since 二 2018-09-18 11:38:58 CST; 2s ago Process: 66173 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 66170 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 66164 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 66175 (nginx) Tasks: 3 CGroup: /system.slice/nginx.service ├─66175 nginx: master process /usr/sbin/nginx ├─66176 nginx: worker process └─66177 nginx: worker process 9月 18 11:38:57 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server... 9月 18 11:38:58 localhost.localdomain nginx[66170]: nginx: the configuration file /etc/nginx/nginx.co... ok 9月 18 11:38:58 localhost.localdomain nginx[66170]: nginx: configuration file /etc/nginx/nginx.conf t...ful 9月 18 11:38:58 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server. Hint: Some lines were ellipsized, use -l to show in full. [root@localhost ~]#
nginx默認使用80端口,已經啟動了,可以從瀏覽器中進行訪問了。
nginx核心模塊
https://nginx.org/en/docs/ 可以進這個里面找你需要的看,
nginx HTTP模塊
https://nginx.org/en/docs/http/ngx_http_core_module.html#server
Nginx虛擬主機
Nginx反向代理
反向代理 VS 正向代理
什么是正向代理?什么是反向代理?
正向代理:
假設在客戶機與目標主機之間,只用戶代理內部網絡對Internet的鏈接請求,客戶機必須指定代理服務器,並將原來要直接發送到web服務器上的HTTP請求發送到代理服務器中。 1.提高訪問速度 2.防火牆作用 3.通過代理服務器訪問不能方案文的目標站點
反向代理:
服務器架設在服務器端,通過緩沖經常被請求的頁面來環節服務器的工作量,將客戶機請求轉發給內部網絡上的目標服務器;並將從服務器上得到的結果返回給Internet請求鏈接的客戶端,此時代理服務器與目標主機一起對外表現為一個服務器。 1.可以防止外網對內網服務器的惡性攻擊 2.緩存以減少服務器的壓力 3.訪問安全控制之外 4.可以進行負載均衡,將用戶的請求分配給多個服務器
# proxy the PHP scripts to Apache listening on 127.0.0.1c80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} location ~ \.html$ { proxy_pass http://192.168.251.102c8080; }
# proxy the PHP scripts to Apache listening on 127.0.0.1c80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} location ~ \.html$ { proxy_pass http://webserver; } upstream webserver { server 192.168.251.102:8080; }
# proxy the PHP scripts to Apache listening on 127.0.0.1c80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} location ~ \.html$ { proxy_pass http://webserver; proxy_cache my-cache; } upstream webserver { s erver 192.168.251.102c8080; }
server { listen 80; server_name xxx.xt.com; access_log /var/log/nginx/git.chjrt.access.log; error_log /var/log/nginx/git.chjrt.error.log; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://172.20.206.41:8089; } }
更多等用到了補充。目前就用了這么點。。