關於Nginx
它是一個輕量級、高性能、穩定性高、並發性好的HTTP和反向代理服務器,當我們搭建自己的應用時,通常用它作為反向代理服務器,圖片服務器和負載均衡。
1.Ubuntu 16安裝 Nginx
這里有2種方式可以安裝:
1.從互聯網中的軟件倉庫安裝
直接使用命令行:sudo apt-get install nginx

安裝好后(這里應該是幫你安裝了nginx所需要的依賴庫),測試:sudo service nginx start

可以看到默認的80端口已經處於監聽狀態了。
注:
,可以看到,自動下載的版本較低。
nginx的卸載(按順序執行):
sudo apt-get remove nginx nginx-common # 卸載刪除除了配置文件以外的所有文件。
sudo apt-get purge nginx nginx-common # 卸載所有東東,包括刪除配置文件。
sudo apt-get autoremove # 在上面命令結束后執行,主要是卸載刪除Nginx的不再被使用的依賴包。
sudo apt-get remove nginx-full nginx-common #卸載刪除兩個主要的包。
2.推薦去官網下載tar包,手動配置依賴環境(參考:https://www.cnblogs.com/EasonJim/p/7806879.html)
1.安裝gcc g++的依賴庫
sudo apt-get install build-essential(c語言的編譯環境) sudo apt-get install libtool(腳本庫)
安裝pcre依賴庫(正則表達式庫)
sudo apt-get update sudo apt-get install libpcre3 libpcre3-dev
安裝zlib依賴庫(壓縮解壓用)
sudo apt-get install zlib1g-dev
安裝SSL依賴庫(16默認已經安裝了)
sudo apt-get install openssl
如果沒有ftp可以使用rz命令上傳文件。
#解壓:
tar -zxvf nginx-1.14.2.tar.gz
#進入解壓目錄:
cd nginx-1.14.2
#編譯:
make
#安裝:
sudo make install
#啟動:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路徑,不加的話,nginx會自動加載默認路徑的配置文件,可以通過-h查看幫助命令。
nginx默認是80端口,輸入地址即可看到nginx首頁:

2.nginx基本配置
參考(https://blog.csdn.net/david_xtd/article/details/16967837)(https://carrot.is/coding/nginx_introduction)
nginx的配置文件默認位於/usr/local/nginx/conf下的nginx.conf。先看一下它的默認配置:
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } ... }
nginx.conf配置功能的格式像css,打開nginx.conf,其中重點是作為服務器的配置的server:
LISTEN:
server{ listen 80; }
listen代表服務器的監聽端口,默認是80.一般來說這個默認的端口不是必須的。
SERVER_NAME:
server_name localhost;
server_name也是一個很重要的參數,任意請求經過該nginx服務器,都會去匹配這個server_name對應的域名,並被匹配到不同的server。
比如我們可以這樣設置:
server { listen 80; server_name localhost1; location / { root html; index index1.html index1.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name localhost2; location / { root html; index index2.html index2.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
上面設置了2個server。location是路由配置,index指的是該服務器的默認首頁。
這樣設置以后,當我們本地配置了host,訪問localhost1這個域名就會進入index1.html這個主頁,訪問localhost2會訪問index2.html。
這個功能很強大,它可以讓你在一個配置文件里管理不同的站點,使用不同的域名,並給予它們不同的規則。
LOCATION:
server_name letsdoit.com; location / { root html; index index.html index.htm; } ...
location是一個對url訪問的路由,它可以配置多個,並用正則匹配。讓不同的路徑訪問到不同的應用。
比如上面的配置,location后面跟單獨的"/" ,表示該地址下任意路徑都匹配到其中的應用。訪問www.letsdoit.com/asdf 就會匹配到這個 index.html。.
location中的“root”代表資源的路徑(做圖片服務器時有用)。“index”代表默認主頁。
下面介紹一點常用的配置:(示例來源:https://blog.51cto.com/superpcm/2092317)
location = / { [ configuration A ] } #用戶請求"/"時,匹配A,例如:www.pcm.com/
location / { [ configuration B ] } #當用戶請求"/index.html"時,匹配B,例如:www.pcm.com/index.html
location /documents/ { [ configuration C ] } #當用戶請求"/documents/"時,匹配C,例如:www.pcm.com/documents/index.html
location ^~ /images/ { [ configuration D ] } #當用戶請求"/images/"時,匹配D,:www.pcm.com/images/1.jpg
location ~* \.(gif|jpg|jpeg)$ { [ configuration E ] } #當用戶請求".gif|.jpg|.jpeg"時,匹配E,例如:www.pcm.com/documents/1.jpg
#上面的反斜杠是轉義字符,$的意思是結尾
上面的是一些基本的配置,還有些重要的配置,當nginx做代理或者負載的時候會用到。
3.nginx作為圖片服務器的例子
nginx作為靜態資源服務器的性能也很強大,我們常常把它作為圖片服務器使用。
我們可以利用location的規則,把單獨的一個應用作為靜態資源訪問:
配置文件如下:
server { listen 80; server_name localhost; location /images/ { root /home/ftp/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
我們以地址/imgages/ 開頭的url作為資源訪問路徑。並通過root,做訪問路徑的映射:
這樣配置以后,在 /home/ftp/images文件夾上傳一張圖片做測試:

重啟nginx : ./usr/local/nginx/sbin/nginx -s reload
通過瀏覽器訪問,測試成功:

注意這里新手可能會出現404的情況。我之前認為root映射的路徑就是url的訪問路徑,
於是我這樣設置:
location /images/ { root /home/ftp/images/; }
但這樣訪問的路徑其實是 /home/ftp/images/images/。
配置root的話, 訪問后會在root配置的目錄后跟上URL,組成一個文件路徑。
如果不想在路徑上加上url,可以使用“alias”(參考:https://www.cnblogs.com/jiongchen/p/9139156.html)
location /images/ { alias /home/ftp/images/; }
這樣就不會拼接上url。
注:
root響應的路徑:配置的路徑(root指向的路徑)+完整訪問路徑(location的路徑)+靜態文件
alias響應的路徑:配置路徑+靜態文件(去除location中配置的路徑)
