Nginx服務器 | Nginx基礎服務實戰

6Xj5gx

You got a dream,you gotta protect it.People can’t do something themselves,they wanna tell you you can’t do it.If you want something,go get it.

不要別人告訴你該做什麼,有夢想,就得保護。他人做不成什麼事情,就跟你說你也做不成。如果你想要什麼,就要去爭取。——《當幸福來敲門》

基本概述

6XjZnO.jpg

Nginx 是互聯網主流的高性能 http 和 反響代理 Web 服務器,Nginx 不但可以作爲 Web 服務器,它還提供了靜態資源服務、緩存、負載均衡 等功能。

不論你是前端程序員還是後端程序員,對於Nginx的接觸,應該不會陌生。對於前端和後端來說,它就像是溝通橋樑,類似粘合劑的作用。 尤其是在前後端分離的時代,Nginx出場的頻率和一線偶像明星出鏡的次數差不多。
在雲原生[Cloud Native]時代,Envoy的出現,似乎增加了我們技術選型的可能性。當然Envoy和Nginx都採用了 多線程 + 非阻塞 + 異步IO(Libevent) 的架構模式。

實戰搭建Nginx

基於Centos 7部署Nginx
  • 安裝Nginx相關依賴
#安裝使nginx支持rewrite
yum -y install pcre* 
#安裝使nginx支持gcc-c++
yum -y install gcc-c++
#安裝使nginx支持zlib      
yum -y install zlib*
#安裝使nginx支持openssl
yum -y install openssl
  • 下載安裝nginx-1.19.8.tar.gz安裝包,上傳到服務器目錄: /usr/local
[root@centos-pivtoal ~]# cd /usr/local
[root@centos-pivtoal local]# ls
aegis  bin  cloudmonitor  etc  games  include  lib  lib64  libexec  nginx-1.19.8.tar.gz  sbin  share  src
[root@centos-pivtoal local]# tar -xvf nginx-1.19.8.tar.gz 
[root@centos-pivtoal local]# cd nginx-1.19.8
[root@centos-pivtoal nginx-1.19.8]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@centos-pivtoal nginx-1.19.8]# 
  • 配置安裝檢查: ./configure
[root@centos-pivtoal nginx-1.19.8]# ./configure
[root@centos-pivtoal nginx-1.19.8]# 
  • 編譯並安裝Nginx: make && make install
[root@centos-pivtoal nginx-1.19.8]#  make && make install
[root@centos-pivtoal nginx-1.19.8]# 
  • 配置開機自啓動Nginx

[1]. 在/lib/systemd/system/在目錄創建nginx.service:

[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

[注意事項]:

[Unit]: 服務的說明

Description:描述服務

After:描述服務類別

[Service]服務運行參數的設置

Type:forking是後臺運行的形式

ExecStart爲服務的具體運行命令

ExecReload爲重啓命令

ExecStop爲停止命令

PrivateTmp:True表示給服務分配獨立的臨時空間

[Service]的啓動、重啓、停止命令全部要求使用絕對路徑

[Install]運行級別下服務安裝的相關設置,可設置爲多用戶,即系統運行級別爲3

[2]. 添加系統自啓動

#設置開機啓動
systemctl enable nginx.service
#啓動nginx服務
systemctl start nginx.service
#檢查nginx服務狀態
systemctl status nginx.service
#重新啓動服務
systemctl restart nginx.service
#查看所有已啓動的服務
systemctl list-units --type=service
基於Dokcer部署Nginx
  • 拉取Nginx鏡像
[root@centos-pivtoal ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete 
0732ab25fa22: Pull complete 
d7f36f6fe38f: Pull complete 
f72584a26f32: Pull complete 
7125e4df9063: Pull complete 
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@centos-pivtoal ~]# docker tag docker.io/library/nginx:latest nginx:latest 
[root@centos-pivtoal ~]# 
  • 編寫Docker部署腳本
docker run -itd -p 80:80 -p 443:443  --name nginx-server --network-alias nginx-server --hostname nginx-server --restart always -v /docker/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /home/application/html:/usr/share/nginx/html nginx:latest
  • 執行腳本命令
docker run -itd -p 80:80 -p 443:443  --name nginx-server --network-alias nginx-server --hostname nginx-server --restart always -v /docker/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /home/application/html:/usr/share/nginx/html nginx:latest

實戰配置Nginx

  • 設置Nginx進程數目[worker_processes]:默認可以設置爲CPU的核數相等,併發比較大的時候,可以設置爲cpu核數*2
#查看服務器cpu信息:cat /proc/cpuinfo | grep processor
[root@iZm5efc4cs8k6t2agr7tceZ manager]# cat /proc/cpuinfo | grep processor
processor       : 0
processor       : 1
processor       : 2
processor       : 3
[root@iZm5efc4cs8k6t2agr7tceZ manager]# 

添加配置舉例:
[1] .4 CPU (4 Core) + 4 worker_processes(每個worker_processes 使用1個CPU)

worker_processes 4;

worker_cpu_affinity 0001 0010 0100 1000;

[2].8 CPU (8 Core) + 8 worker_processes (每個worker_processes 使用1個CPU)

worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

[3].16 CPU (16 Core) + 16 worker_processes (每個worker_processes 使用1個CPU)

worker_processes 16;

worker_cpu_affinity
0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;

  • 配置事件處理模型[events]:
#配置Nginx worker進程最大打開文件數
worker_rlimit_nofile 65535;
events { 
#使用高性能的 epoll 事件驅動,處理效率高
use epoll;
accept_mutex on;
#打開同時接受多個新網絡連接請求的功能
multi_accept on; 
#單個進程允許的客戶端最大連接數
worker_connections 65535;
}
  • 開啓高效傳輸模式
#開啓高效文件傳輸模式
sendfile on; 
#需要在sendfile開啓模式纔有效,防止網路阻塞,積極的減少網絡報文段的數量。將響應頭和正文的開始部分一起發送,而不一個接一個的發送。
tcp_nopush on; 
tcp_nodelay on;
  • 開啓傳輸壓縮
gzip on; 
gzip_vary on; 
gzip_proxied any; 
gzip_comp_level 6; 
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章