一、Nginx介紹
1.1 概述
Nginx是一個開源且高性能、可靠的http web服務、代理服務,
-
開源:直接獲取源代碼
-
高性能:支持海量并發
-
可靠:服務穩定
1.2 Nginx特點
-
高性能,高并發
Nginx支持很高的并發,Nginx在處理大量并發的情況下比其他web服務要快
-
輕量且高擴展性
- 輕量:功能模塊少,只保留核心模塊,其他代碼模塊化 (易讀,便于二次開發,對于開發人員非常友好)
- 高擴展性:需要什么模塊再安裝模塊,不需要全部安裝,并且還支持第三方模塊
-
高可靠性
只要不過分幾乎不會出現問題,其他的web服務需要每隔一段時間進行重啟,Nginx不需要;Nginx的宕機時間,是99999級別
-
支持熱部署
Nginx可以在運行期間,更新迭代,代碼部署,
-
大多公司都在用Nginx
- Nginx技術成熟,具備的功能是企業最常使用而且最需要的;
- 適合當前主流架構趨勢, 微服務、云架構、中間層;
- 統一技術堆疊, 降低維護成本, 降低技術更新成本;
-
Nginx使用的是Epoll網路模型
- Select: 當用戶發起一次請求,select模型就會進行一次遍歷掃描,從而導致性能低下,
- Epoll: 當用戶發起一次請求,epoll模型會直接進行處理,效率高效,并無連接限制,
-
Nginx應用場景

