最近看了好多關於nginx的內容 和文章,也試着用了些。想着總結一下,方便之後查閱。畢竟像這種瑣碎的知識點,不可能說在不用的狀況下還能一直牢記。
本文便是這樣而來。其中的知識點都是經常使用的——不論是初學者,仍是有些許經驗的開發者。可是筆者實在不認爲它適合「零基礎小白」——由於筆者不以爲能比文檔之類的更能把基礎知識點講明白。javascript
nginx環境
「nginx是一款輕量級HTTP服務器,採用事件驅動的異步非阻塞處理方式的框架。這讓其具備極好的IO性能,時經常使用於服務端的反向代理和負載均衡。」php
nginx優勢 :支持海量高併發(採用IO多路複用epoll)、內存消耗少、配置簡單、免費可商業化使用。css
必要程序的安裝 —— Linux環境:html
yum -y install gcc gcc-c++ autoconf pcre-devel make automake yum -y install wget httpd-tools vim
新建yum源:前端
vim /etc/yum.repos.d/nginx.repo
進入後:java
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch #筆者使用的是centos7.x版本 gpgcheck=0 enabled=1
保存並退出
而後咱們來執行如下命令:(查看yum是否已存在)node
yum list | grep nginx
nginx配置文件
查看nginx安裝目錄:nginx
rpm -ql nginx
- rpm:Linux的rpm包管理工具
- q選項:詢問模式
- l選項:返回列表展現
我將nginx.conf和conf.d文件夾下的default.conf兩個配置文件幾乎作了詳細備註,列出以下:web
//nginx.conf #運行用戶,默認便是nginx,能夠不進行設置 user nginx; #Nginx進程,通常設置爲和CPU核數同樣 worker_processes 1; #錯誤日誌存放目錄 error_log /var/log/nginx/error.log warn; #進程pid存放位置 pid /var/run/nginx.pid; events { worker_connections 1024; # 單個後臺進程的最大併發數 } http { include /etc/nginx/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 /var/log/nginx/access.log main; #nginx訪問日誌存放位置 sendfile on; #開啓高效傳輸模式 #tcp_nopush on; #減小網絡報文段的數量 keepalive_timeout 65; #保持鏈接的時間,也叫超時時間 #gzip on; #開啓gzip壓縮 include /etc/nginx/conf.d/*.conf; #包含的子配置項位置和文件
//default.conf server { listen 8081; #配置監聽端口 server_name localhost; //配置域名 #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; #服務默認啓動目錄 index index.html index.htm; #默認訪問文件 } #error_page 404 /404.html; # 配置404頁面 # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; #錯誤狀態碼的顯示頁面,配置後須要重啓 location = /50x.html { root /usr/share/nginx/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; #} }
nginx服務啓動、中止、重啓
啓動: 兩種方式正則表達式
nginx
systemctl start nginx.service
中止:
- 從容中止:
nginx -s quit
- 當即中止:
nginx -s stop
- 暴力中止:
killall nginx
systemctl stop nginx.service
重啓:
systemctl restart nginx.service
查看服務器狀態:
ps aux | grep nginx
相關實踐:
- 更改配置文件後重載:
nginx -s reload
(不用重啓服務器) - 查看端口號佔用狀況:
netstat -tlnp
自定義錯誤頁 & 訪問設置
在/etc/nginx/conf.d/default.conf
下能夠看到一句話:
error_page 500 502 503 504 /50x.html;
其表示在http錯誤碼爲500、50二、50三、504時,轉向網站根目錄下的/50.html
進行處理。
一樣的還有如:
error_page 404 http://cjxnsb.cn/mxcf/index.html
(
設置錯誤頁:這個功能是web中很是實用的功能之一,它能夠減小「不恰當的」請求。
這開發者在後端中也能夠設置——下面以node服務器爲例演示:
const express=require('express'); const path=require('path'); var app=express(); app.use('/index',function(req,res){ res.status(200).sendFile(path.join(__dirname,'www','index.html')) }) //1.html——默認的訪問頁面(靜態目錄) app.use(express.static(path.join(__dirname,'www','1.html'))) app.use('*',function(req,res){ res.status(200).sendFile(path.join(__dirname,'www','err','404.html')) }) app.listen(8081,function(err){ if(err){ console.log('監聽失敗'); throe err; } })
)
一樣的,咱們還能夠用來優雅地設置 301重定向:
好比
location /original-page{ add_header Cache-Control no-store; # 永久重定向(301) rewrite ^/original-page http://cjxnsb.cn/mxcf/index.html permanent; # 臨時重定向(302) # rewrite ^/original-page http://cjxnsb.cn/mxcf/index.html redirect; }
(增長add_header 字段的用意在於:設置瀏覽器不要緩存301資源)
有時咱們的服務器只容許特定主機訪問 —— 好比內部OA系統,這時咱們就須要控制一些IP訪問。
咱們能夠直接在location裏配置:(隸屬於文件default.conf)
nginx權限是自上而下的,並且只要上面的執行成功,則下面的具備類似功能的就再也不執行了!
location /{ deny IP地址; #禁止訪問IP allow IP地址; #容許訪問IP }
若用IP地址組,需加「/」:deny 182.116.193.67/200;
nginx訪問權限處理
location中設置的權限是「從上到下」執行的。好比在deny中設置了all,則就再也不執行allow選項了。
在工做中,訪問權限的控制需求很是複雜,例如,對於網站下的img(圖片目錄)是容許全部用戶訪問,但對於網站下的admin目錄則只容許公司內部固定IP訪問。這時候僅靠deny和allow這兩個指令,是沒法實現的。咱們須要location塊來完成相關的需求匹配。
精確匹配符「=」的使用:
location =/img{ allow all; } location =/admin{ deny all; }
正則表達式的使用——「~」
好比:以「php」結尾的文件都禁止外部用戶訪問:
location ~\.php${ deny:all; }
含有mxc的文件都容許訪問:
location ~*mxc{ allow all; }
nginx配置「虛擬主機」
經常使用的有「基於端口號的、基於IP的、基於域名的」,這裏咱們看下基於端口號設置——其原理就是:nginx監聽多個端口,根據不一樣端口號,區分不一樣網站。
咱們固然能夠在主文件 etc/nginx/nginx.conf
或子配置文件etc/nginx/conf.d/default.conf
裏設置,或者在conf.d文件夾下再建一個文件。而咱們要作的,是修改(添加)server選項:如
server{ listen 8081; server_name localhost; root /usr/share/nginx/html/html8081; index index.html; }
咱們在/usr/share/nginx/html/html8081/
目錄下編寫index.html 便可訪問查看結果。
基於IP的配置和基於端口號的配置基本無二,只是把server_name值設置成IP地址(如127.0.0.1的形式)便可。
nginx使用域名設置虛擬主機
在實際中,咱們實際上用的是「域名」的方式設置。
通常來講,你多是在阿里雲上買的域名(推薦)。點擊「域名」後,你會看到你買過的域名,而後點擊「解析」,便可獲得(二級)域名。好比:
筆者用nginx.mxcfq.top
這個域名映射到默認的nginx首頁位置,用nginx2.mxcfq.top
映射到上面設置的8001端口位置。
咱們去vim etc/nginx/conf.d/default.conf
文件:修改以下
listen 80; server_name nginx.mxcfq.top;
再到vim etc/nginx/conf.d/8001.conf
:修改以下
server{ listen 80; server_name nginx2.mxcfq.top; root /usr/share/nginx/html/html8081; index index.html; }
重啓後,分別訪問上面兩個server_name,便可查看結果。
nginx反向代理
反向代理跟代理正好相反(須要說明的是,如今基本全部的大型網站的頁面都是用了反向代理),客戶端發送的請求,想要訪問server服務器上的內容。發送的內容被髮送到代理服務器上,這個代理服務器再把請求發送到本身設置好的內部服務器上,而用戶真實想得到的內容就在這些設置好的服務器上。
好比:如今咱們要訪問http://nginx2.mxcfq.top
而後反向代理到jspang.com
這個網站。咱們直接到etc/nginx/conf.d/8001.conf
配置文件裏進行修改。
server{ listen 80; server_name nginx2.mxcfq.top; location / { proxy_pass http://cjxnsb.cn; } }
(通常咱們反向代理的都是一個IP,可是我這裏代理了一個域名也是能夠的。其實這時候咱們反向代理就算成功了,咱們能夠在瀏覽器中打開http://nginx2.jspang.com
來測試一下)
nginx開啓壓縮gzip
打開nginx.conf配置文件:
http{ //... gzip on; gzip_proxied any; # nginx作前端代理時,無條件啓用壓縮 gzip_min_length 要壓縮頁面的最小大小; gzip_comp_level 壓縮級別; # 設置壓縮級別,最大爲9,最小爲1,此值越大,壓縮時間越短 gzip_types text/plain text/css application/x-javascript application/javascript application/xml; gzip_static on; gzip_http_version 1.0; add_header Vary Accept-Encoding gzip; #代替指令gzip_vary on;,用於在使用gzip功能時發送帶有Vary:Accept-Encoding頭域的響應頭部——這對於自己不支持gzip壓縮的客戶端瀏覽器是有用的 //... server{ //... } }
針對IE6上gzip開啓問題,並且如今IE6幾乎「無人問津」的狀況,筆者認爲,咱們能夠放棄IE6:
gzip_disable "MSIE [1-6]\.";
nginx負載均衡
當一個域名指向多臺web服務器,添加一臺nginx負載均衡服務器,將客戶端請求「平均」發送給每臺web服務器,避免單臺服務器因負載太高而其他服務器空閒而出現的損失
新建配置文件:
vi /etc/nginx/conf.d/test.conf
upstream test{ #有多個文件,此處配置多個(test——自定義名) ip_hash; server 192.168.0.1:80 weight=100; server 192.168.0.2:80 weight=50; } server{ listen 80; server_name cjxnsb.cn; location /{ proxy_pass http://test; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
nginx適配pc & 移動端
這個功能曾經很是常見:用兩套代碼分別在pc端和移動端時使用。 —— 固然,如今常用的方案是「用兩套css樣式代碼」(或者採用自適應方案)!
好比,
- 咱們在
/usr/share/nginx/
目錄下新建兩個文件夾,分別爲:pc和mobile
cd /usr/share/nginx mkdir pc mkdir mobile
-
而後在其中分別加入咱們寫的適用於PC端和移動端的html文件:index.html
-
進入
etc/nginx/conf.d
目錄,修改端口號.conf
文件,改成下面的形式:
server{ listen 80; server_name cjxnsb.cn; location / { root /usr/share/nginx/pc; if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') { root /usr/share/nginx/mobile; } index index.html; } }
nginx使用autoindex
如圖,有時候一個nginx服務就是爲了用來下載文件的,網上不少下載服務都是這樣的:
這個很重要,也很簡單:
在http段加上如下參數,重啓nginx就行
autoindex on; autoindex_exact_size off; autoindex_localtime on; charset utf-8;
哦對了,修改完配置,不要忘了重啓服務器!
本文同步分享在 博客「行舟客」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。