Nginx Web快速入門
Nginx Web快速入門
目錄Nginx概述
Nginx是一個開源且高效能,可靠的服務,代理服務。
開源:直接獲取原始碼
高效能: 支援海量併發
可靠: 服務穩定
為什麼選擇Nginx服務
1.Nginx技術成熟,具備的功能是企業最常使用而且最需要的
2.適合當前主流架構趨勢,微服務、雲架構、中間層
3.統一技術棧,降低維護成本,降低技術更新成本。
Nginx採用Epool網路模型,Apache採用Select模型
Select:當用戶發起一次請求,select模型就會進行一次遍歷掃描,從而導致效能低下。
Epool:當用戶發起請求,epool模型會直接進行處理,效率高效,並無連線限制。
Nginx的應用場景
原始碼安裝nginx
Nginx的官方網站https://nginx.org/
#建立存放原始碼包的目錄 [root@web02 ~]$ mkdir /source_code [root@web02 ~]$ cd /source_code/ #下載Nginx的原始碼包 [root@web02 /source_code]$ wget https://nginx.org/download/nginx-1.20.1.tar.gz [root@web02 /source_code]$ ll total 1040 -rw-r--r-- 1 root root 1061461 May 25 23:34 nginx-1.20.1.tar.gz #安裝nginx的依賴包 [root@web02 /source_code]$ yum install -y pcre-devel zlib-devel # 1.解壓 [root@web02 /source_code]$ tar xf nginx-1.20.1.tar.gz [root@web02 /source_code]$ ll total 1040 drwxr-xr-x 8 1001 1001 158 May 25 20:35 nginx-1.20.1 -rw-r--r-- 1 root root 1061461 May 25 23:34 nginx-1.20.1.tar.gz # 2.生成 [root@web02 /source_code]$ cd nginx-1.20.1/ [root@web02 /source_code/nginx-1.20.1]$ ./configure --prefix=/app/nginx-1.20.1 # 3.編譯 [root@web02 /source_code/nginx-1.20.1]$ make # 4.安裝 [root@web02 /source_code/nginx-1.20.1]$ make install # 5.安裝後做軟連結 [root@web02 /source_code]$ ln -s /app/nginx-1.20.1 /app/nginx [root@web02 /source_code]$ ll /app total 0 lrwxrwxrwx 1 root root 17 Jul 15 16:18 nginx -> /app/nginx-1.20.1 drwxr-xr-x 6 root root 54 Jul 15 16:15 nginx-1.20.1 # 6.新增nginx命令的環境變數 [root@web02 /source_code]$ vim /etc/profile PATH="/app/nginx/sbin:$PATH" # 7.載入環境變數 [root@web02 /source_code]$ source /etc/profile # 8.檢測配置檔案 [root@web02 /source_code]$ nginx -t nginx: the configuration file /app/nginx-1.20.1/conf/nginx.conf syntax is ok nginx: configuration file /app/nginx-1.20.1/conf/nginx.conf test is successful # 9.啟動nginx服務 [root@web02 /source_code]$ nginx # 10.檢測nginx程序 [root@web02 /source_code]$ ps -ef|grep [n]ginx root 13924 1 0 16:21 ? 00:00:00 nginx: master process nginx nobody 13925 13924 0 16:21 ? 00:00:00 nginx: worker process # 11.檢測nginx的埠 [root@web02 /source_code]$ netstat -lntup|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13924/nginx: master # 12.停止Nginx服務的命令 nginx -s stop
開啟瀏覽器:http://10.0.0.8/
Yum安裝nginx
進入Nginx官網,選擇download,選擇stable and mainline,點選RHEL/CentOS,即可出現Nginx官方源配置檔案資訊
# 1.新增nginx的官方源 [root@web02 ~]$ 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 # 2.先壓縮其他倉庫,指定從官方下載 [root@web02 /etc/yum.repos.d]$ gzip CentOS-Base.repo [root@web02 /etc/yum.repos.d]$ gzip epel.repo [root@web02 /etc/yum.repos.d]$ ll total 12 -rw-r--r-- 1 root root 542 Jul 5 19:29 CentOS-Base.repo.gz -rw-r--r-- 1 root root 254 Jul 5 19:24 epel.repo.gz -rw-r--r-- 1 root root 192 Jul 15 17:21 nginx.repo # 3.安裝nginx [root@web02 /etc/yum.repos.d]$ yum install -y nginx #檢查nginx當前版本 [root@web02 ~]$ /sbin/nginx -v nginx version: nginx/1.20.1 #檢視安裝的nginx功能模組 [root@web02 ~]$ /sbin/nginx -V nginx version: nginx/1.20.1 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' #將利用yum安裝的nginx功能模組,新增到我們用原始碼安裝的nginx中 # 1.進入source_code/nginx-1.20.1/目錄中 [root@web02 ~]$ cd /source_code/nginx-1.20.1/ [root@web02 /source_code/nginx-1.20.1]$ ll total 792 drwxr-xr-x 6 1001 1001 326 Jul 15 16:09 auto -rw-r--r-- 1 1001 1001 311503 May 25 20:35 CHANGES -rw-r--r-- 1 1001 1001 475396 May 25 20:35 CHANGES.ru drwxr-xr-x 2 1001 1001 168 Jul 15 16:09 conf -rwxr-xr-x 1 1001 1001 2590 May 25 20:35 configure drwxr-xr-x 4 1001 1001 72 Jul 15 16:09 contrib drwxr-xr-x 2 1001 1001 40 Jul 15 16:09 html -rw-r--r-- 1 1001 1001 1397 May 25 20:35 LICENSE -rw-r--r-- 1 root root 442 Jul 15 16:12 Makefile drwxr-xr-x 2 1001 1001 21 Jul 15 16:09 man drwxr-xr-x 3 root root 174 Jul 15 16:14 objs -rw-r--r-- 1 1001 1001 49 May 25 20:35 README drwxr-xr-x 9 1001 1001 91 Jul 15 16:09 src # 2.刪除Makefile檔案 [root@web02 /source_code/nginx-1.20.1]$ rm -rf Makefile # 3.安裝openssl [root@web02 /source_code/nginx-1.20.1]$ yum install -y openssl-devel # 4.重新生成功能模組 [root@web02 /source_code/nginx-1.20.1]$ ./configure --prefix=/app/nginx-1.20.1 --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' # 5.編譯並安裝 [root@web02 /source_code/nginx-1.20.1]$ make [root@web02 /source_code/nginx-1.20.1]$ make install # 6.檢查是否安裝好功能模組 [root@web02 /source_code/nginx-1.20.1]$ nginx -V nginx version: nginx/1.20.1 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=/app/nginx-1.20.1 --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' # 7.檢查/source_code/nginx-1.20.1目錄下的Makefile是否生成 [root@web02 /source_code/nginx-1.20.1]$ ll total 79 -rw-r--r-- 1 root root 442 Jul 15 18:00 Makefile -------------------------------------------------------------------------------------------------------------------------------- PS:我用yum安裝的nginx命令位於/sbin/nginx,利用原始碼安裝的nginx的命令位於/app/nginx-1.20.1/sbin/下,但是加入了PATH環境變數,所以直接使用nginx時,用的是原始碼安裝
Nginx相關命令總結
nginx:啟動nginx服務
nginx -v:檢視當前nginx版本
nginx -V:檢查nginx已安裝的功能模組
nginx -s stop:停止nginx服務
nginx -s reload:重新載入nginx服務(非重啟)
nginx -t:檢查配置檔案是否正確
./configure:生成指定功能模組
make:原始碼安裝方式時進行編譯
make install:原始碼安裝方式時進行安裝
Nginx相關的配置檔案
1.Nginx主配置檔案
路徑 | 型別 | 作用 |
---|---|---|
/etc/nginx/nginx.conf | 配置檔案 | nginx主配置檔案 |
/etc/nginx/conf.d/default.conf | 配置檔案 | 預設網站配置檔案(虛擬主機配置檔案) |
PS:網站的每個站點都可以在/etc/nginx/conf.d下建立對應的配置檔案,default.conf是一個範本,以後自己寫的就按照他的格式就行
2.Nginx代理相關引數檔案
路徑 | 型別 | 作用 |
---|---|---|
/etc/nginx/fastcgi_params | 配置檔案 | Fastcgi代理配置檔案 |
/etc/nginx/scgi_params | 配置檔案 | scgi代理配置檔案 |
/etc/nginx/uwsgi_params | 配置檔案 | uwsgi代理配置檔案 |
3.Nginx編碼相關配置檔案
路徑 | 型別 | 作用 |
---|---|---|
/etc/nginx/win-utf | 配置檔案 | Nginx編碼轉換對映檔案 |
/etc/nginx/koi-utf | 配置檔案 | Nginx編碼轉換對映檔案 |
/etc/nginx/koi-win | 配置檔案 | Nginx編碼轉換對映檔案 |
/etc/nginx/mime.types | 配置檔案 | Content-Type與副檔名 |
4.Nginx管理相關命令
路徑 | 型別 | 作用 |
---|---|---|
/usr/sbin/nginx | 命令 | Nginx命令列管理終端工具 |
/usr/sbin/nginx-debug | 命令 | Nginx命令列與終端除錯工具 |
5.Nginx日誌相關目錄與檔案
路徑 | 型別 | 作用 |
---|---|---|
/var/log/nginx | 目錄 | Nginx預設存放日誌目錄 |
/etc/logrotate.d/nginx | 配置檔案 | Nginx預設的日誌切割 |
[root@web02 ~]$ ll /var/log/nginx/
total 4
# 訪問日誌
-rw-r----- 1 nginx adm 0 Jul 15 17:25 access.log
# 錯誤日誌
-rw-r----- 1 nginx adm 512 Jul 15 18:07 error.log
Nginx的配置檔案
nginx主配置檔案內容詳解/etc/nginx/nginx.conf
-------------------------------------------------------------------------
------------------------------- 核心模組 ----------------------------------
# nginx的啟動使用者:nginx
user nginx;
# worker程序(子程序),根據cpu的核心數,幾核CPU就對應幾個程序,auto自動獲取配置
worker_processes auto;
# 錯誤日誌,以及日誌的路徑和級別
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;
# 預設需要下載的型別,不能直接開啟訪問
default_type application/octet-stream;
# 日誌格式 格式名稱;nginx內建變數,定義了nginx日誌資訊的格式(以空格為分隔符)
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;
# 虛擬主機相關配置(網站的配置)
server {
# 監聽在80埠
listen 80; (ipv4)
# 配置訪問的域名或者IP或者主機,就是瀏覽器要輸入什麼才能訪問這個網站(_代表用ip訪問)
server_name _;
# 指定該網站的站點目錄:這個站點目錄中包含了50x.html報錯頁面
root /usr/share/nginx/html;
# 404頁面的報錯路徑
error_page 404 /404.html;
# 頁面跳轉的相關配置
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# 包含所有nginx的虛擬主機配置檔案
include /etc/nginx/conf.d/*.conf;
}