一文帶你瞭解nginx基礎
阿新 • • 發佈:2020-05-27
學習nginx,就要先了解什麼是nginx,為什麼使用nginx,最後才是瞭解怎麼使用nginx
- [nginx簡介](#profile)
- [nginx安裝](#install)
- [一、Linux中安裝nginx](#linux)
- [二、Docker中安裝nginx](#docker)
- [三、檔案目錄](#file)
- [nginx常用功能和配置](#function)
- [一、限流](#limit)
- [二、安全配置](#security)
- [三、程序數、併發數、系統優化](#optimize)
- [四、GZIP](#gzip)
- [五、狀態監控](#status)
- [六、負載均衡](#balance)
- [七、access日誌切割](#log)
- [八、動靜分離](#sepa)
- [nginx.conf基礎配置項](#conf)
免費開源的nginx能夠在眾多同類產品中脫穎而出,是因為它具備低記憶體、高併發的優勢,且配置簡單,支援URL重寫、GZIP,內建健康檢查,能自動檢測叢集伺服器狀態,跳過宕機伺服器。 # 為 nginx 指定一個配置檔案
/usr/local/nginx/sbin/nginx -t # 不執行,僅測試配置檔案正確性
/usr/local/nginx/sbin/nginx -v # 顯示nginx版本
/usr/local/nginx/sbin/nginx -V # 顯示 nginx 的版本,編譯器版本和配置引數
netstat -anput | grep nginx # 檢測 nginx 執行狀態(如無任何資訊顯示則是未執行)
```
###### 4.配置開機啟動
關機重啟了你不會想自己動手再啟動一遍nginx吧?
- 新建 nginx.service 檔案
```
vim /usr/lib/systemd/system/nginx.service
```
- 然後在檔案中新增以下配置
- 配置詳情參考:[https://www.cnblogs.com/yyxianren/articles/10677332.html](https://www.cnblogs.com/yyxianren/articles/10677332.html)
```
# 服務說明項
[Unit]
# 描述
Description=nginx
# 在哪些服務啟動之後啟動該服務,多個服務用空格隔開
After=network.target
# 在哪些服務啟動之前啟動該服務
# Before=xxx
# 弱依賴,如果xxx服務啟動失敗或停止執行,不影響該服務
# Wants=xxx
# 強依賴,如果xxx服務啟動失敗或停止執行,該服務也必須退出
# Requires=xxx
# 執行引數設定
[Service]
# 啟動型別
# simple(預設值):ExecStart欄位啟動的程序為主程序
# forking:ExecStart欄位將以fork()方式啟動,此時父程序將會退出,子程序將成為主程序
# oneshot:類似於simple,但只執行一次,Systemd 會等它執行完,才啟動其他服務
# dbus:類似於simple,但會等待 D-Bus 訊號後啟動
# notify:類似於simple,啟動結束後會發出通知訊號,然後 Systemd 再啟動其他服務
# idle:類似於simple,但是要等到其他任務都執行完,才會啟動該服務。一種使用場合是為讓該服務的輸出,不與其他服務的輸出相混合
Type=forking
# Start命令
ExecStart=/usr/local/nginx/sbin/nginx
# Reload命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# Stop命令
ExecStop=/usr/local/nginx/sbin/nginx -s quit
# 是否給服務分配獨立的臨時空間
PrivateTmp=true
# 配置開機啟動方式
[Install]
# 該服務所在的 Target,Target的含義是服務組,表示一組服務
# multi-user.target 預設被配置為開機啟動
WantedBy=multi-user.target
```
- 純淨版(去除註釋)
```
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
```
- 最後啟動服務並重啟機器進行測試
```
systemctl enable nginx.service # 啟用 nginx.service 開機啟動
reboot # 重啟電腦
## 其他命令
systemctl disable nginx.service # 禁止 nginx.service 開機啟動
systemctl stop nginx.service # 停止 nginx.service
systemctl start nginx.service # 啟動 nginx.service
systemctl restart nginx.service # 重啟 nginx.service
systemctl list-units --type=service # 檢視所有已啟動的服務
```
- 檢查 nginx.service 執行狀況
```
systemctl status nginx.service
```
- 檢查nginx執行狀況
```
netstat -anput | grep nginx
```
###### 下載映象
```
# 搜尋映象
docker search nginx
# 拉取映象
docker pull nginx
```
###### 建立並啟動容器
- `-d`:後臺執行
```
docker run -d -it -p 8080:80 --name nginx8080 nginx
```
###### 訪問nginx
```
curl :8080
```
---
###### 微服務分散式部署前期準備
為了方便修改nginx配置,需要將容器內的nginx相關檔案拷貝到主機中,並將其對映到容器中去,這樣,修改起來就方便多了
- 拷貝nginx檔案到本地
```
docker cp nginx8080:/etc/nginx ~/nginx
```
- 上面步驟建立的nginx8080已經沒用了,刪除即可
```
docker stop nginx8080
docker rm nginx8080
```
###### 建立掛載本地目錄的nginx8080容器
- 掛載:即主機上指定目錄與容器內指定目錄檔案共享
- `-v ~/nginx:/etc/nginx`:將本地~/nginx檔案對映到容器/etc/nginx,方便修改配置
- `-v ~/nginx/log:/var/log/nginx`:將本地~/nginx/log對映到容器/var/log/nginx,方便檢視日誌
- 我的環境:apidemo1、apidemo1、nginx8080都在同一docker中
- 方法一:使用 link 建立容器間通訊
- `--link=apidemo1:demo1`:實現nginx8080容器到apidemo1容器的單向通訊,併為apidemo1起別名為demo1,nginx8080內部可直接使用demo1訪問apidemo1
- `--link=apidemo2:demo2`:實現nginx8080容器到apidemo2容器的單向通訊,併為apidemo2起別名為demo2,nginx8080內部可直接使用demo2訪問apidemo2
```
# apidemo1與apidemo2是同一應用的不同分發
docker run -d -it -p 8080:80 --name nginx8080 -v ~/nginx:/etc/nginx -v ~/nginx/log:/var/log/nginx --link=apidemo1:demo1 --link=apidemo2:demo2 nginx
```
- 修改nginx.conf
```
vim ~/nginx/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://apiserver/api/;
}
}
upstream apiserver {
server demo1 weight=1;
server demo2 weight=1;
}
```
- 方法二:使用 docker bridge 建立容器間通訊
- `docker network ls`:檢視docker網路資訊,其中,我們建立的容器預設使用bridge(橋接)
- `docker network inspect bridge`:檢視bridge的詳細資訊以及哪些容器使用了該網路型別,我這邊的ip地址是
- apidemo1:172.17.0.2
- apidemo2:172.17.0.3
```
docker run -d -it -p 8080:80 --name nginx8080 -v ~/nginx:/etc/nginx -v ~/nginx/log:/var/log/nginx nginx
```
- 修改nginx.conf
```
vim ~/nginx/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://apiserver/api/;
}
}
upstream apiserver {
server 172.17.0.2 weight=1;
server 172.17.0.3 weight=1;
}
```
###### 為了應用新nginx.conf,重啟nginx8080
```
docker restart nginx8080
```
###### 測試
```
// 這裡的路徑按照自己的來
curl :8080/api/values
```
###### 可能出現的問題
1. Http staus code——502:
解決:檢視日誌可以看到“113: No route to Host”,嘗試關閉docker所在linux機器的防火牆,發現可以訪問了,可以確定是防火牆的問題。但是把防火牆關閉太不安全了,所以我們選擇開啟防火牆,然後執行以下命令
```
# 信任 docker0 服務連線
nmcli connection modify docker0 connection.zone trusted
# 停止 NetworkManager 服務(該服務用語監測網路、自動連線網路)
systemctl stop NetworkManager.service
# 將docker0網路介面設定為內部區域(永久)
firewall-cmd --permanent --zone=trusted --change-interface=docker0
# 啟動 NetworkManager 服務
systemctl start NetworkManager.service
# 信任 docker0 服務連線
nmcli connection modify docker0 connection.zone trusted
# 重啟 docker
systemctl restart docker.service
```
- *_temp:共5個temp結尾的目錄,用於存放nginx執行時產生的臨時檔案
- conf:存放配置檔案的目錄,包含主配置檔案nginx.conf
- html:存放了nginx的錯誤頁面和歡迎頁面
- logs:存放了訪問日誌和錯誤日誌
- sbin:存放了nginx的二進位制命令,常用於nginx服務的啟動、停止等管理工作
rate+burst 時,立即返回503;如果沒設定,則會按照rate排隊等待處理
# 該語句還可直接放置到http模組下,這樣下屬的server都應用該配置
# 該語句還可放置到server中的location模組中,這樣僅指定的location應用該配置
limit_req zone=perip burst=2 nodelay;
}
}
```
- 測試:`ab -c 1 -n 3 http://localhost/`
- 3個請求全部成功,因為正在處理的請求數1加上快取數2,沒超過限制
```
Server Software: nginx/1.18.0
Server Hostname: 192.168.159.128
Server Port: 80
Document Path: /
Document Length: 612 bytes
Concurrency Level: 1
Time taken for tests: 0.001 seconds
Complete requests: 3
Failed requests: 0
Total transferred: 2535 bytes
HTML transferred: 1836 bytes
Requests per second: 2439.02 [#/sec] (mean)
Time per request: 0.410 [ms] (mean)
Time per request: 0.410 [ms] (mean, across all concurrent requests)
Transfer rate: 2012.67 [Kbytes/sec] received
```
- 測試:`ab -c 3 -n 4 http://localhost/`
- 3個請求成功,1個請求失敗,因為正在處理的請求數1加上快取數2,另外1條請求失敗
```
erver Software: nginx/1.18.0
Server Hostname: 192.168.159.128
Server Port: 80
Document Path: /
Document Length: 612 bytes
Concurrency Level: 1
Time taken for tests: 0.002 seconds
Complete requests: 4
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Non-2xx responses: 1
Total transferred: 3223 bytes
HTML transferred: 2330 bytes
Requests per second: 2504.70 [#/sec] (mean)
Time per request: 0.399 [ms] (mean)
Time per request: 0.399 [ms] (mean, across all concurrent requests)
Transfer rate: 1970.86 [Kbytes/sec] received
```
###### [ngx_http_upstream_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html)
```
upstream MyName {
server 192.168.0.1:8080 weight=1 max_conns=10;
server 192.168.0.2:8080 weight=1 max_conns=10;
}
```
nginx簡介
nginx是一個高效能的HTTP和反向代理Web伺服器,還支援正向代理、透明代理、負載均衡、HTTP快取等功能。nginx始於2004年10月4日,使用C語言編寫,2013年4月24日,nginx釋出了v1.4.0穩定版,建議大家使用的版本高於此版本。免費開源的nginx能夠在眾多同類產品中脫穎而出,是因為它具備低記憶體、高併發的優勢,且配置簡單,支援URL重寫、GZIP,內建健康檢查,能自動檢測叢集伺服器狀態,跳過宕機伺服器。