小白也可以完成的0基礎(chǔ)部署Nginx服務(wù)
1. Nginx介紹:
1.1 Nginx是什么?
Nginx(“engine x”)是一個(gè)開源的,支持高性能、高并發(fā)的www服務(wù)和代理服務(wù)軟件。
由俄羅斯人Igor Sysoev開發(fā),最初應(yīng)用于俄羅斯大型網(wǎng)站www.rambler.ru上。
Nginx具有高并發(fā)、占用系統(tǒng)資源少等特性。
Nginx可以運(yùn)行在UNIX、Linux、DSB、Mac OS X、Solaris及Windows等操作系統(tǒng)上。
1.2 Nginx主要特性
支持高并發(fā):能支持幾萬并發(fā)連接
資源消耗少:三萬并發(fā)連接下,開始10個(gè)線程消耗內(nèi)存不到200MB。
可以做HTTP反向代理及加速緩存,即負(fù)載均衡功能,內(nèi)置對(duì)RS節(jié)點(diǎn)服務(wù)器健康檢查功能
具備Squid等專業(yè)緩存軟件的緩存功能
支持異步網(wǎng)絡(luò)I/O事件模型
1.3 Nginx軟件的主要功能應(yīng)用
作為Web服務(wù)軟件
反向代理及負(fù)載均衡服務(wù)
前端業(yè)務(wù)數(shù)據(jù)緩存服務(wù)
2. Nginx Web服務(wù)
2.1 Nginx作為Web服務(wù)器應(yīng)用場(chǎng)景
使用Nginx運(yùn)行HTML、JS、CSS、小圖片等靜態(tài)數(shù)據(jù)
Nginx結(jié)合FastCGI運(yùn)行PHP等動(dòng)態(tài)程序
Nginx結(jié)合Tomcat/Resin等支持Java動(dòng)態(tài)程序
2.2 如何選擇Web服務(wù)器
工作中,根據(jù)需求來選擇合適的業(yè)務(wù)服務(wù)軟件:
- 靜態(tài)業(yè)務(wù):高并發(fā)場(chǎng)景,首選采用Nginx
- 動(dòng)態(tài)業(yè)務(wù):Nginx與Apache都可,建議Nginx
- 靜態(tài)+動(dòng)態(tài)業(yè)務(wù):推薦Nginx
3 編譯安裝Nginx
安裝方法多種,本文使用編譯安裝方式。如果需要大規(guī)模部署,可將業(yè)務(wù)需求定制好rpm包,然后通過Ansible安裝。
3.1 安裝pcre庫
查看當(dāng)前系統(tǒng)版本:
cat /etc/redhat-release uname -r
結(jié)果:
CentOS release 6.10 (Final)
2.6.32-754.el6.x86_64
采用yum方式安裝pcre:
yum -y install pcre pcre-devel rpm -qa pcre pcre-devel
結(jié)果:
pcre-devel-7.8-7.el6.x86_64
pcre-7.8-7.el6.x86_64
3.2 安裝Nginx
檢查是否裝有openssl、openssl-devel:
rpm -qa openssl openssl-devel
結(jié)果:如果沒有,使用yum安裝
openssl-1.0.1e-57.el6.x86_64
openssl-devel-1.0.1e-57.el6.x86_64
創(chuàng)建nginx包存放目錄:
mkdir -p /app/nginx-1.8.1 mkdir -p /server/tools cd /server/tools/
下載nginx軟件包:
官方地址:www.nginx.rog
wget -q http://nginx.org/download/nginx-1.8.1.tar.gz
創(chuàng)建nginx用戶:
useradd nginx -s /sbin/nologin -M
解壓軟件包并進(jìn)入解壓后的目錄:
tar xf nginx-1.8.1.tar.gz cd nginx-1.8.1
進(jìn)行編譯:
編譯模塊可以通過./configure --help查看
./configure --user=nginx --group=nginx --prefix=/app/nginx-1.8.1/ --with-http_stub_status_module --with-http_ssl_module
安裝:
make make install
創(chuàng)建軟鏈接:方便使用以及版本升級(jí)
ln -s /app/nginx-1.8.1/ /app/nginx
啟動(dòng)前測(cè)試:
/app/nginx/sbin/nginx -t
結(jié)果:
nginx: the configuration file /app/nginx-1.8.1//conf/nginx.conf syntax is oknginx: configuration file /app/nginx-1.8.1//conf/nginx.conf test is successful
啟動(dòng)Nginx服務(wù)并檢查端口:
/app/nginx/sbin/nginx netstat -utpln | grep 80
結(jié)果:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13689/nginx
檢查Nginx啟動(dòng)結(jié)果:以下內(nèi)容代表啟動(dòng)成功
curl 192.168.1.31
結(jié)果:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a rel="external nofollow" >nginx.org</a>.<br/>
Commercial support is available at
<a rel="external nofollow" >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4. Nginx目錄結(jié)構(gòu)與配置文件
4.1 Nginx目錄結(jié)構(gòu)說明
tree /app/nginx
/app/nginx ├── client_body_temp ├── conf #nginx配置文件目錄 │ ├── fastcgi.conf #fastcgi相關(guān)參數(shù)配置文件 │ ├── fastcgi.conf.default │ ├── fastcgi_params #fastcgi參數(shù)文件 │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types #媒體類型 │ ├── mime.types.default │ ├── nginx.conf #Nginx主配置文件 │ ├── nginx.conf.default │ ├── scgi_params #scgi配置文件 │ ├── scgi_params.default │ ├── uwsgi_params #uwsgi配置文件 │ ├── uwsgi_params.default │ └── win-utf ├── fastcgi_temp #fastcgi臨時(shí)數(shù)據(jù)文件 ├── html #默認(rèn)站點(diǎn)目錄 │ ├── 50x.html #錯(cuò)誤頁面顯示文件 │ └── index.html #默認(rèn)的站點(diǎn)首頁文件 ├── logs #默認(rèn)日志路徑 │ ├── access.log #默認(rèn)訪問日志文件 │ ├── error.log #默認(rèn)錯(cuò)誤日志文件 │ └── nginx.pid #Nginx的pid文件 ├── proxy_temp #臨時(shí)目錄 ├── sbin #Nginx命令目錄 │ ├── nginx #啟動(dòng)命令 │ └── nginx.old ├── scgi_temp #臨時(shí)目錄 └── uwsgi_temp #臨時(shí)目錄 9 directories, 22 files
4.2 Nginx主配置文件
去注釋顯示配置文件:
egrep -v "#|^$" /app/nginx/conf/nginx.conf.default
結(jié)果:
worker_processes 1; #worker進(jìn)程數(shù)量
events { #事件區(qū)塊開始
worker_connections 1024; #單worker進(jìn)程支持的最大連接
} #事件區(qū)塊結(jié)束
http { #HTTP區(qū)塊開始
include mime.types; #支持的媒體類型庫
default_type application/octet-stream; #默認(rèn)媒體類型
sendfile on; #開啟高效傳輸模式
keepalive_timeout 65; #連接超時(shí)
server { #server區(qū)塊開始
listen 80; #服務(wù)端口,默認(rèn)80
server_name localhost; #域名主機(jī)名
location / { #location區(qū)塊開始
root html; #站點(diǎn)根目錄
index index.html index.htm; #默認(rèn)首頁文件
} #location區(qū)塊結(jié)束
error_page 500 502 503 504 /50x.html;#對(duì)應(yīng)狀態(tài)碼及回應(yīng)
location = /50x.html { #location開始回應(yīng)50x.html
root html; #站點(diǎn)目錄為html
}
}
} #HTTP區(qū)塊結(jié)束
注:server區(qū)塊和location區(qū)塊可以是多個(gè)。
以上就是小白也可以完成的0基礎(chǔ)部署Nginx服務(wù)的詳細(xì)內(nèi)容,更多關(guān)于Nginx服務(wù)部署的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
讓VIM支持Nginx .conf文件語法高亮顯示功能的方法
這篇文章主要給大家介紹了關(guān)于讓VIM支持Nginx .conf文件語法高亮顯示功能的方法,文中分別介紹了手動(dòng)修改和自動(dòng)化腳本兩種方法的實(shí)現(xiàn),都給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07
Nginx轉(zhuǎn)發(fā)需求querystring轉(zhuǎn)寫示例解析
這篇文章主要為大家介紹了Nginx轉(zhuǎn)發(fā)需求querystring轉(zhuǎn)寫示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
nginx通過location配置代理的原理和實(shí)現(xiàn)方式
這篇文章主要介紹了nginx通過location配置代理的原理和實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Nginx優(yōu)化服務(wù)之網(wǎng)頁壓縮的實(shí)現(xiàn)方法
這篇文章主要介紹了Nginx優(yōu)化服務(wù)之網(wǎng)頁壓縮的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Nginx Rewrite模塊應(yīng)用的幾種場(chǎng)景
這篇文章主要介紹了Nginx Rewrite模塊應(yīng)用的幾種場(chǎng)景,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

