nginx
目錄
- nginx
- nginx簡介
- nginx的特性
- nginx的作業原理
- 部署nginx
nginx簡介
nginx(發音同engine x)是一款輕量級的Web服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,并在一個BSD-like協議下發行,
nginx由俄羅斯的程式設計師Igor Sysoev所開發,最初供俄國大型的入口網站及搜尋引擎Rambler使用,
第一個公開版本0.1.0發布于2004年10月4日,其將源代碼以類BSD許可證的形式發布,因它的穩定性、豐富的功能集、示例組態檔和低系統資源的消耗而聞名,2011年6月1日,nginx 1.0.4發布,
nginx的特點是占有記憶體少,并發能力強,事實上nginx的并發能力確實在同型別的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等,
nginx的特性
nginx是一個很牛的高性能Web和反向代理服務器,它具有很多非常優越的特性:
在高連接并發的情況下,nginx是Apache服務器不錯的替代品,能夠支持高達50000個并發連接數的回應
使用epoll and kqueue作為開發模型
nginx作為負載均衡服務器:nginx既可在內部直接支持和PHP程式對外進行服務,也可支持作為HTTP代理服務器對外進行服務
nginx采用C進行撰寫,不論系統資源開銷還是CPU使用效率都比Perlbal要好很多
nginx的作業原理
nginx的模塊直接被編譯進nginx,因此屬于靜態編譯方式,
啟動nginx后,nginx的模塊被自動加載,與Apache不一樣,首先將模塊編譯為一個so檔案,然后在組態檔中指定是否進行加載,
在決議組態檔時,nginx的每個模塊都有可能去處理某個請求,但是同一個處理請求只能由一個模塊來完成,
nginx的行程架構:
啟動nginx時,會啟動一個Master行程,這個行程不處理任何客戶端的請求,主要用來產生worker執行緒,一個worker執行緒用來處理n個request,

作業方式
在作業方式上,Nginx分為單作業行程和多作業行程兩種模式,在單作業行程模式下,除主行程外,還有一個作業行程,作業行程是單執行緒的;在多作業行程模式下,每個作業行程包含多個執行緒,Nginx默認為單作業行程模式,
Nginx在啟動后,會有一個master行程和多個worker行程,
下圖展示了nginx模塊一次常規的HTTP請求和回應的程序

下圖展示了基本的WEB服務請求步驟

部署nginx
nginx官網
在官網里面找Stable version(穩定版本)
[root@localhost ~]# systemctl stop firewalld.service //關閉防火墻
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//創建系統用戶nginx
[root@localhost ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
//安裝依賴包
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
//創建日志存放目錄
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz //下載nginx
[root@localhost src]# ls
debug kernels nginx-1.22.0.tar.gz
[root@localhost src]# tar xf nginx-1.22.0.tar.gz
[root@localhost src]# cd nginx-1.22.0/
[root@localhost nginx-1.22.0]# ./configure \ //進行編譯
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l)
//統計系統里面有幾個cpu,然后進行安裝
[root@localhost nginx-1.22.0]# ls
CHANGES CHANGES.ru LICENSE Makefile README auto conf configure contrib html man objs src
[root@localhost nginx-1.22.0]# ls objs/ //編譯存放的地方
Makefile nginx ngx_auto_config.h ngx_modules.c src
autoconf.err nginx.8 ngx_auto_headers.h ngx_modules.o
[root@localhost nginx-1.22.0]# file objs/nginx
objs/nginx: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=146c123f448d84a21258affd355418734db34b42, with debug_info, not stripped
//主程式檔案
[root@localhost nginx-1.22.0]# make install
//安裝
[root@localhost nginx-1.22.0]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf html logs sbin
[root@localhost nginx]# /usr/local/nginx/sbin/nginx //開啟ngix
[root@localhost nginx]# ss -antl //查看80埠
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost nginx]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
//配置環境變數
[root@localhost nginx]# source /etc/profile.d/nginx.sh
//使其生效
[root@localhost nginx]# cd
[root@localhost ~]# which nginx //現在就是可以直接用nginx啟動服務
/usr/local/nginx/sbin/nginx
[root@localhost ~]# nginx -s stop //停止服務
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# nginx //開啟服務
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//服務控制方式,使用nginx命令
-t //檢查組態檔語法
-v //輸出nginx的版本
-c //指定組態檔的路徑
-s //發送服務控制信號,可選值有{stop|quit|reopen|reload}
訪問:

將其添加到systemd服務里面
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service
[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service
[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost ~]# nginx -s stop //先停止nginx
[root@localhost ~]# systemctl daemon-reload
//加載配置
[root@localhost ~]# systemctl enable --now nginx //設定開機自啟
[root@localhost ~]# systemctl status nginx.service //查看狀態
● nginx.service - nginx server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2022-10-10 21:49:52 CST; 1min 56s ago
Process: 322790 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 322791 (nginx)
Tasks: 2 (limit: 12221)
Memory: 2.3M
CGroup: /system.slice/nginx.service
├─322791 nginx: master process /usr/local/nginx/sbin/nginx
└─322792 nginx: worker process
Oct 10 21:49:52 localhost systemd[1]: Starting nginx server daemon...
Oct 10 21:49:52 localhost systemd[1]: Started nginx server daemon.
[root@localhost ~]# ss -antl //查看埠
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
訪問:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/512942.html
標籤:Linux
上一篇:nginx
