NGINX是一個免費的,開源的,高性能的HTTP服務器和反向代理,以及IMAP / POP3代理服務器。NGINX以其高性能,穩定性,豐富的功能集,簡單的配置和低資源消耗而聞名。php
NGINX是用於Web服務,反向代理,緩存,負載平衡,媒體流等的開源軟件。它最初是一個旨在實現最高性能和穩定性的Web服務器。除了HTTP服務器功能外,NGINX還能夠用做電子郵件(IMAP,POP3和SMTP)的代理服務器以及HTTP,TCP和UDP服務器的反向代理和負載平衡器。css
yum install yum-utilshtml
vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key
yum install nginx
nginx
wget http://nginx.org/download/nginx-1.9.4.tar.gz tar -xzf nginx-1.9.4.tar.gz cd nginx-1.9.4
yum update yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
添加用戶和組 web
groupadd www useradd -g www www 配置 ./configure \ --user=www \ --group=www \ --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-threads 編譯 make 安裝 make install
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
正則表達式
nginx/sbin/nginx
算法
nginx由模塊組成,這些模塊由配置文件中指定的指令控制。指令分爲簡單指令和塊指令。一個簡單的指令由名稱和參數組成,用空格分隔,以分號(;)結尾。塊指令與簡單指令具備相同的結構,但它不是以分號結尾,而是以大括號({和})包圍的一組附加指令結束。若是塊指令在大括號內能夠有其餘指令,則稱爲上下文(示例: events, http, server和 location)。
http塊包含處理Web流量的指令。這些指令一般被稱爲通用指令,由於它們被傳遞給NGINX服務的全部網站配置。http中能夠配置多個server,一個server中能夠配置多個location,除了http塊、server塊和location塊以外,還有events塊、stream塊等
塊指令和簡單指令是有必定的對應關係的,好比,有些簡單指令只能在http塊中配置,有些簡單指令只能在server塊中配置,有些簡單指令只能在location塊中配置,有些簡單指令既能在server塊中配置又能在http塊中配置,能夠在官網中(http://nginx.org/en/docs/)查看指令存在的位置,而最上方不屬於任何塊的配置指令的區域屬於主配置區,用於定義網站的全局配置 vim
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { ... } http { ... server { ... location ... { ... ... } } server { ... } }
Nginx是以事件的觸發來驅動的web服務器,Nginx服務器響應和處理Web請求的過程,就是基於事件驅動模型的,事件驅動模型包括事件收集器,事件發送器,事件處理器,事件收集器負責收集全部事件,事件包括來自軟件、硬件以及用戶的,事件發送器負責將收集器收集到的事件發送到目標對象,事件處理器負責具體事件的響應,事件包括讀事件,寫事件以及異常事件。centos
爲三類事件分別建立一個事件描述符集合,分別用來收集讀事件的描述符、寫事件的描述符和異常事件的描述符,調用底層select()函數,等待事件發生。而後遍歷三個集合中的事件描述符,當檢測到事件發生時就處理該事件,select受最大文件描述符的限制數組
爲三類事件建立一個集合,最後輪詢的時候,能夠同時檢查這三種事件是否發生
把描述符列表的管理交給內核負責,一旦有某種事件發生,內核把發生事件的描述符列表通知給進程,這樣就避免了輪詢整個描述符列表。epoll庫經過相關調用通知內核建立一個待處理的事件列表,當某一事件發生後,內核將發生事件的描述符列表上報給epoll庫,獲得事件列表的epoll庫,就能夠進行事件處理了。
nginx訪問認證須要用到auth_basic模塊,此模塊使用的是HTTP Basic Authentication協議來對用戶進行訪問控制,但此模塊並不保證安全性,由於瀏覽器是以明文方式將用戶名和密碼傳給Web服務器的
語法 | auth_basic string 丨 off; |
---|---|
默認 | auth_basic off; |
應用位置 | http,server,location,limit_except |
string字符會在用戶認證的彈窗中顯示
語法 | auth_basic_user_file file; |
---|---|
默認 | - |
應用位置 | http,server,location,limit_except |
指定保存用戶名和密碼的文件
htpasswd [-cimBdpsDv] [-C cost] passwordfile username htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password htpasswd -n[imBdps] [-C cost] username htpasswd -nb[mBdps] [-C cost] username password
-c 建立密碼文件 -n 將加密後的內容輸出在屏幕上; -m 默認採用MD5算法對密碼進行加密 -d 採用CRYPT算法對密碼進行加密 -p 不對密碼文件中的密碼進行加密,即便用普通文本格式的密碼 -s 採用SHA算法對密碼進行加密 -b 命令行中一併輸入用戶名和密碼而不是根據提示輸入密碼,能夠看見明文,不須要交互 -D 從密碼文件中刪除指定的用戶
下面咱們經過auth認證來對kibana進行用戶登陸認證
location /kibana/ { auth_basic "kibana"; auth_basic_user_file /etc/nginx/kibanauser; proxy_pass http://127.0.0.1:5601/; proxy_set_header Host $host:5601; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; rewrite ^/kibana/(.*)$ /$1 break; }
htpasswd -c /etc/nginx/kibanauser admin
vim /etc/nginx/nginx.conf server { listen 80 default_server; server_name huazai.com; location / { root html/huazai; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 80; server_name wanger.com; location / { root html/wanger; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
cd /usr/share/nginx/html/ mkdir wanger mkdir huazai echo "I'm huazai" >huazai/index.html echo "I'm wanger" >wanger/index.html chmod -R 777 wanger/ chmod -R 777 huazai/ nginx -s reload
curl -xlocalhost:80 huazai.com I'm huazai curl -xlocalhost:80 wanger.com I'm wanger
server { listen 80 default_server; server_name huazai.com; location / { root html/huazai; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } server { listen 800; server_name huazai.com; location / { root html/wanger; index index.html; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
nginx -t systemctl restart nginx
[root@ html]# curl 127.0.0.1:80 I'm huazai [root@ html]# curl 127.0.0.1:800 I'm wanger
location指令的做用是根據用戶請求的URI來執行不一樣的應用。
location [=|~|~*|^~|@] uri { …… }
uri能夠是普通的字符串地址,也能夠是正則表達式,其中 ~ 和 ~ 用於正則表達式, 其餘前綴和無任何前綴都用於普通字符,而~是區分大小寫的匹配,~用於不區分大小寫的匹配,還可使用「!」對匹配進行取反,^~表示若是與特定的字符串進行匹配,那麼不在進行正則搜索, =表示精確前綴匹配,只有徹底匹配才能生效,使用徹底匹配能夠略微加快請求時間,@定義命名location區段,這些區段客戶端不能訪問,只能夠由內部產生的請求來訪問,如try_files或error_page等
location = / { return "規則A"; } location ^~ /static/ { return "規則B"; } location ^~ /static/files { return "規則C"; } location ~ .*\.(gif|jpg|png|js|css)$ { return "規則D"; } location ~* \.png$ { return "規則E"; } location /img { return "規則F"; } location / { return "規則G";
root和alias均可以定義在location模塊中,都是用來指定請求資源的真實路徑
location /wanger { root html; index index.html; } curl 127.0.0.1/wanger/index.html I'm wanger
客戶端請求http://127.0.0.1/wanger/index.html 地址時,在服務器的資源是/html/wanger/index.html,真實路徑是root加上location指定的值
location /wanger { alias html/; index index.html; }
而alias是location指定的值的別名,也就是當客戶端請求http://127.0.0.1/wanger/index.html 時,在服務器的資源時/html/index.html,真實路徑是alias的路徑,此時訪問結果以下:
curl 127.0.0.1/wanger/index.html I'm huazai
歡迎關注我的微信公衆號「沒有故事的陳師傅」