一、nginx
nginx(“engine x”)是一款是由俄羅斯的程序設計師igor sysoev所開發高性能的 web和 反向代理 服務器,也是一個 imap/pop3/smtp 代理服務器。
三大WEB服務器:apache, Nginx, lighttpd之一。Nginx是一種很好的替代品,當需要處理高并發連接時,它比Apache服務器更為適合。
nginx應用場合
-
靜態服務器。(圖片,視頻服務)另一個是lighttpd。并發幾萬,html,js,css,flv,jpg,gif等。
-
動態服務,nginx——fastcgi 的方式運行PHP,jsp。(PHP并發在500-1500,MySQL 并發在300-1500)。
-
反向代理,負載均衡。日pv2000W以下,都可以直接用nginx做代理。
-
緩存服務。類似 SQUID,VARNISH。
官網提供三種版本:
Nginx官網提供了三個類型的版本?
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版?
Stable version:最新穩定版,生產環境上建議使用的版本?
Legacy versions:遺留的老版本的穩定版
二、nginx服務搭建?
1、使用apt安裝
sudo?apt?install?nginx
2、安裝后的位置:?
-
/usr/sbin/nginx:主程序
-
/etc/nginx:存放配置文件
-
/usr/share/nginx:存放靜態文件
-
/var/log/nginx:存放日志
3、啟動并驗證效果
service?nginx?start??#?啟動nginx service?nginx?reload??#?重新加載nginx配置文件
在瀏覽器輸入你的ip地址,如果出現Wellcome to nginx 那么就是配置成功。
另外兩個命令
nginx?-s?reopen????????????#?重啟?Nginx nginx?-s?stop??????????????#?停止?Nginx
4、查看版本號:
~$?nginx?-v ?nginx?version:?nginx/1.14.0?(Ubuntu)
三、nginx配置文件介紹
1、nginx 文件結構
-
全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日志存放路徑,配置文件引入,允許生成worker process數等。
-
events塊:配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啟多個網絡連接序列化等。
-
http塊:可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。
-
server塊:配置虛擬主機的相關參數,一個http中可以有多個server。
-
location塊:配置請求的路由,以及各種頁面的處理情況。
...??????????????#?全局塊。配置影響nginx全局的指令。 events?{?????????#?events塊。配置影響nginx服務器或與用戶的網絡連接。 ???... } http??????#?http塊。可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方模塊的配置。 { ????...???#?http全局塊 ????server????????#?server塊。配置虛擬主機的相關參數,一個http中可以有多個server。? ????{? ????????...???????#?server全局塊 ????????location?[PATTERN]???#?location塊。配置請求的路由,以及各種頁面的處理情況。 ????????{ ????????????... ????????} ????????location?[PATTERN]? ????????{ ????????????... ????????} ????} ????server ????{ ??????... ????} ????...?????#?http全局塊 }
2、默認的配置
## #?You?should?look?at?the?following?URL's?in?order?to?grasp?a?solid?understanding #?of?Nginx?configuration?files?in?order?to?fully?unleash?the?power?of?Nginx. #?https://www.nginx.com/resources/wiki/start/ #?https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ #?https://wiki.debian.org/Nginx/DirectoryStructure # #?In?most?cases,?administrators?will?remove?this?file?from?sites-enabled/?and #?leave?it?as?reference?inside?of?sites-available?where?it?will?continue?to?be #?updated?by?the?nginx?packaging?team. # #?This?file?will?automatically?load?configuration?files?provided?by?other #?applications,?such?as?Drupal?or?Wordpress.?These?applications?will?be?made #?available?underneath?a?path?with?that?package?name,?such?as?/drupal8. # #?Please?see?/usr/share/doc/nginx-doc/examples/?for?more?detailed?examples. ## #?Default?server?configuration # server?{ ????listen?80?default_server; ????listen?[::]:80?default_server; ????#?SSL?configuration ????# ????#?listen?443?ssl?default_server; ????#?listen?[::]:443?ssl?default_server; ????# ????#?Note:?You?should?disable?gzip?for?SSL?traffic. ????#?See:?https://bugs.debian.org/773332 ????# ????#?Read?up?on?ssl_ciphers?to?ensure?a?secure?configuration. ????#?See:?https://bugs.debian.org/765782 ????# ????#?Self?signed?certs?generated?by?the?ssl-cert?package ????#?Don't?use?them?in?a?production?server! ????# ????#?include?snippets/snakeoil.conf; ????root?/var/www/html; ????#?Add?index.php?to?the?list?if?you?are?using?PHP ????index?index.html?index.htm?index.nginx-debian.html; ????server_name?_; ????location?/?{ ????????#?First?attempt?to?serve?request?as?file,?then ????????#?as?directory,?then?fall?back?to?displaying?a?404. ????????try_files?$uri?$uri/?=404; ????} ????#?pass?PHP?scripts?to?FastCGI?server ????# ????#location?~?.php$?{ ????#????include?snippets/fastcgi-php.conf; ????# ????#????#?With?php-fpm?(or?other?unix?sockets): ????#????fastcgi_pass?unix:/var/run/php/php7.0-fpm.sock; ????#????#?With?php-cgi?(or?other?tcp?sockets): ????#????fastcgi_pass?127.0.0.1:9000; ????#} ????#?deny?access?to?.htaccess?files,?if?Apache's?document?root ????#?concurs?with?nginx's?one ????# ????#location?~?/.ht?{ ????#????deny?all; ????#} } #?Virtual?Host?configuration?for?example.com # #?You?can?move?that?to?a?different?file?under?sites-available/?and?symlink?that #?to?sites-enabled/?to?enable?it. # #server?{ #????listen?80; #????listen?[::]:80; # #????server_name?example.com; # #????root?/var/www/example.com; #????index?index.html; # #????location?/?{ #????????try_files?$uri?$uri/?=404; #????} #}
3、nginx的基本配置
###########?每個指令必須有分號結束。################# #user?administrator?administrators;??#配置用戶或者組,默認為nobody?nobody。 #worker_processes?2;??#允許生成的進程數,默認為1 #pid?/nginx/pid/nginx.pid;???#指定nginx進程運行文件存放地址 error_log?log/error.log?debug;??#制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg events?{ ????accept_mutex?on;???#設置網路連接序列化,防止驚群現象發生,默認為on ????multi_accept?on;??#設置一個進程是否同時接受多個網絡連接,默認為off ????#use?epoll;??????#事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport ????worker_connections??1024;????#最大連接數,默認為512 } http?{ ????include???????mime.types;???#文件擴展名與文件類型映射表 ????default_type??application/octet-stream;?#默認文件類型,默認為text/plain ????#access_log?off;?#取消服務日志???? ????log_format?myFormat?'$remote_addr–$remote_user?[$time_local]?$request?$status?$body_bytes_sent?$http_referer?$http_user_agent?$http_x_forwarded_for';?#自定義格式 ????access_log?log/access.log?myFormat;??#combined為日志格式的默認值 ????sendfile?on;???#允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。 ????sendfile_max_chunk?100k;??#每個進程每次調用傳輸數量不能大于設定的值,默認為0,即不設上限。 ????keepalive_timeout?65;??#連接超時時間,默認為75s,可以在http,server,location塊。 ????upstream?mysvr?{??? ??????server?127.0.0.1:7878; ??????server?192.168.10.121:3333?backup;??#熱備 ????} ????error_page?404?https://www.baidu.com;?#錯誤頁 ????server?{ ????????keepalive_requests?120;?#單連接請求上限次數。 ????????listen???????80;???#監聽端口 ????????server_name??127.0.0.1;???#監聽地址??????? ????????location??~*^.+$?{???????#請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。 ???????????#root?path;??#根目錄 ???????????#index?vv.txt;??#設置默認頁 ???????????proxy_pass??http://mysvr;??#請求轉向mysvr?定義的服務器列表 ???????????deny?127.0.0.1;??#拒絕的ip ???????????allow?172.18.5.54;?#允許的ip??????????? ????????}? ????} }
四、 nginx虛擬主機配置
#下面是server虛擬主機的配置段 ?server ??{ ????listen?80;#監聽端口 ????server_name?localhost;#域名 ????index?index.html?index.htm?index.php; ????root?/usr/local/webserver/nginx/html;#站點目錄 ??????location?~?.*.(php|php5)?$ ????{ ??????#fastcgi_pass?unix:/tmp/php-cgi.sock; ??????fastcgi_pass?127.0.0.1:9000; ??????fastcgi_index?index.php; ??????include?fastcgi.conf; ????} ????location?~?.*.(gif|jpg|jpeg|png|bmp|swf|ico)$ ????{ ??????expires?30d; ????#access_log?off; ????} ????location?~?.*.(js|css)?$ ????{ ??????expires?15d; ?????#access_log?off; ????} ????access_log?off; ??}
驗證配置文件:
root@ubuntu:?nginx?-t nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful
五、nginx全局變量
-
為了記錄客戶端的IP地址,可以使用$remote_addr和$http_x_forwarded_for
-
$remote_user :用來記錄客戶端用戶名稱;
-
$time_local : 用來記錄訪問時間與時區;
-
$request : 用來記錄請求的url與http協議;
-
$status : 用來記錄請求狀態;成功是200;
-
$body_bytes_s ent :記錄發送給客戶端文件主體內容大小;
-
$http_referer :用來記錄從那個頁面鏈接訪問過來的;
-
$http_user_agent :記錄客戶端瀏覽器的相關信息;
訪問鏈接是:http://localhost:88/test1/test.php? 網站路徑是:/var/www/html $host:localhost $server_port:88 $request_uri:<a>http:</a>//localhost:88/test1/test.php $document_uri:/test1/test.php $document_root:/var/www/html $request_filename:/var/www/html/test1/test.php
六、Nginx主要配置
1、靜態Http服務器配置
首先,Nginx是一個HTTP服務器,可以將服務器上的靜態文件(如HTML、圖片)通過HTTP協議展現給客戶端。?
配置:
server?{ ????listen?80;? #?端口 ????server_name?localhost??192.168.1.100;? #?域名??? ????location?/?{ #?代表這是項目根目錄 ????????root?/usr/share/nginx/www;? ?#?虛擬目錄 ????} }
2、反向代理服務器配置
什么是反向代理??
客戶端本來可以直接通過HTTP協議訪問某網站應用服務器,如果網站管理員在中間加上一個Nginx,客戶端請求Nginx,Nginx請求應用服務器,然后將結果返回給客戶端,此時Nginx就是反向代理服務器。
反向代理配置:
server?{ ????listen?80; ????location?/?{ ????????proxy_pass?http://192.168.0.112:8080;? #?應用服務器HTTP地址 ????} }
既然服務器可以直接HTTP訪問,為什么要在中間加上一個反向代理,不是多此一舉嗎?反向代理有什么作用?繼續往下看,下面的負載均衡、虛擬主機,都基于反向代理實現,當然反向代理的功能也不僅僅是這些。
3、負載均衡配置
當網站訪問量非常大,也攤上事兒了。因為網站越來越慢,一臺服務器已經不夠用了。因此,可以將同一應用部署在多臺服務器上,以將眾多用戶請求分配至多臺機器進行處理。即使其中一臺服務器故障,只要其他服務器正常運行,用戶仍然可以正常使用,這是多臺服務器帶來的好處。Nginx可以通過反向代理來實現負載均衡。
負載均衡配置:
upstream?myapp?{ ????server?192.168.0.111:8080;? #?應用服務器1 ????server?192.168.0.112:8080;? #?應用服務器2 } server?{ ????listen?80; ????location?/?{ ????????proxy_pass?http://myweb; ????} }
4、虛擬主機配置
有的網站訪問量大,需要負載均衡。然而并不是所有網站都如此出色,有的網站,由于訪問量太小,需要節省成本,將多個網站部署在同一臺服務器上。?
例如將www.aaa.com和www.bbb.com兩個網站部署在同一臺服務器上,兩個域名解析到同一個IP地址,但是用戶通過兩個域名卻可以打開兩個完全不同的網站,互相不影響,就像訪問兩個服務器一樣,所以叫兩個虛擬主機。
虛擬主機配置:
server?{ ????listen?80?default_server; ????server_name?_; ????return?444;? #?過濾其他域名的請求,返回444狀態碼 } server?{ ????listen?80; ????server_name?www.aaa.com;? #?www.aaa.com域名 ????location?/?{ ????????proxy_pass?http://localhost:8080;? #?對應端口號8080 ????} } server?{ ????listen?80; ????server_name?www.bbb.com;? #?www.bbb.com域名 ????location?/?{ ????????proxy_pass?http://localhost:8081;? #?對應端口號8081 ????} }
在服務器8080和8081分別開了一個應用,客戶端通過不同的域名訪問,根據server_name可以反向代理到對應的應用服務器。
虛擬主機的原理是通過HTTP請求頭中的Host是否匹配server_name來實現的,有興趣的同學可以研究一下HTTP協議。
另外,server_name配置還可以過濾有人惡意將某些域名指向你的主機服務器。