Nginx安裝和配置過程(yum安裝和編譯安裝)
1. Yum 安裝 Nginx
nginx的官方網(wǎng)站:http://www.nginx.org
Nginx版本類型
- Mainline version:主線版,即開發(fā)版
- Stable version:最新穩(wěn)定版,生產(chǎn)環(huán)境上建議使用的版本
- Legacy versions:遺留的老版本的穩(wěn)定版
Yum安裝nginx
配置Yum源的官網(wǎng):http://nginx.org/en/linux_packages.html
1.1. 配置 Nginx 的 Yum 源
安裝說明:
在新計(jì)算機(jī)上首次安裝nginx之前,需要設(shè)置nginx軟件包存儲(chǔ)庫。 之后,您可以從存儲(chǔ)庫安裝和更新nginx。
要設(shè)置yum存儲(chǔ)庫,請(qǐng)創(chuàng)建名為/etc/yum.repos.d/nginx.repo的文件,其中包含以下內(nèi)容:
//安裝yum工具包 [root@localhost ~]# yum -y install yum-utils [root@localhost ~]# 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 [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
1.2. 安裝 Nginx
這里我們用穩(wěn)定版本
[root@localhost ~]# yum install -y nginx [root@localhost ~]# nginx -V //查看nginx的詳細(xì)信息,包括模塊,安裝目錄、用戶等 nginx version: nginx/1.24.0 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' ? [root@localhost ~]# nginx -v //查看版本號(hào) nginx version: nginx/1.24.0 //關(guān)閉防火墻和selinux [root@localhost ~]# getenforce Enforcing [root@localhost ~]# setenforce 0 [root@localhost ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config ? [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld //啟動(dòng)并設(shè)置開機(jī)啟動(dòng) [root@localhost ~]# systemctl start nginx [root@localhost ~]# systemctl enable nginx
瀏覽器輸入ip訪問:
2. Nginx 編譯安裝與配置使用
2.1. 安裝編譯環(huán)境
[root@localhost ~]# yum -y install gcc gcc-c++ pcre pcre-devel gd-devel openssl openssl-devel zlib zlib-devel
2.2. 創(chuàng)建用戶nginx
[root@localhost ~]# useradd nginx && echo "nginx" | passwd --stdin nginx
2.3. 安裝 Nginx
[root@localhost ~]# rpm -qa|grep wget && wget http://nginx.org/download/nginx-1.24.0.tar.gz [root@localhost ~]# mkdir -p /tmp/nginx/ [root@localhost ~]# tar -xzvf nginx-1.24.0.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/nginx-1.24.0/ [root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream [root@localhost nginx-1.24.0]# make && make install
2.4. Nginx 編譯參數(shù)詳解
--prefix=/usr/local/nginx ##指向安裝目錄 --conf-path=/etc/nginx/nginx.conf ##指定配置文件 --http-log-path=/var/log/nginx/access.log ##指定訪問日志 --error-log-path=/var/log/nginx/error.log ##指定錯(cuò)誤日志 --lock-path=/var/lock/nginx.lock ##指定lock文件(進(jìn)程鎖文件路徑)Nginx進(jìn)程鎖(lock file)是用于控制Nginx進(jìn)程并發(fā)性的一種機(jī)制。在Nginx啟動(dòng)時(shí),會(huì)創(chuàng)建一個(gè)進(jìn)程鎖文件,用于記錄Nginx主進(jìn)程的PID,并避免多個(gè)Nginx進(jìn)程同時(shí)運(yùn)行。 --pid-path=/run/nginx.pid ##指定pid文件 --http-client-body-temp-path=/var/lib/nginx/body ##設(shè)定http客戶端請(qǐng)求臨時(shí)文件路徑(客戶端請(qǐng)求體臨時(shí)文件路徑) #作用:HTTP客戶端請(qǐng)求體臨時(shí)文件用于存儲(chǔ)客戶端請(qǐng)求體的臨時(shí)數(shù)據(jù),通常情況下,當(dāng)客戶端上傳的數(shù)據(jù)比較大時(shí),Nginx會(huì)將請(qǐng)求體存儲(chǔ)在臨時(shí)文件中,以避免內(nèi)存不足的問題 ? --http-fastcgi-temp-path=/var/lib/nginx/fastcgi ##設(shè)定http fastcgi臨時(shí)文件路徑(用于設(shè)置FastCGI臨時(shí)文件路徑) #FastCGI是一種Web服務(wù)器和應(yīng)用程序之間通信的協(xié)議,它可以用于將動(dòng)態(tài)生成的Web頁面的生成過程交給獨(dú)立的FastCGI進(jìn)程處理,從而提高Web服務(wù)器的性能和可靠性。在處理FastCGI請(qǐng)求時(shí),Nginx會(huì)使用FastCGI臨時(shí)文件來存儲(chǔ)和傳遞FastCGI進(jìn)程生成的數(shù)據(jù)。 ? --http-proxy-temp-path=/var/lib/nginx/proxy ##設(shè)定http代理臨時(shí)文件路徑(用于設(shè)置代理模塊臨時(shí)文件路徑) #代理模塊是Nginx中的一個(gè)核心模塊,用于將客戶端的請(qǐng)求轉(zhuǎn)發(fā)到后端服務(wù)器,并將后端服務(wù)器的響應(yīng)返回給客戶端。在處理代理請(qǐng)求時(shí),Nginx會(huì)使用代理模塊臨時(shí)文件來存儲(chǔ)和傳遞代理請(qǐng)求的數(shù)據(jù)。 ? --http-scgi-temp-path=/var/lib/nginx/scgi ##設(shè)定http scgi模塊臨時(shí)路徑(用于設(shè)置SCGI(Simple Common Gateway Interface)模塊臨時(shí)文件路徑) #SCGI是一種Web服務(wù)器和應(yīng)用程序之間通信的協(xié)議,類似于FastCGI。在處理SCGI請(qǐng)求時(shí),Nginx會(huì)使用SCGI模塊臨時(shí)文件來存儲(chǔ)和傳遞SCGI進(jìn)程生成的數(shù)據(jù)。 ? --http-uwsgi-temp-path=/var/lib/nginx/uwsgi ##設(shè)定http uwsgi臨時(shí)文件路徑(用于設(shè)置uWSGI(Universal Web Server Gateway Interface)模塊臨時(shí)文件路徑) #uWSGI是一種Web服務(wù)器和應(yīng)用程序之間通信的協(xié)議,類似于FastCGI和SCGI。在處理uWSGI請(qǐng)求時(shí),Nginx會(huì)使用uWSGI模塊臨時(shí)文件來存儲(chǔ)和傳遞uWSGI進(jìn)程生成的數(shù)據(jù)。 ? --with-debug #啟用調(diào)試日志信息,用于調(diào)試和排查問題。 --with-ipv6 #啟用IPv6支持。 --with-http_ssl_module #啟用HTTPS協(xié)議支持的SSL/TLS模塊。 ? --with-http_stub_status_module #啟用Nginx自上次啟動(dòng)以來的狀態(tài)信息模塊,可以通過HTTP接口獲取。 --with-http_realip_module #允許從請(qǐng)求頭中更改客戶端的IP地址值,用于反向代理等場(chǎng)景,默認(rèn)為關(guān)閉狀態(tài)。 --with-http_auth_request_module #實(shí)現(xiàn)基于子請(qǐng)求結(jié)果的客戶端授權(quán),如果子請(qǐng)求返回2xx響應(yīng)代碼,則允許訪問;如果返回401或403響應(yīng)代碼,則拒絕訪問;其他響應(yīng)代碼則視為錯(cuò)誤。 ?--with-http_addition_module #作為一個(gè)輸出過濾器,支持不完全緩沖、分部分響應(yīng)請(qǐng)求。 --with-http_dav_module #增加PUT、DELETE、MKCOL、COPY和MOVE方法,用于WebDAV協(xié)議支持,默認(rèn)為關(guān)閉狀態(tài),需要編譯開啟。 ?--with-http_geoip_module #使用預(yù)編譯的MaxMind數(shù)據(jù)庫解析客戶端IP地址,得到變量值。 ?--with-http_gunzip_module #用于解壓具有“Content-Encoding: gzip”頭的響應(yīng),以支持不支持“gzip”編碼方法的客戶端。 ?--with-http_gzip_static_module #在線實(shí)時(shí)壓縮輸出數(shù)據(jù)流,以降低網(wǎng)絡(luò)帶寬消耗。 ?--with-http_spdy_module #啟用SPDY協(xié)議支持,可以縮短網(wǎng)頁的加載時(shí)間。 ?--with-http_sub_module #允許在Nginx響應(yīng)中用一些其他文本替換指定的文本,用于文本替換和重定向等場(chǎng)景。 ?--with-http_xslt_module #用于過濾和轉(zhuǎn)換XML請(qǐng)求。 ?--with-mail #啟用POP3/IMAP4/SMTP代理模塊支持。 ?--with-mail_ssl_module #啟用ngx_mail_ssl_module支持,用于支持SSL/TLS加密的SMTP/POP3/IMAP4協(xié)議。 ?#這些選項(xiàng)可以根據(jù)具體需求進(jìn)行選擇和配置,以滿足不同的應(yīng)用場(chǎng)景。需要注意的是,一些模塊默認(rèn)是關(guān)閉的,需要通過編譯時(shí)的選項(xiàng)進(jìn)行開啟。同時(shí),這些選項(xiàng)需要在編譯Nginx之前進(jìn)行設(shè)置。 ? --with 表示在編譯過程中需要給nginx添加的模塊 --without 表示編譯nginx時(shí)默認(rèn)該模塊是添加進(jìn)去的當(dāng)使用這個(gè)參數(shù)時(shí)表示將默認(rèn)編譯的模塊移除
//查看 nginx 安裝的模塊 [root@localhost ~]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.24.0 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=/usr/local/nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream ? //只查看版本號(hào) [root@localhost ~]# /usr/local/nginx/sbin/nginx -v nginx version: nginx/1.24.0
2.5. 修改配置文件/etc/nginx/nginx.conf
[root@localhost ~]# cd /etc/nginx/ [root@localhost nginx]# ls fastcgi.conf koi-utf nginx.conf uwsgi_params fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default fastcgi_params mime.types scgi_params win-utf fastcgi_params.default mime.types.default scgi_params.default [root@localhost nginx]# cp nginx.conf{,.bak} [root@localhost nginx]# vim nginx.conf
# 全局參數(shù)設(shè)置 user nginx; #設(shè)置nginx使用的用戶 worker_processes 4; #設(shè)置nginx啟動(dòng)進(jìn)程的數(shù)量,一般設(shè)置成與邏輯cpu數(shù)量相同 error_log logs/error.log; #指定錯(cuò)誤日志 pid /var/run/nginx.pid; events { worker_connections 1024; #設(shè)置一個(gè)進(jìn)程的最大并發(fā)連接數(shù) } ? # http 服務(wù)相關(guān)設(shè)置 http { include mime.types; #關(guān)聯(lián)mime類型,關(guān)聯(lián)資源的媒體類型 default_type application/octet-stream; #根據(jù)文件的后綴來匹配相應(yīng)的MIME類型 log_format main 'remote_addr - remote_user [time_local] "request" ' 'status body_bytes_sent "$http_referer" ' '"http_user_agent" "http_x_forwarded_for"'; #這一段是Nginx的日志格式配置,這一小段忽略: remote_addr:客戶端IP地址。 remote_user:客戶端用戶名。 [time_local]:訪問時(shí)間,格式為本地時(shí)間。 "request":HTTP請(qǐng)求方法、URI和協(xié)議版本。 status:HTTP響應(yīng)狀態(tài)碼。 body_bytes_sent:發(fā)送給客戶端的數(shù)據(jù)量,不包括響應(yīng)頭。 "$http_referer":HTTP Referer頭信息,表示訪問來源。 "http_user_agent":HTTP User-Agent頭信息,表示客戶端瀏覽器或應(yīng)用程序的信息。 "http_x_forwarded_for":HTTP X-Forwarded-For頭信息,表示客戶端真實(shí)IP地址,用于反向代理場(chǎng)景。 access_log /var/log/nginx/access.log main; #設(shè)置訪問日志的位置和格式 sendfile on; # 用于開啟文件高效傳輸模式,一般設(shè)置為on,若nginx是用來進(jìn)行磁盤IO負(fù)載應(yīng)用時(shí),可以設(shè)置為off,降低系統(tǒng)負(fù)載 tcp_nopush on; # 減少網(wǎng)絡(luò)報(bào)文段數(shù)量,當(dāng)有數(shù)據(jù)時(shí),先別著急發(fā)送,確保數(shù)據(jù)包已經(jīng)裝滿數(shù)據(jù),避免了網(wǎng)絡(luò)擁塞 tcp_nodelay on; # 提高I/O性能,禁用Nagle算法,確保數(shù)據(jù)盡快發(fā)送,提高可數(shù)據(jù)傳輸效率 (Nagle算法是一種常用的TCP網(wǎng)絡(luò)傳輸優(yōu)化算法,它將多個(gè)小的數(shù)據(jù)包合并成一個(gè)大的數(shù)據(jù)包發(fā)送,以減少網(wǎng)絡(luò)傳輸?shù)拈_銷) gzip on; #是否開啟gzip壓縮,將注釋去掉開啟 keepalive_timeout 65; #設(shè)置長連接的超時(shí)時(shí)間,即:請(qǐng)求完成之后還要保持連接多久 # 虛擬服務(wù)器的相關(guān)設(shè)置 server { listen 80; #設(shè)置監(jiān)聽的端口 server_name localhost; #設(shè)置綁定的主機(jī)名、域名或ip地址 charset koi8-r; # 設(shè)置編碼字符 location / { root /var/www/nginx; #設(shè)置服務(wù)器默認(rèn)網(wǎng)站的根目錄位置,需要手動(dòng)創(chuàng)建 index index.html index.htm; #設(shè)置默認(rèn)打開的文檔 } error_page 500 502 503 504 /50x.html; #設(shè)置錯(cuò)誤信息返回頁面 location = /50x.html { root html; #這里的絕對(duì)位置是/usr/local/nginx/html } } }
1、nginx.conf的組成:
- nginx.conf一共由三部分組成,分別為:全局塊、events塊、http塊。在http塊中又包含http全局塊、多個(gè)server塊。
- 每個(gè)server塊中又包含server全局塊以及多個(gè)location塊。在統(tǒng)一配置塊中嵌套的配置快,各個(gè)之間不存在次序關(guān)系。
2、Nginx的組成:
- nginx的二進(jìn)制可執(zhí)行文件:用來啟動(dòng)關(guān)閉nginx的,由各個(gè)模塊編譯出來的
#—/usr/local/nginx/sbin/nginx
- nginx.conf配置文件:控制nginx
- access.log訪問日志:記錄每條http請(qǐng)求
- error.log 錯(cuò)誤日志:定位問題
2.6. 檢測(cè) Nginx 配置文件是否正確
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -t ? //最簡單的方法讓服務(wù)器全局都能使用nginx命令 [root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx #注意一定要是絕對(duì)路徑?。?! ? [root@localhost nginx]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
2.7. 啟動(dòng) Nginx 服務(wù)
[root@localhost ~]# /usr/local/nginx/sbin/nginx //或者直接 nginx
2.8. 通過 Nginx 命令控制 Nginx 服務(wù)
nginx -c /path/nginx.conf - # 以特定目錄下的配置文件啟動(dòng)nginx nginx -s reload # 修改配置后重新加載生效 nginx -s stop # 快速停止nginx nginx -s quit # 正常停止nginx nginx -t # 測(cè)試當(dāng)前配置文件是否正確 nginx -t -c /path/to/nginx.conf # 測(cè)試特定的nginx配置文件是否正確
注意:
nginx -s reload
命令加載修改后的配置文件,命令下達(dá)后發(fā)生如下事件:
- 1.Nginx的master進(jìn)程檢查配置文件的正確性,若是錯(cuò)誤則返回錯(cuò)誤信息,nginx繼續(xù)采用原配置文件進(jìn)行工作(因?yàn)閣orker未受到影響)
- 2.如果配置文件沒問題,則Nginx會(huì)啟動(dòng)新的worker進(jìn)程,并且采用新的配置文件
- 3.Nginx將新的請(qǐng)求分配新的worker進(jìn)程
- 4.Nginx等待以前的worker進(jìn)程的全部請(qǐng)求已經(jīng)都返回后,關(guān)閉相關(guān)worker進(jìn)程
- 5.重復(fù)上面過程,直到全部舊的worker進(jìn)程都被關(guān)閉掉
3. Nginx 日志文件詳解
- nginx 日志文件分為 log_format 和 access_log 兩部分
- log_format 定義記錄的格式,其語法格式為:
# log_format 樣式名稱 樣式詳情
配置文件中默認(rèn)有,編譯安裝的nginx默認(rèn)是關(guān)閉該格式的
log_format main 'remote_addr - remote_user [time_local] "request" ' 'status body_bytes_sent "$http_referer" ' '"http_user_agent" "http_x_forwarded_for"';
參數(shù)解釋
變量 | 說明 |
---|---|
remote_addr | 客戶端IP地址 |
remote_user | 客戶端用戶名 |
[time_local] | 訪問時(shí)間,格式為本地時(shí)間 |
“request” | HTTP請(qǐng)求方法、URI和協(xié)議版本 |
status | HTTP響應(yīng)狀態(tài)碼 |
body_bytes_sent | 發(fā)送給客戶端的數(shù)據(jù)量,不包括響應(yīng)頭 |
“$http_referer” | HTTP Referer頭信息,表示訪問來源 |
“http_user_agent” | HTTP User-Agent頭信息,表示客戶端瀏覽器或應(yīng)用程序的信息 |
“http_x_forwarded_for” | HTTP X-Forwarded-For頭信息,表示客戶端真實(shí)IP地址,用于反向代理場(chǎng)景 |
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用nginx正向代理實(shí)現(xiàn)訪問外網(wǎng)
這篇文章主要介紹了使用nginx正向代理實(shí)現(xiàn)讓內(nèi)網(wǎng)主機(jī)通過外網(wǎng)主機(jī)訪問外網(wǎng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12Keepalived+Nginx+Tomcat 實(shí)現(xiàn)高可用Web集群的示例代碼
這篇文章主要介紹了Keepalived+Nginx+Tomcat 實(shí)現(xiàn)高可用Web集群的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Nginx配合php實(shí)現(xiàn)生成實(shí)時(shí)縮略圖功能
這篇文章主要介紹了Nginx配合php實(shí)現(xiàn)生成實(shí)時(shí)縮略圖功能,這在一些特殊場(chǎng)合可能會(huì)要用到,需要的朋友可以參考下2014-10-10Nginx實(shí)現(xiàn)自簽名SSL證書生成與配置實(shí)現(xiàn)
本文主要介紹了Nginx實(shí)現(xiàn)自簽名SSL證書生成與配置實(shí)現(xiàn),文章將詳細(xì)介紹生成自簽名SSL證書的步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09