Nginx安裝及啟動(dòng)全過(guò)程
一 Nginx yum安裝
1.1 前置準(zhǔn)備
1 [root@nginx01 ~]# systemctl status firewalld.service #檢查防火墻 2 [root@nginx01 ~]# getenforce #檢查SELinux 3 Disabled
提示:建議關(guān)閉防火墻,或通過(guò)如下方式放通相關(guān)80或443端口:
1 firewall-cmd --permanent --add-port=80/tcp 2 firewall-cmd --permanent --add-port=443/tcp
1.2 配置yum源
1 [root@nginx01 ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF 2 [nginx-stable] 3 name=nginx stable repo 4 baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/ 5 gpgcheck=1 6 enabled=1 7 gpgkey=https://nginx.org/keys/nginx_signing.key 8 module_hotfixes=true 9 10 [nginx-mainline] 11 name=nginx mainline repo 12 baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/ 13 gpgcheck=1 14 enabled=0 15 gpgkey=https://nginx.org/keys/nginx_signing.key 16 module_hotfixes=true 17 EOF
1.3 安裝Nginx
1 [root@nginx01 ~]# yum -y install nginx 2 [root@nginx01 ~]# nginx -v 3 nginx version: nginx/1.18.0
提示:如上安裝默認(rèn)安裝為當(dāng)前最新穩(wěn)定版,若需要安裝開發(fā)版,可執(zhí)行yum-config-manager --enable nginx-mainline,然后yum安裝,不建議安裝開發(fā)版。
1 [root@nginx01 ~]# systemctl start nginx 2 [root@nginx01 ~]# systemctl enable nginx #啟動(dòng)服務(wù)
1.4 測(cè)試訪問(wèn)
瀏覽器訪問(wèn):http://172.24.8.71/

1.5 其他信息
1 [root@nginx01 ~]# nginx -V #查看yum安裝所編譯的模塊及參數(shù) 2 nginx version: nginx/1.18.0 3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 4 built with OpenSSL 1.0.2k-fips 26 Jan 2017 5 TLS SNI support enabled 6 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' 7 [root@nginx01 ~]# rpm -ql nginx #查看所安裝的文件 8 [root@nginx01 ~]# rpm -qc nginx #查看相關(guān)的配置文件
二 Nginx源碼編譯安裝
2.1 依賴組件
1 [root@nginx01 ~]# yum -y install gcc gcc-c++ wget autoconf pcre pcre-devel openssl openssl-devel openssh-clients net-tools vim ntp screen lrzsz bash-completion bash-completion-extras lvm2 make automake epel-release tree zlib zlib-devel libtool
提示:部分依賴包為比如,如:
- zlib庫(kù):zlib庫(kù)是ngx_http_gzip_module(gzip壓縮模塊)所必需的
- openssl庫(kù) :--with-http_ssl_module使用該模塊必需裝openssl庫(kù),來(lái)實(shí)現(xiàn)http支持https協(xié)議。
2.2 編譯安裝
1 [root@nginx01 ~]# useradd -s /sbin/nologin -M nginx #提前創(chuàng)建用戶及用戶組 2 [root@nginx01 ~]# wget http://nginx.org/download/nginx-1.17.8.tar.gz 3 [root@nginx01 ~]# tar -xvf nginx-1.17.8.tar.gz 4 [root@nginx01 ~]# cd nginx-1.17.8/ 5 [root@nginx01 nginx-1.17.8]# ./configure \ 6 --conf-path=/usr/local/nginx/conf/nginx.conf \ 7 --error-log-path=/var/log/nginx/error.log \ 8 --group=nginx \ 9 --http-client-body-temp-path=/var/cache/nginx/client_temp \ 10 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ 11 --http-log-path=/var/log/nginx/access.log \ 12 --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ 13 --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ 14 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ 15 --lock-path=/var/run/nginx.lock \ 16 --pid-path=/var/run/nginx.pid \ 17 --prefix=/usr/local/nginx \ 18 --sbin-path=/usr/local/bin/nginx \ 19 --user=nginx \ 20 --with-http_gzip_static_module \ 21 --with-http_realip_module \ 22 --with-http_ssl_module \ 23 --with-http_stub_status_module \ 24 --with-http_sub_module \ 25 --with-http_v2_module \ 26 --with-stream \ 27 --with-stream_realip_module \ 28 --with-stream_ssl_module 29 [root@nginx01 nginx-1.17.8]# make && make install 30 [root@nginx01 ~]# nginx -V #查看安裝版本 31 [root@nginx01 ~]# tree /usr/local/nginx/ #查看目錄結(jié)構(gòu)

目錄 | 作用 |
|---|---|
conf | 用于存儲(chǔ)nginx配置文件 |
html | 用于存放靜態(tài)網(wǎng)頁(yè) |
logs | 存放日志 |
sbin | 用于存放 nginx 執(zhí)行命令 |
2.3 服務(wù)管理
1 [root@nginx01 ~]# echo $PATH 2 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 3 [root@nginx01 ~]# mkdir -p /var/cache/nginx/ 4 [root@nginx01 ~]# ll /usr/local/bin/ 5 total 7.5M 6 -rwxr-xr-x 1 root root 7.5M Mar 5 01:09 nginx
提示:若nginx的prefix未編譯系統(tǒng)PATH中,如/opt/nginx/,需要在PATH中,可通過(guò)如下方式添加:
echo 'PATH=/opt/nginx/:\$PATH' > /etc/profile.d/nginx.sh
1 [root@nginx01 ~]# nginx #服務(wù)啟動(dòng) 2 [root@nginx01 ~]# nginx -s stop #服務(wù)關(guān)閉 3 [root@nginx01 ~]# nginx -s reload #重載配置文件 4 [root@nginx01 ~]# nginx -s reopen #重啟Nginx 5 [root@nginx01 ~]# nginx -s quit #關(guān)閉Nginx 6 [root@nginx01 ~]# nginx -t #測(cè)試配置文件 7 [root@nginx01 ~]# nginx -t -c 【file】 #使用額外的配置文件測(cè)試 8 [root@nginx01 ~]# ps aux | grep nginx #查看進(jìn)程 9 [root@nginx01 ~]# netstat -ano | grep 80 #查看端口
2.4 開機(jī)啟動(dòng)
1 [root@nginx01 ~]# vi /usr/lib/systemd/system/nginx.service 2 [Unit] 3 Description=nginx - high performance web server 4 Documentation=http://nginx.org/en/docs/ 5 After=network-online.target remote-fs.target nss-lookup.target 6 Wants=network-online.target 7 8 [Service] 9 Type=forking 10 PIDFile=/var/run/nginx.pid 11 ExecStart=/usr/local/bin/nginx -c /usr/local/nginx/conf/nginx.conf 12 ExecReload=/bin/kill -s HUP $MAINPID 13 ExecStop=/bin/kill -s TERM $MAINPID 14 15 [Install] 16 WantedBy=multi-user.target 17 [root@nginx01 ~]# systemctl daemon-reload 18 [root@nginx01 ~]# systemctl start nginx.service #啟動(dòng)服務(wù) 19 [root@nginx01 ~]# systemctl enable nginx.service #開機(jī)啟動(dòng)
說(shuō)明:
- Description:描述服務(wù)
- After:描述服務(wù)類別
- [Service]:服務(wù)運(yùn)行參數(shù)的設(shè)置
- Type=forking:是后臺(tái)運(yùn)行的形式
- ExecStart:為服務(wù)的具體運(yùn)行命令
- ExecReload:為重啟命令
- ExecStop:為停止命令
- PrivateTmp=True:表示給服務(wù)分配獨(dú)立的臨時(shí)空間
注意:[Service]的啟動(dòng)、重啟、停止命令全部要求使用絕對(duì)路徑
[Install]運(yùn)行級(jí)別下服務(wù)安裝的相關(guān)設(shè)置,可設(shè)置為多用戶,即系統(tǒng)運(yùn)行級(jí)別為3
- systemctl start nginx.service?。▎?dòng)nginx服務(wù))
- systemctl stop nginx.service?。ㄍV筺ginx服務(wù))
- systemctl enable nginx.service (設(shè)置開機(jī)自啟動(dòng))
- systemctl disable nginx.service (停止開機(jī)自啟動(dòng))
- systemctl status nginx.service (查看服務(wù)當(dāng)前狀態(tài))
- systemctl restart nginx.service (重新啟動(dòng)服務(wù))
- systemctl list-units --type=service (查看所有已啟動(dòng)的服務(wù))
2.5 測(cè)試訪問(wèn)
瀏覽器訪問(wèn):http://172.24.8.71/
2.6 編譯選項(xiàng)
1 [root@nginx01 nginx-1.17.8]# ./configure --help #查看編譯選項(xiàng)
如下為常見(jiàn)編譯選項(xiàng)及其釋義:
編譯選項(xiàng) | 作用 |
|---|---|
--prefix=/etc/nginx | 程序安裝目錄和路徑 |
--sbin-path=/usr/sbin/nginx | Nginx啟動(dòng)停止名 |
--modules-path=/usr/lib64/nginx/modules | Nginx模塊路徑 |
--conf-path=/etc/nginx/nginx.conf | Nginx主配置文件路徑 |
--error-log-path=/var/log/nginx/error.log | Nginx錯(cuò)誤日志路徑 |
--http-log-path=/var/log/nginx/access.log | Nginx訪問(wèn)日志路徑 |
--pid-path=/var/run/nginx.pid | Nginx Pid路徑 |
--lock-path=/var/run/nginx.lock | Nginx鎖路徑 |
--http-client-body-temp-path=/var/cache/nginx/client_temp | client頭部臨時(shí)緩存文件 |
--http-proxy-temp-path=/var/cache/nginx/proxy_temp | proxy臨時(shí)緩存文件 |
--http-fastcgi-temp-path=/var/cache/nginx/proxy_temp | fastcgi臨時(shí)緩存文件 |
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp | uwsgi臨時(shí)緩存文件 |
--http-scgi-temp-path=/var/cache/nginx/scgi_temp | scgi臨時(shí)緩存文件 |
--user=nginx | 設(shè)置Nginx進(jìn)程啟動(dòng)用戶 |
--group=nginx | 設(shè)置Nginx進(jìn)程啟動(dòng)用戶組 |
--with-cc-opt | 設(shè)置額外的參數(shù)將被添加到CFLACS變量 |
--with-id-opt | 設(shè)置額外的參數(shù),鏈接系統(tǒng)庫(kù) |
三 Nginx目錄及模塊
3.1 相關(guān)目錄
如下以Nginx yum安裝后的目錄為例:
路徑 | 類型 | 作用 |
|---|---|---|
/etc/nginx /etc/nginx/nginx.conf /etc/nginx/conf.d /etc/nginx/conf.d/default.conf | 配置文件 | Nginx主配置文件 |
/etc/nginx/fastcgi_params /etc/nginx/scgi_params /etc/nginx/uwsgi_params | 配置文件 | Cgi、Fastcgi、Uwsgi配置文件 |
/etc/nginx/win-utf /etc/nginx/koi-utf /etc/nginx/koi-win | 配置文件 | Nginx編碼轉(zhuǎn)換映射文件 |
/etc/nginx/mime.types | 配置文件 | http協(xié)議的Content-Type |
/etc/rc.d/init.d/nginx /etc/rc.d/init.d/nginx-debug /etc/sysconfig/nginx /etc/sysconfig/nginx-debug | 配置文件 | 配置系統(tǒng)守護(hù)進(jìn)程管理器 |
/etc/logrotate.d/nginx | 配置文件 | Nginx日志輪詢、日志切割 |
/usr/sbin/nginx /usr/sbin/nginx-debug | 命令 | Nginx終端管理器命令 |
/usr/share/doc/nginx-1.xx.x /usr/share/man/man8/nginx.8.gz | 目錄 | Nginx的幫助手冊(cè) |
/var/cache/nginx | 目錄 | Nginx的緩存目錄 |
/var/log/nginx | 目錄 | Nginx的日志目錄 |
/etc/nginx/modules /etc/lib64/nginx /etc/lib64/nginx/modules | 目錄 | Nginx的模塊目錄 |
/usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html | 目錄 | Nginx默認(rèn)站點(diǎn)目錄 |
3.2 Nginx模塊
Nginx模塊分為Nginx官方模塊和Nginx第三方模塊。
Nginx編譯選項(xiàng) | 模塊作用 |
|---|---|
ngx_http_core_module | 包含一些核心的http參數(shù)配置,對(duì)應(yīng)Nginx的配置區(qū)塊部分。 |
ngx_http_access_module | 訪問(wèn)控制模塊,用來(lái)控制網(wǎng)站用戶對(duì)Nginx的訪問(wèn)。 |
ngx_http_gzip_module | 壓縮模塊,對(duì)Nginx返回的數(shù)據(jù)壓縮,屬于性能優(yōu)化模塊。 |
ngx_http_fastcgi_module | fastcgi模塊,和動(dòng)態(tài)應(yīng)用相關(guān)的模塊,例如PHP。 |
ngx_http_proxy_module | proxy代理模塊。 |
ngx_http_upstream_module | 負(fù)載均衡模塊,實(shí)現(xiàn)網(wǎng)站的負(fù)載均衡功能機(jī)健康檢查。 |
ngx_http_rewrite_module | URL地址重寫模塊。 |
ngx_http_limit_conn_module | 限制用戶并發(fā)連接數(shù)及請(qǐng)求連接數(shù)。 |
ngx_http_limit_req_module | 限制Nginx request processing rate根據(jù)定義的key。 |
ngx_http_log_module | 訪問(wèn)日志模塊,以指定的格式記錄Nginx客戶訪問(wèn)日志等信息。 |
ngx_http_auth_basic_module | Web認(rèn)證模塊,設(shè)置Web用戶通過(guò)賬號(hào)密碼訪問(wèn)Nginx。 |
ngx_http_ssl_module | ssl模塊,用于加密的http連接,如https。 |
四 Nginx變量及狀態(tài)碼
4.1 Nginx變量
ngx_http_core_module的內(nèi)置變量通常有:http請(qǐng)求變量、Nginx內(nèi)置變量、自定義變量。
- $uri:當(dāng)前請(qǐng)求的URI,不帶參數(shù);
- $request_uri:請(qǐng)求的URI,帶完整參數(shù);
- $host:http請(qǐng)求報(bào)文中的host首部,如果沒(méi)有則以處理此請(qǐng)求的虛擬主機(jī)的主機(jī)名代替;
- $hostname:Nginx服務(wù)運(yùn)行所在主機(jī)的主機(jī)名;
- $remote_addr:客戶端IP;
- $remote_prot:客戶端端口;
- $remote_user:使用用戶認(rèn)證時(shí)客戶端用戶輸入的用戶名;
- $request_filename:用戶請(qǐng)求中的URI經(jīng)過(guò)本地root或alias轉(zhuǎn)換后映射的本地文件路徑;
- $request_method:請(qǐng)求方法,GET、POST、PUT;
- $server_addr:服務(wù)器地址;
- $server_name:服務(wù)器名稱;
- $server_port:服務(wù)器端口;
- $server_protocol:服務(wù)器向客戶端發(fā)送響應(yīng)時(shí)的協(xié)議,如http/1.1、http/1.0;
- $scheme:在請(qǐng)求中使用scheme。如http://xxx.com中的http;
- $http_HEADER:匹配請(qǐng)求報(bào)文中指定的HEADER;
- $http_host:匹配請(qǐng)求報(bào)文中的host首部。
4.2 http狀態(tài)碼
http狀態(tài)碼是用以表示網(wǎng)頁(yè)服務(wù)器HTTP響應(yīng)狀態(tài)的3位數(shù)字代碼。
可通過(guò)查看HTTP狀態(tài)碼來(lái)判斷服務(wù)器狀態(tài),常見(jiàn)的有404、502等。
- 301:永久移動(dòng),被請(qǐng)求的資源已被永久移動(dòng)位置;
- 302:請(qǐng)求的資源限制臨時(shí)從不同的URI響應(yīng)請(qǐng)求;
- 305:使用代理,被請(qǐng)求的資源必須通過(guò)指定的代理才能訪問(wèn);
- 307:臨時(shí)跳轉(zhuǎn),被請(qǐng)求的資源在臨時(shí)從不同的URL響應(yīng)請(qǐng)求;
- 400:錯(cuò)誤請(qǐng)求;
- 402:需要付款,預(yù)留狀態(tài)碼,用于將來(lái)一些數(shù)字貨幣或者微支付;
- 403:禁止訪問(wèn),服務(wù)器已理解請(qǐng)求,但拒絕執(zhí)行它;
- 404:找不到對(duì)象,請(qǐng)求失敗,資源不存在;
- 406:不可接受的,請(qǐng)求的資源內(nèi)容特性無(wú)法滿足請(qǐng)求頭部中的條件,因而無(wú)法生成響應(yīng)實(shí)體;
- 408:請(qǐng)求超時(shí);
- 409:沖突,由于和被請(qǐng)求的資源的當(dāng)前狀態(tài)之間存在沖突,請(qǐng)求無(wú)法完成;
- 410:遺失的,被請(qǐng)求的資源在服務(wù)器上已經(jīng)不再可用,而且沒(méi)有任何已知的轉(zhuǎn)發(fā)地址;
- 413:響應(yīng)實(shí)體太大,服務(wù)器拒絕處理當(dāng)前請(qǐng)求,請(qǐng)求超過(guò)服務(wù)器所能處理和允許的最大值;
- 417:期望失敗。在請(qǐng)求頭 Expect 中指定的預(yù)期內(nèi)容無(wú)法被服務(wù)器滿足;
- 418:我是一個(gè)茶壺。超文本咖啡罐控制協(xié)議,但是并沒(méi)有被實(shí)際的HTTP服務(wù)器實(shí)現(xiàn);
- 420:方法失效;
- 422:不可處理的實(shí)體。請(qǐng)求格式正確,但是由于含有語(yǔ)義錯(cuò)誤,無(wú)法響應(yīng);
- 500:服務(wù)器內(nèi)部錯(cuò)誤。服務(wù)器遇到了一個(gè)未曾預(yù)料的狀況,導(dǎo)致了它無(wú)法完成對(duì)請(qǐng)求的處理;
- 502:請(qǐng)求后端失?。?/li>
- 504:請(qǐng)求成功,但是響應(yīng)超時(shí)。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
nginx配置https加密訪問(wèn)的詳細(xì)教程
這篇文章主要介紹了nginx配置https加密訪問(wèn)的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
為Nginx自定義404,502錯(cuò)誤頁(yè)面的方法
為Nginx自定義404,502錯(cuò)誤頁(yè)面的方法,需要的朋友可以參考下。2010-12-12
Nginx安裝后/etc/nginx/conf.d下沒(méi)有default.conf的解決
nginx.conf是nginx默認(rèn)加載的配置文件 通過(guò)nginx -V可以看nginx默認(rèn)配置文件路徑,本文主要介紹了Nginx安裝后/etc/nginx/conf.d下沒(méi)有default.conf的解決,感興趣的可以了解一下2023-11-11
nginx進(jìn)行端口轉(zhuǎn)發(fā)的實(shí)現(xiàn)
本文主要介紹了nginx進(jìn)行端口轉(zhuǎn)發(fā)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Nginx設(shè)置Referer來(lái)防止盜圖的實(shí)現(xiàn)方法
這篇文章主要介紹了Nginx設(shè)置Referer來(lái)防止盜圖的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
詳解Nginx靜態(tài)服務(wù)配置(root和alias指令)
這篇文章主要介紹了詳解Nginx靜態(tài)服務(wù)配置(root和alias指令),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Windows設(shè)置nginx啟動(dòng)端口的方法
在服務(wù)器配置與開發(fā)過(guò)程中,nginx 作為一款高效的 HTTP 和反向代理服務(wù)器,被廣泛應(yīng)用,而在 Windows 系統(tǒng)中,合理設(shè)置 nginx 的啟動(dòng)端口,是確保其正常運(yùn)行與滿足業(yè)務(wù)需求的關(guān)鍵步驟,本文將詳細(xì)介紹 Windows 設(shè)置 nginx 啟動(dòng)端口的方法,需要的朋友可以參考下2025-02-02

