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 源
安裝說明:
在新計算機上首次安裝nginx之前,需要設置nginx軟件包存儲庫。 之后,您可以從存儲庫安裝和更新nginx。
要設置yum存儲庫,請創(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的詳細信息,包括模塊,安裝目錄、用戶等 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 //查看版本號 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 //啟動并設置開機啟動 [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 ##指定錯誤日志 --lock-path=/var/lock/nginx.lock ##指定lock文件(進程鎖文件路徑)Nginx進程鎖(lock file)是用于控制Nginx進程并發(fā)性的一種機制。在Nginx啟動時,會創(chuàng)建一個進程鎖文件,用于記錄Nginx主進程的PID,并避免多個Nginx進程同時運行。 --pid-path=/run/nginx.pid ##指定pid文件 --http-client-body-temp-path=/var/lib/nginx/body ##設定http客戶端請求臨時文件路徑(客戶端請求體臨時文件路徑) #作用:HTTP客戶端請求體臨時文件用于存儲客戶端請求體的臨時數(shù)據(jù),通常情況下,當客戶端上傳的數(shù)據(jù)比較大時,Nginx會將請求體存儲在臨時文件中,以避免內(nèi)存不足的問題 ? --http-fastcgi-temp-path=/var/lib/nginx/fastcgi ##設定http fastcgi臨時文件路徑(用于設置FastCGI臨時文件路徑) #FastCGI是一種Web服務器和應用程序之間通信的協(xié)議,它可以用于將動態(tài)生成的Web頁面的生成過程交給獨立的FastCGI進程處理,從而提高Web服務器的性能和可靠性。在處理FastCGI請求時,Nginx會使用FastCGI臨時文件來存儲和傳遞FastCGI進程生成的數(shù)據(jù)。 ? --http-proxy-temp-path=/var/lib/nginx/proxy ##設定http代理臨時文件路徑(用于設置代理模塊臨時文件路徑) #代理模塊是Nginx中的一個核心模塊,用于將客戶端的請求轉(zhuǎn)發(fā)到后端服務器,并將后端服務器的響應返回給客戶端。在處理代理請求時,Nginx會使用代理模塊臨時文件來存儲和傳遞代理請求的數(shù)據(jù)。 ? --http-scgi-temp-path=/var/lib/nginx/scgi ##設定http scgi模塊臨時路徑(用于設置SCGI(Simple Common Gateway Interface)模塊臨時文件路徑) #SCGI是一種Web服務器和應用程序之間通信的協(xié)議,類似于FastCGI。在處理SCGI請求時,Nginx會使用SCGI模塊臨時文件來存儲和傳遞SCGI進程生成的數(shù)據(jù)。 ? --http-uwsgi-temp-path=/var/lib/nginx/uwsgi ##設定http uwsgi臨時文件路徑(用于設置uWSGI(Universal Web Server Gateway Interface)模塊臨時文件路徑) #uWSGI是一種Web服務器和應用程序之間通信的協(xié)議,類似于FastCGI和SCGI。在處理uWSGI請求時,Nginx會使用uWSGI模塊臨時文件來存儲和傳遞uWSGI進程生成的數(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自上次啟動以來的狀態(tài)信息模塊,可以通過HTTP接口獲取。 --with-http_realip_module #允許從請求頭中更改客戶端的IP地址值,用于反向代理等場景,默認為關(guān)閉狀態(tài)。 --with-http_auth_request_module #實現(xiàn)基于子請求結(jié)果的客戶端授權(quán),如果子請求返回2xx響應代碼,則允許訪問;如果返回401或403響應代碼,則拒絕訪問;其他響應代碼則視為錯誤。 ?--with-http_addition_module #作為一個輸出過濾器,支持不完全緩沖、分部分響應請求。 --with-http_dav_module #增加PUT、DELETE、MKCOL、COPY和MOVE方法,用于WebDAV協(xié)議支持,默認為關(guān)閉狀態(tài),需要編譯開啟。 ?--with-http_geoip_module #使用預編譯的MaxMind數(shù)據(jù)庫解析客戶端IP地址,得到變量值。 ?--with-http_gunzip_module #用于解壓具有“Content-Encoding: gzip”頭的響應,以支持不支持“gzip”編碼方法的客戶端。 ?--with-http_gzip_static_module #在線實時壓縮輸出數(shù)據(jù)流,以降低網(wǎng)絡帶寬消耗。 ?--with-http_spdy_module #啟用SPDY協(xié)議支持,可以縮短網(wǎng)頁的加載時間。 ?--with-http_sub_module #允許在Nginx響應中用一些其他文本替換指定的文本,用于文本替換和重定向等場景。 ?--with-http_xslt_module #用于過濾和轉(zhuǎn)換XML請求。 ?--with-mail #啟用POP3/IMAP4/SMTP代理模塊支持。 ?--with-mail_ssl_module #啟用ngx_mail_ssl_module支持,用于支持SSL/TLS加密的SMTP/POP3/IMAP4協(xié)議。 ?#這些選項可以根據(jù)具體需求進行選擇和配置,以滿足不同的應用場景。需要注意的是,一些模塊默認是關(guān)閉的,需要通過編譯時的選項進行開啟。同時,這些選項需要在編譯Nginx之前進行設置。 ? --with 表示在編譯過程中需要給nginx添加的模塊 --without 表示編譯nginx時默認該模塊是添加進去的當使用這個參數(shù)時表示將默認編譯的模塊移除
//查看 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 ? //只查看版本號 [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ù)設置
user nginx;  #設置nginx使用的用戶
worker_processes  4;          #設置nginx啟動進程的數(shù)量,一般設置成與邏輯cpu數(shù)量相同 
error_log  logs/error.log;    #指定錯誤日志 
pid        /var/run/nginx.pid; 
events { 
    worker_connections  1024; #設置一個進程的最大并發(fā)連接數(shù) 
}
?
# http 服務相關(guān)設置 
http { 
    include      mime.types; #關(guān)聯(lián)mime類型,關(guān)聯(lián)資源的媒體類型
    default_type  application/octet-stream; #根據(jù)文件的后綴來匹配相應的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]:訪問時間,格式為本地時間。
    "request":HTTP請求方法、URI和協(xié)議版本。
    status:HTTP響應狀態(tài)碼。
    body_bytes_sent:發(fā)送給客戶端的數(shù)據(jù)量,不包括響應頭。
    "$http_referer":HTTP Referer頭信息,表示訪問來源。
    "http_user_agent":HTTP User-Agent頭信息,表示客戶端瀏覽器或應用程序的信息。
    "http_x_forwarded_for":HTTP X-Forwarded-For頭信息,表示客戶端真實IP地址,用于反向代理場景。
    
    
    access_log  /var/log/nginx/access.log  main;    #設置訪問日志的位置和格式 
    sendfile          on;      # 用于開啟文件高效傳輸模式,一般設置為on,若nginx是用來進行磁盤IO負載應用時,可以設置為off,降低系統(tǒng)負載
    tcp_nopush        on;      # 減少網(wǎng)絡報文段數(shù)量,當有數(shù)據(jù)時,先別著急發(fā)送,確保數(shù)據(jù)包已經(jīng)裝滿數(shù)據(jù),避免了網(wǎng)絡擁塞
    tcp_nodelay       on;      # 提高I/O性能,禁用Nagle算法,確保數(shù)據(jù)盡快發(fā)送,提高可數(shù)據(jù)傳輸效率 (Nagle算法是一種常用的TCP網(wǎng)絡傳輸優(yōu)化算法,它將多個小的數(shù)據(jù)包合并成一個大的數(shù)據(jù)包發(fā)送,以減少網(wǎng)絡傳輸?shù)拈_銷)
    
    gzip              on;      #是否開啟gzip壓縮,將注釋去掉開啟 
    keepalive_timeout  65;     #設置長連接的超時時間,即:請求完成之后還要保持連接多久
# 虛擬服務器的相關(guān)設置 
    server { 
        listen      80;        #設置監(jiān)聽的端口 
        server_name  localhost;        #設置綁定的主機名、域名或ip地址 
        charset koi8-r;      # 設置編碼字符 
        location / { 
            root  /var/www/nginx;           #設置服務器默認網(wǎng)站的根目錄位置,需要手動創(chuàng)建
            index  index.html index.htm;    #設置默認打開的文檔 
            } 
        error_page  500 502 503 504  /50x.html; #設置錯誤信息返回頁面 
        location = /50x.html { 
            root  html;        #這里的絕對位置是/usr/local/nginx/html
        } 
    } 
 }
1、nginx.conf的組成:
- nginx.conf一共由三部分組成,分別為:全局塊、events塊、http塊。在http塊中又包含http全局塊、多個server塊。
 - 每個server塊中又包含server全局塊以及多個location塊。在統(tǒng)一配置塊中嵌套的配置快,各個之間不存在次序關(guān)系。
 
2、Nginx的組成:
- nginx的二進制可執(zhí)行文件:用來啟動關(guān)閉nginx的,由各個模塊編譯出來的
 
#—/usr/local/nginx/sbin/nginx
- nginx.conf配置文件:控制nginx
 - access.log訪問日志:記錄每條http請求
 - error.log 錯誤日志:定位問題
 
2.6. 檢測 Nginx 配置文件是否正確
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -t ? //最簡單的方法讓服務器全局都能使用nginx命令 [root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx #注意一定要是絕對路徑?。。? ? [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. 啟動 Nginx 服務
[root@localhost ~]# /usr/local/nginx/sbin/nginx //或者直接 nginx
2.8. 通過 Nginx 命令控制 Nginx 服務
nginx -c /path/nginx.conf - # 以特定目錄下的配置文件啟動nginx nginx -s reload # 修改配置后重新加載生效 nginx -s stop # 快速停止nginx nginx -s quit # 正常停止nginx nginx -t # 測試當前配置文件是否正確 nginx -t -c /path/to/nginx.conf # 測試特定的nginx配置文件是否正確
注意:
nginx -s reload 命令加載修改后的配置文件,命令下達后發(fā)生如下事件:
- 1.Nginx的master進程檢查配置文件的正確性,若是錯誤則返回錯誤信息,nginx繼續(xù)采用原配置文件進行工作(因為worker未受到影響)
 - 2.如果配置文件沒問題,則Nginx會啟動新的worker進程,并且采用新的配置文件
 - 3.Nginx將新的請求分配新的worker進程
 - 4.Nginx等待以前的worker進程的全部請求已經(jīng)都返回后,關(guān)閉相關(guān)worker進程
 - 5.重復上面過程,直到全部舊的worker進程都被關(guān)閉掉
 
3. Nginx 日志文件詳解
- nginx 日志文件分為 log_format 和 access_log 兩部分
 - log_format 定義記錄的格式,其語法格式為:
# log_format 樣式名稱 樣式詳情 
配置文件中默認有,編譯安裝的nginx默認是關(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] | 訪問時間,格式為本地時間 | 
| “request” | HTTP請求方法、URI和協(xié)議版本 | 
| status | HTTP響應狀態(tài)碼 | 
| body_bytes_sent | 發(fā)送給客戶端的數(shù)據(jù)量,不包括響應頭 | 
| “$http_referer” | HTTP Referer頭信息,表示訪問來源 | 
| “http_user_agent” | HTTP User-Agent頭信息,表示客戶端瀏覽器或應用程序的信息 | 
| “http_x_forwarded_for” | HTTP X-Forwarded-For頭信息,表示客戶端真實IP地址,用于反向代理場景 | 
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
 Keepalived+Nginx+Tomcat 實現(xiàn)高可用Web集群的示例代碼
這篇文章主要介紹了Keepalived+Nginx+Tomcat 實現(xiàn)高可用Web集群的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
 Nginx實現(xiàn)自簽名SSL證書生成與配置實現(xiàn)
本文主要介紹了Nginx實現(xiàn)自簽名SSL證書生成與配置實現(xiàn),文章將詳細介紹生成自簽名SSL證書的步驟,具有一定的參考價值,感興趣的可以了解一下2023-09-09

