天天看點

Nginx 操作總結

安裝 nginx

下載下傳 nginx 的壓縮封包件到根目錄,官網下載下傳位址:nginx.org/download/nginx-x.xx.xx.tar.gz

yum update #更新系統軟體  
cd /  
wget nginx.org/download/nginx-1.17.2.tar.gz             

複制

Bash

Copy

解壓 tar.gz 壓縮封包件,進去 nginx-1.17.2

tar -xzvf nginx-1.17.2.tar.gz  
cd nginx-1.17.2             

複制

Bash

Copy

進入檔案夾後進行配置檢查

./configure             

複制

Bash

Copy

通過安裝前的配置檢查,發現有報錯。檢查中發現一些依賴庫沒有找到,這時候需要先安裝 nginx 的一些依賴庫

yum -y install pcre* #安裝使nginx支援rewrite  
yum -y install gcc-c++  
yum -y install zlib*  
yum -y install openssl openssl-devel             

複制

Bash

Copy

再次進行檢查操作

./configure

沒發現報錯顯示,接下來進行編譯并安裝的操作

// 檢查子產品支援
  ./configure  --prefix=/usr/local/nginx  --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-threads --user=www --group=www            

複制

Bash

Copy

這裡得特别注意下,你以後需要用到的功能子產品是否存在,不然以後添加新的包會比較麻煩。

檢視預設安裝的子產品支援

ls nginx-1.17.2

檢視 nginx 的檔案清單,可以發現裡面有一個 auto 的目錄。

在這個 auto 目錄中有一個 options 檔案,這個檔案裡面儲存的就是 nginx 編譯過程中的所有選項配置。

通過指令:

cat nginx-1.17.2/auto/options | grep YES

就可以檢視

nginx 編譯安裝時,怎麼檢視安裝子產品

編譯并安裝

make && make install             

複制

Bash

Copy

這裡需要注意,子產品的支援跟後續的 nginx 配置有關,比如 SSL,gzip 壓縮等等,編譯安裝前最好檢查需要配置的子產品存不存在。

檢視 nginx 安裝後在的目錄,可以看到已經安裝到 /usr/local/nginx 目錄了

whereis nginx  
$nginx: /usr/local/nginx             

複制

Bash

Copy

啟動 nginx 服務

cd /usr/local/nginx/sbin/  
./nginx             

複制

Bash

Copy

服務啟動的時候報錯了:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

,通過指令檢視本機網絡位址和端口等一些資訊,找到被占用的 80 端口

netstat \-ntpl

的 tcp 連接配接,并殺死程序(kill 程序 pid)

netstat -ntpl  
kill 程序PID             

複制

Bash

Copy

繼續啟動 nginx 服務,啟動成功

./nginx             

複制

Bash

Copy

在浏覽器直接通路 ip 位址,頁面出現 Welcome to Nginx! 則安裝成功。

nginx 配置

基本結構

main        # 全局配置,對全局生效  
├── events  # 配置影響 nginx 伺服器或與使用者的網絡連接配接  
├── http    # 配置代理,緩存,日志定義等絕大多數功能和第三方子產品的配置  
│   ├── upstream # 配置後端伺服器具體位址,負載均衡配置不可或缺的部分  
│   ├── server   # 配置虛拟主機的相關參數,一個 http 塊中可以有多個 server 塊  
│   ├── server  
│   │   ├── location  # server 塊可以包含多個 location 塊,location 指令用于比對 uri  
│   │   ├── location  
│   │   └── ...  
│   └── ...  
└── ...             

複制

Bash

Copy

主要配置含義

  • main:nginx 的全局配置,對全局生效。
  • events:配置影響 nginx 伺服器或與使用者的網絡連接配接。
  • http:可以嵌套多個 server,配置代理,緩存,日志定義等絕大多數功能和第三方子產品的配置。
  • server:配置虛拟主機的相關參數,一個 http 中可以有多個 server。
  • location:配置請求的路由,以及各種頁面的處理情況。
  • upstream:配置後端伺服器具體位址,負載均衡配置不可或缺的部分。

nginx.conf 配置檔案的文法規則

  1. 配置檔案由指令與指令塊構成
  2. 每條指令以 “;” 分号結尾,指令與參數間以空格符号分隔
  3. 指令塊以 {} 大括号将多條指令組織在一起
  4. include 語句允許組合多個配置檔案以提升可維護性
  5. 通過 # 符号添加注釋,提高可讀性
  6. 通過 \$ 符号使用變量
  7. 部分指令的參數支援正規表達式,例如常用的 location 指令

内置變量

nginx 常用的内置全局變量,你可以在配置中随意使用:

常用指令

這裡列舉幾個常用的指令:

nginx -s reload  # 向主程序發送信号,重新加載配置檔案,熱重新開機  
nginx -s reopen  # 重新開機 Nginx  
nginx -s stop    # 快速關閉  
nginx -s quit    # 等待工作程序處理完成後關閉  
nginx -T         # 檢視目前 Nginx 最終的配置  
nginx -t -c <配置路徑>  # 檢查配置是否有問題,如果已經在配置目錄,則不需要 -c             

複制

Bash

Copy

以上指令通過

nginx \-h

就可以檢視到,還有其它不常用這裡未列出。

Linux 系統應用管理工具 systemd 關于 nginx 的常用指令:

systemctl start nginx    # 啟動 Nginx  
systemctl stop nginx     # 停止 Nginx  
systemctl restart nginx  # 重新開機 Nginx  
systemctl reload nginx   # 重新加載 Nginx,用于修改配置後  
systemctl enable nginx   # 設定開機啟動 Nginx  
systemctl disable nginx  # 關閉開機啟動 Nginx  
systemctl status nginx   # 檢視 Nginx 運作狀态             

複制

Bash

Copy

配置 nginx 開機自啟