1.nginx
Nginx (engine x) 是一個高性能的HTTP和反向代理web服務器,Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,在BSD-like 協議下發行(摘自百度百科)
優點
| 優點 | 說明 |
|---|---|
| 高并發 | 基于 epoll/kqueue 模型開發,能夠支持高達 50,000 個并發連接數的回應 |
| 記憶體開銷小 | Nginx采用C進行撰寫,不論是系統資源開銷還是CPU使用效率都比 Perlbal 要好很多 |
| 簡單穩定 | 配置簡單(一個conf檔案),運行簡單(nginx命令),而且運行穩定 |
| 模塊化程度高 | 支持插件化配置 |
| 低成本 | nginx支持負載均衡功能,而且該軟體是免費開源的 |
缺點
| 缺點 | 說明 |
|---|---|
| 動態處理差 | nginx善于處理靜態檔案,處理動態頁面的能力相對于Apache等重量級web軟體稍弱 |
2.安裝和命令
Ubuntu安裝
apt-get install nginx -y
常用命令
# 啟動/停止/重啟 ngix
sudo systemctl [start|stop|reload] nginx
nginx -V # 查看版本和其他資訊
nginx -v # 查看版本
nginx -c /etc/nginx/nginx.conf # 使用組態檔
nginx -s [stop|reload] # [關閉|重啟]
nginx -t # 可以使用此命令判斷組態檔是否正確
3.配置
3.1 nginx默認目錄
| 目錄 | 說明 |
|---|---|
| /etc/nginx | 作業目錄 |
| /usr/sbin/nginx | 執行檔案 |
| /var/log/nginx | 日志檔案 |
| /var/www/html/ | web目錄 |
3.2 nginx默認組態檔
| 目錄 | 說明 |
|---|---|
| /etc/nginx/nginx.conf | 默認檔案(全域配置) |
| /etc/nginx/conf.d | 存放自定義的conf檔案 |
| /etc/nginx/{sites-available/sites-enabled/conf.d} | 其他目錄(存放第三方應用的組態檔) |
3.3 配置引數
一個組態檔里主要有全域配置段、http配置段、server配置段、location配置段
server配置
新建一個conf組態檔,里面主要配置server,一個server代表一個服務
server {
# 設定要監聽的ip、埠
listen 8000;
# 當一個主機存在多個網站時,可以通過Server_name指定域名,例如server_name www.example.com;
server_name www.mysite.com;
# 指定Server相應請求的HTML檔案所在路徑
root /var/www/html;
# 定義相應請求后回傳的檔案名或格式
index index.html index.htm index.nginx-debian.html;
}
上面的listen是指監聽的ip和port
| 形式 | 說明 | 舉例 |
|---|---|---|
| IP:Port | 精確指定IP和埠 | listten 192.168.205.153:8080 |
| IP | 只指定IP,默認監聽80埠 | listen 192.168.205.153 |
| Port | 只指定埠,監聽所有IP | listen 8080 listen 0.0.0.0:8080 |
| default_server | 默認地址,只能有一個server使用此關鍵字 |
Location配置
匹配規則(數字越小,優先級越高)
| 型別 | 含義 | 優先級 | 樣式 |
|---|---|---|---|
| =/路徑 | 精確匹配 | 1 | location = /image {} |
| ^~ | 優先匹配 | 2 | location ^~ /page {} |
| ~ 或 !~ | 正則敏感或不敏感 | 3 | location ~ .(jpe?g)$ {} |
| ~*或 !~ | 正則不敏感或敏感 | 3 | location ~* .(jpe?g)$ {} |
| / | 通用匹配 | 4 | location / {} |
| @ | 內部重定向 | location @name {} |
location匹配規律
(1)、多個location匹配字符都能匹配成功的情況下,選擇優先級最高的location規則去處理
(2)、多個locaiton匹配字符都能匹配成功,且他們的優先級一樣的情況下,選擇先定義的location規則去處理(優先上面的)
3.4 配置舉例
新建/etc/nginx/conf.d/8080.conf并編輯如下:
server {
listen 8080;
server_name www.meiduo.site;
location / {
root /data/front_end_pc/;
index index.html;
try_files $uri $uri/ =404;
}
}
新建/etc/nginx/conf.d/8081.conf并編輯如下:
server {
listen 8081;
server_name www.meiduo.site;
location / {
root /data/meiduo_mall_admin/;
index index.html;
try_files $uri $uri/ =404;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/164399.html
標籤:其他
