linux基礎之nginx 詳述01
阿新 • • 發佈:2022-01-05
目錄
Nginx
1、什麼是web服務
web就是B/S架構:“Browser/Server”的縮寫,即“瀏覽器/伺服器”模式。
2、web伺服器軟體
2.1 apache
apache : httpd,最早期使用的web服務,效能不高,操作難。 網路模型: select : 效率低(windows只支援這個模型) poll epoll
2.2 Nginx
Nginx是一個開源且高效能、可靠的http web服務、代理服務。
Nginx如果部署在Windows裡面會使用select模型,如果部署在linux裡面就使用epoll模型。
官網:https://nginx.org/
軟體:https://nginx.org/download/
3、部署Nginx(客戶端)
3.1 yum安裝
使用官方的源,配置簡單,適合小白 [root@web01 ~]# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [root@web01 ~]# yum install nginx -y [root@web01 ~]# systemctl stop httpd [root@web01 ~]# systemctl start nginx # 檢查:在瀏覽器訪問192.168.15.7 (該ip為部署好的虛擬機器公網ip),如果出現welcome to nginx!則為成功部署。
3.2 二進位制安裝
二進位制包裡面包括了已經編譯完成,可以直接執行的程式。你通過sudo apt-get install來進行下載和解包(安裝),執行完該指令後就可以馬上使用了。因此這種方式簡單快捷,適合比較固定、無需改動的程式。
3.3 編譯安裝
在客戶端安裝,不建議小白,容易出錯 1.下載原始碼包 [root@web01 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz 2.解壓 [root@web01 ~]# tar -xf nginx-1.20.2.tar.gz 3.cd 到資料夾 [root@web01 nginx-1.20.2]# cd nginx-1.20.2 4.做系統設定 [root@web01 nginx-1.20.2]# ./configure 5.安裝 [root@web01 nginx-1.20.2]# make [root@web01 nginx-1.20.2]# make install
4、平滑增加Nginx模組
一般情況下不會需要增加,企業裡面需要的模組都已經裝了。增加模組必須重新編譯,把編譯過的目錄刪掉然後重新編譯。
# 在web02進行測試
# 把web01的檔案scp到wenb02,然後快速部署一下
# 用./config --help檢視所有的引數
# 安裝過程中如果報錯沒有庫,就直接yum安裝
[root@web02 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
[root@web02 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web02 ~]# cd nginx-1.20.2
[root@web02 nginx-1.20.2]#./configure --with-http_ssl_module
[root@web02 nginx-1.20.2]# yum install openssl openssl-devel -y
[root@web02 nginx-1.20.2]#./configure --with-http_ssl_module
[root@web02 nginx-1.20.2]# make
[root@web02 nginx-1.20.2]# make install
[root@web02 nginx-1.20.2]# cd /usr/local/nginx/
[root@web02 nginx]# /usr/local/nginx/sbin/nginx -v
[root@web02 nginx]# /usr/local/nginx/sbin/nginx -V
5、nginx命令(引數)
1.nginx -h :列印nginx所有的引數命令
2.-v : 列印版本號
[root@web01 ~]# nginx -v
nginx version: nginx/1.20.2
3.-V : 列印版本號和配置項
[root@web01 ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx
4.-t : 檢查配置檔案是否正確
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5.-T : 測試配置檔案並啟動
6.-q :列印錯誤日誌
7.-s : 操作程序
stop :停止
quit :退出
reopen :重啟(關機之後再啟動)
reload :過載(重新讀取配置檔案)
8.-p : 指定nginx的工作目錄
9.-e : 指定錯誤日誌路徑
10.-c : 指定配置檔案的路徑
11.-g : 設定一個全域性的Nginx配置項
[root@web01 ~]# nginx -g 'daemon off;'
6、nginx配置檔案
6.1 全域性配置
# 切換路徑,編輯配置檔案,配置檔案修改後都需要重啟nginx才會生效
[root@web03 ~]# cd /etc/nginx
[root@web03 nginx]# ll
[root@web03 nginx]# vim /etc/nginx/nginx.conf
1.user : 指定Nginx的啟動使用者
# 在配置檔案user改為www,重啟nginx,執行ps -ef | grep niginx檢視當前啟動使用者為www
[root@web03 nginx]# vim etc/nginx/nginx.conf
user nginx 修改為 user www
[root@web03 nginx]# systemctl restart nginx
[root@web03 nginx]# ps -ef|grep nginx
2.worker_processes : 定義Nginx的worker程序數
auto == CPU數量 :auto等價於cpu的數量
# 可以在配置檔案修改數量後重啟nginx並檢視
[root@web03 nginx]# vim etc/nginx/nginx.conf
worker_processes auto 修改為 worker_processes 10
[root@web03 nginx]# systemctl restart nginx
[root@web03 nginx]# ps -ef|grep nginx
3.error_log : 錯誤日誌路徑
error_log /var/log/nginx/error.log notice
4.pid : pid程序號的存放檔案路徑
pid /var/run/nginx.pid
6.2 模組配置
6.2.1 events 模組配置
1. worker_connections :每一個worker程序最多同時接入多少個請求
worker_connections 1024 :每一個worker程序最多同時接入1024個請求
2. use : 指定Nginx的網路模型(# linux預設使用epoll,即檔案中預設存在 use epoll; 不需要手動配置)
6.2.2 http 模組配置( web服務的模組)
1. include : 載入外部的配置項,降低當前配置檔案的複雜度(# /etc/nginx/mime.types也是一個配置檔案)
include /etc/nginx/mime.types
2. default_type : 如果找不到檔案的型別,則按照指定預設型別處理
default_type application/octet-stream
3. log_format : 定義日誌格式
# 新增一個json格式,註釋掉源格式,然後重啟nginx,複製一個新會話監控日誌,在瀏覽器訪問該ip,然後檢視新增日誌資訊已經換成jason格式了。json格式日誌在企業中使用比較多。
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"service":"nginxTest",'
'"trace":"$upstream_http_ctx_transaction_id",'
'"log":"log",'
'"clientip":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
access_log /var/log/nginx/access.log json ;
# access_log /var/log/nginx/access.log main;
4. sendfile : 高效讀取檔案
sendfile on
5. keepalive_timeout : 長連結保持連線的秒數(HTTP 1.0版本:短連結; HTTP 1.1版本:長連線)
keepalive_timeout 65
6. /etc/nginx/conf.d/*.conf配置
server : 網址模組:每個server都生成一個網站服務
listen : 監聽的埠
server_name : 定義域名
location : 訪問路徑
root : 指定網址路徑
index : 指定網址的索引檔案(預設訪問這個檔案)
7、搭建網頁遊戲
1.上傳程式碼並配置許可權
[root@web03 ~]# cd /opt/
[root@web03 opt]# mkdir Super_Marie
[root@web03 opt]# ll
[root@web03 opt]# cd Super_Marie/
[root@web03 Super_Marie]# 上傳程式碼壓縮包
[root@web03 Super_Marie]# 解壓壓縮包
[root@web03 html5-mario]# cd /etc/nginx/conf.d/
[root@web03 conf.d]# groupadd www -g 666
[root@web03 conf.d]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
[root@web03 conf.d]# chown -R www.www /opt/*
[root@web03 conf.d]# chown -R www.www /opt/Super_Marie/*
[root@web03 conf.d]# chown -R www.www /etc/nginx/conf.d/*
2.編輯配置檔案
[root@web03 conf.d]# vim /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name game.test.com;
location / {
root /opt/Super_Marie;
index index.html;
}
}
# 注意: root後跟的目錄需要可以直接訪問到遊戲的程式碼檔案,而不能是包含程式碼檔案的資料夾
3.測試配置檔案是否正常
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
4.重啟Nginx
[root@web03 conf.d]# systemctl restart nginx
5.域名解析
C:\Windows\System32\drivers\etc\hosts
修改為:
172.16.1.9 game.test.com