1.3 其他web服務
- apache:httpd,最早期使用的web服務,性能不高,操作難
- nginx
tengine:Tengine是由淘寶網發起的Web服務器專案,它在Nginx的基礎上,針對大訪問量網站的需求,添加了很多高級功能和特性
openresty-nginx:OpenResty 是一個基于 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項,用于方便地搭建能夠處理超高并發、擴展性極高的動態 Web 應用、Web 服務和動態網關, - IIS:windows下的web服務
- lighttpd:是一個德國人領導的開源 Web 服務器軟體,其根本的目的是提供一個專門針對高性能網站,安全、快速、兼容性好并且靈活的 Web Server 環境,具有非常低的記憶體開銷,CPU 占用率低,效能好,以及豐富的模塊等特點,
- GWS:google web server
- BWS:baidu web server
Tomcat
Resin
weblogic
Jboss
……
二、Nginx安裝
2.1 安裝方式
1.epol源安裝 web01
2.官方源安裝 web03
3.原始碼包安裝 web03
#三種安裝方式都需要安裝nginx運行的依賴環境
yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
2.2 epol源安裝
[root@web01 ~]# yum install -y nginx
2.3 官方源安裝
配置官方源:
#配置官方源:
[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
#下載:
[root@web02 ~]# yum install -y nginx
2.4 原始碼包安裝
-
下載安裝包
[root@web03 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz -
解壓原始碼包
[root@web03 ~]# tar -xf nginx-1.20.2.tar.gz -
配置安裝的環境(運行用戶,安裝目錄等)
1.創建用戶和組,且不創建用戶的家目錄 [root@web03 ~]# groupadd www -g 666 [root@web03 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M 2.創建一個安裝目錄 公司不指定安裝目錄時,默認安裝到/usr/local/軟體名/ 公司指定的話,就要按照公司要求來 -
生成Makefile
[root@web03 ~/nginx-1.20.2]# cd nginx-1.20.2/ [root@web03 ~/nginx-1.20.2]# ./configure --prefix=/usr/local/nginx-1.20.2 --user=www --group=www --without-http_gzip_module -
編譯安裝,結束后去安裝目錄查看安裝結果
1.編譯安裝 [root@web03 nginx-1.20.2]# make && make install 2.查看安裝結果 [root@web03 /usr/local]# cd /usr/local/nginx-1.20.2/ [root@web03 /usr/local/nginx-1.20.2]# ll -
做軟鏈接
[root@web03 /usr/local/nginx-1.20.2]# ln -s /usr/local/nginx-nginx-1.20.2 /usr/local/nginx -
配置環境變數
[root@web03 ~]# vim /etc/profile.d/nginx.sh export PATH=$PATH:/usr/local/nginx/sbin ~ [root@web03 ~]# source /etc/profile #在當前bash環境下讀取并執行/etc/profile中的命令 -
system管理配置
#原始碼包安裝后沒有辦法使用system管理,需要我們自己配置 [root@web03 /usr/lib/systemd/system]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target [root@web03 ~]# systemctl daemon-reload #加載新的unit (*.service)組態檔
三、Nginx命令
nginx #啟動nginx, 等價于systemctl start nginx
nginx -s reopen #重啟Nginx, 等價于systemctl restart nginx
nginx -s reload #重新加載Nginx組態檔,然后以優雅的方式重啟Nginx, 等價于systemctl reload nginx
nginx -s stop #強制停止Nginx服務, 等價于systemctl stop nginx
nginx -s quit #優雅地停止Nginx服務(即處理完所有請求后再停止服務)
nginx -t #檢測組態檔是否有語法錯誤,然后退出
nginx -?,-h #打開幫助資訊
nginx -v #顯示版本號資訊并退出
nginx -V #顯示版本號和配置選項資訊,然后退出
nginx -V 2>&1 | sed "s/\s\+--/\n --/g" #模塊分行輸出,格式化輸出
killall nginx #殺死所有nginx行程
systemctl enable nginx #加入開機自啟動
Centos6:
啟動:nginx
service nginx start
/etc/init.d/nginx start
加入開機自啟:
chkconfig nginx on
nginx -T #檢測組態檔是否有語法錯誤,轉儲并退出
nginx -q #在檢測組態檔期間屏蔽非錯誤資訊(列印錯誤日志)
nginx -p prefix #設定nginx的作業目錄(默認是:/usr/share/nginx/)
nginx -c filename #設定組態檔路徑(默認是:/etc/nginx/nginx.conf)
nginx -g directives #設定組態檔外的全域指令
四、Nginx服務平滑無感知添加模塊和升級
4.1 模塊決議
[root@web02 ~]# 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 --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie
############################內容決議#######################################
nginx version: nginx/1.20.2 #nginx版本
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 #nginx檔案的安裝路徑,其它選項如果使用相對路徑,那么以此路徑為基礎路徑
--sbin-path=/usr/sbin/nginx #二進制程式的安裝目錄
--modules-path=/usr/lib64/nginx/modules #模塊安裝路徑
--conf-path=/etc/nginx/nginx.conf #設定nginx的conf檔案路徑
--error-log-path=/var/log/nginx/error.log #設定nginx錯誤日志路徑
--http-log-path=/var/log/nginx/access.log #設定nginx的訪問日志路徑
--pid-path=/var/run/nginx.pid #設定nginx的pid檔案路徑
--lock-path=/var/run/nginx.lock #設定lock檔案臨時存放的路徑
--http-client-body-temp-path=/var/cache/nginx/client_temp #設定http客戶端主體臨時快取路徑
--http-proxy-temp-path=/var/cache/nginx/proxy_temp #設定http反向代理臨時路徑
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp #設定http的fastcgi臨時快取路徑
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp #設定uwsgi臨時目錄
--http-scgi-temp-path=/var/cache/nginx/scgi_temp #scgi臨時存放目錄
--user=nginx #設定啟動 worker 行程時所使用的非特權用戶名
--group=nginx #設定啟動 worker 行程時所使用的非特權用戶組名
--with-compat
--with-file-aio
--with-threads
......
4.2 Nginx服務添加模塊
1.安裝依賴
[root@web02 nginx-1.20.2]# yum install -y openssl openssl-devel
2.再生成一次
[root@web02 nginx-1.20.2]# ./configure --prefix=/usr/local/nginx-1.20.2-new --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
3.安裝
[root@web02 nginx-1.20.2]# make && make install
4.重做軟連接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.20.2-new /usr/local/nginx
5.重啟服務
[root@web02 nginx-1.20.2]# systemctl restart nginx
4.3 Nginx升級
1.下載新版本的包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
2.解壓
[root@web02 ~]# tar xf nginx-1.20.2.tar.gz
3.生成
[root@web02 nginx-1.20.2]# cd nginx-1.20.2
[root@web02 nginx-1.20.2]# ./configure --prefix=/usr/local/nginx-1.20.2 --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module
4.編譯安裝
[root@web02 nginx-1.20.2]# make && make install
5.重做軟連接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.20.2 /usr/local/nginx
6.重啟服務
[root@web02 ~]# systemctl restart nginx
五、Nginx相關檔案
為了讓大家更清晰的了解Nginx軟體的全貌,可使用rpm -ql nginx查看整體的目錄結構及對應的功能,如下表格整理了Nginx比較重要的組態檔,
5.1 Nginx主組態檔
| 路徑 | 型別 | 作用 |
|---|---|---|
| /etc/nginx/nginx.conf | 組態檔 | nginx主組態檔 |
| /etc/nginx/conf.d/default.conf | 組態檔 | 默認網站組態檔 |
5.2Nginx代理相關引數檔案
| 路徑 | 型別 | 作用 |
|---|---|---|
| /etc/nginx/fastcgi_params | 組態檔 | Fastcgi代理組態檔 |
| /etc/nginx/scgi_params | 組態檔 | scgi代理組態檔 |
| /etc/nginx/uwsgi_params | 組態檔 | uwsgi代理組態檔 |
5.3 Nginx編碼相關組態檔
| 路徑 | 型別 | 作用 |
|---|---|---|
| /etc/nginx/win-utf | 組態檔 | Nginx編碼轉換映射檔案 |
| /etc/nginx/koi-utf | 組態檔 | Nginx編碼轉換映射檔案 |
| /etc/nginx/koi-win | 組態檔 | Nginx編碼轉換映射檔案 |
| /etc/nginx/mime.types | 組態檔 | Content-Type與擴展名 |
5.4 Nginx管理相關命令
| 路徑 | 型別 | 作用 |
|---|---|---|
| /usr/sbin/nginx | 命令 | Nginx命令列管理終端工具 |
| /usr/sbin/nginx-debug | 命令 | Nginx命令列與終端除錯工具 |
5.5 Nginx日志相關目錄與檔案
| 路徑 | 型別 | 作用 |
|---|---|---|
| /var/log/nginx | 目錄 | Nginx默認存放日志目錄 |
| /etc/logrotate.d/nginx | 組態檔 | Nginx默認的日志切割 |
六、Nginx組態檔
Nginx主組態檔/etc/nginx/nginx.conf是一個純文本型別的檔案,整個組態檔是以區塊的形式組織的,一般,每個區塊以一對大括號{}來表示開始與結束,
Nginx主組態檔整體分為三塊進行學習,分別是CoreModule(核心模塊),EventModule(事件驅動模塊),HttpCoreModule(http內核模塊)
組態檔內容:
[root@web01 ~]# cat /etc/nginx/nginx.conf
#########################核心模塊####################
#指定啟動的用戶
user www;
#nginx的worker行程的數量,auto==CPU數量
worker_processes auto;
#指定錯誤日志存放的路徑以及記錄的級別 debug/info/notice/warn/error/emerg
error_log /var/log/nginx/error.log notice;
#指定pid檔案
pid /var/run/nginx.pid;
########################事件驅動模塊#################
events {
#每個worker作業行程的最大連接數
worker_connections 1024;
}
######################http內核模塊###################
http {
#包含,nginx可識別的檔案型別
include /etc/nginx/mime.types;
#當nginx不識別檔案型別的時候,默認下載
default_type application/octet-stream;
#指定日志格式,日志格式起個名字
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#指定訪問日志存盤路徑與格式
access_log /var/log/nginx/access.log main;
#高效傳輸
sendfile on;
#高效傳輸
#tcp_nopush on;
#開啟長連接
keepalive_timeout 65;
#開啟壓縮
#gzip on;
#包含網站的組態檔
include /etc/nginx/conf.d/*.conf;
#一個server表示一個網站
server {
#監聽埠
listen 80;
#網站提供的域名
server_name localhost;
#字符集
charset utf8;
#匹配、控制訪問的網站站點
location / {
#指定站點目錄
root /usr/share/nginx/html;
#指定默認訪問的頁面
index index.html index.htm;
}
}
}
access_log /var/log/nginx/access.log json ;
日志格式可以用下面的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"}';
七、搭建小游戲案例
7.1 撰寫史上最簡單配置
[root@web01 ~]# vim /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name www.mario.com;
#server_name localhost;
location / {
root /opt/Super_Marie;
index index.html;
}
}
7.2 檢查組態檔
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
7.3 創建站點目錄
[root@web01 ~]# mkdir -p /opt/Super_Marie
7.4 上傳代碼包
[root@web01 ~]# cd /opt/Super_Marie
[root@web01 Super_Marie]# rz
[root@web01 Super_Marie]# unzip mario.zip
7.5 多載Nginx
[root@web01 Super_Marie]# systemctl restart nginx
7.6 配置Windows下的hosts(域名決議)
Windows中hosts檔案所在位置:
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 www.mario.com
7.7 瀏覽器上訪問頁面玩游戲
八、再搭建一個游戲
8.1 編輯組態檔
[root@web01 ~]# vim /etc/nginx/conf.d/gametwo.conf
server {
listen 80;
server_name www.tank.com;
location / {
root /opt/tank;
index index.html;
}
}
[root@web01 opt]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# mkdir -p /opt/tank
[root@web01 opt]# systemctl restart nginx
8.2 上傳代碼
[root@web01 ~]# cd /opt/tank
[root@web01 tank]# rz
[root@web01 tank]# unzip tankedazhan.zip
8.3 配置Windows下的hosts
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 www.mario.com www.tank.com
8.4 瀏覽器上輸入不同域名地址訪問對應頁面進行游戲
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402392.html
標籤:Linux
