CentOS?7.6安裝與Nginx的配置文件詳解
一、安裝Nginx需要的環(huán)境庫(kù)
注:所有命令均在root權(quán)限下執(zhí)行
- 項(xiàng)目首先我們需要安裝gcc、gcc-c++、zlib、pcre 和openssl。
- 安裝gcc gcc-c++;
yum install -y gcc gcc-c++
- 下載安裝pcre;
cd /usr/local/ wget http://downloads.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz tar -zxvf pcre-8.45.tar.gz cd pcre-8.45 ./configure make && make install
- 下載安裝openssl;
cd /usr/local/ wget https://www.openssl.org/source/openssl-1.1.1t.tar.gz --no-check-certificate tar -zxvf openssl-1.1.1t.tar.gz cd openssl-1.1.1t ./config make && make install
注:wget https://www.openssl.org/source/openssl-1.1.1t.tar.gz 后面記得一定加上–no-check-certificate,不然要報(bào)錯(cuò)。顯示www.openssl.org上頒發(fā)的證書(shū)已經(jīng)過(guò)期無(wú)法驗(yàn)證,手動(dòng)狗頭。
- 下載安裝zlib;
cd /usr/local/ wget http://zlib.net/zlib-1.2.13.tar.gz tar -zxvf zlib-1.2.13.tar.gz cd zlib-1.2.13 ./configure make && make install
二、安裝Ngnix
- 下載安裝Nginx;
cd /usr/local/ wget http://nginx.org/download/nginx-1.23.3.tar.gz tar -zxvf nginx-1.23.3.tar.gz cd nginx-1.23.3 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.45 make && make install
- 創(chuàng)建ssl的軟鏈接,不然啟動(dòng)nginx會(huì)報(bào)錯(cuò);
ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1 ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
報(bào)錯(cuò)圖片如下:
三、啟動(dòng)Nginx
- 啟動(dòng)nginx
/usr/local/nginx/sbin/nginx
測(cè)試一下nginx,從別臺(tái)機(jī)器訪問(wèn)一下服務(wù)器的IP,出現(xiàn)“Welcome to nginx!”頁(yè)面就說(shuō)明成功了;如果訪問(wèn)不到頁(yè)面但是可以ping通服務(wù)器的話可能是開(kāi)啟了防火墻,關(guān)閉就行。
關(guān)閉防火墻
systemctl stop firewalld.service
關(guān)閉防火墻開(kāi)機(jī)自啟
systemctl disable firewalld.service
四、介紹一下Nginx命令
- 啟動(dòng)nginx服務(wù)
/usr/local/nginx/sbin/nginx
- 重啟nginx服務(wù)
/usr/local/nginx/sbin/nginx –s reload
- 停止nginx服務(wù)
/usr/local/nginx/sbin/nginx –s stop
- 強(qiáng)制關(guān)閉nginx服務(wù)
pkill nginx
五、介紹一下Nginx的配置
- nginx.conf配置文件介紹
#nginx配置 #user nobody; worker_processes 1; #服務(wù)器并發(fā)處理服務(wù)關(guān)鍵配置 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; #最大連接數(shù)為 1024. } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; include mime.types; default_type application/octet-stream; sendfile on; tcp_nopush on; keepalive_timeout 65; #gzip on; #http頭壓縮 #正向代理配置 server { listen 8080; # 代理監(jiān)聽(tīng)端口 resolver 114.114.114.114; #代理DNS配置 #charset koi8-r; access_log /home/lich/logs/fproxy.access.log; #accesslog輸出路徑 error_log /home/lich/logs/fproxy.error.log; #errorlog輸出路徑 location / { proxy_pass $scheme://$host$request_uri; # 配置正向代理參數(shù) proxy_set_header Host $http_host; # 解決如果URL中帶"."后Nginx 503錯(cuò)誤 proxy_buffers 256 4k; # 配置緩存大小 proxy_max_temp_file_size 0; # 關(guān)閉磁盤(pán)緩存讀寫(xiě)減少I(mǎi)/O proxy_connect_timeout 30; # 代理連接超時(shí)時(shí)間 # 配置代理服務(wù)器HTTP狀態(tài)緩存時(shí)間 proxy_cache_valid 200 302 10m; proxy_cache_valid 301 1h; proxy_cache_valid any 1m; } } #反向代理配置 server { listen 80; server_name test.test.com; #代理轉(zhuǎn)發(fā)域名配置 access_log /home/lich/logs/rproxy.access.log; error_log /home/lich/logs/rproxy.error.log; location / { proxy_pass http://172.16.113.1:8001; #代理到后段實(shí)際應(yīng)用服務(wù)器地址 index index.html index.htm index.jsp; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
- 監(jiān)聽(tīng)配置用法
listen *:80 | *:8080 #監(jiān)聽(tīng)所有80端口和8080端口 listen IP_address:port #監(jiān)聽(tīng)指定的地址和端口號(hào) listen IP_address #監(jiān)聽(tīng)指定ip地址所有端口 listen port #監(jiān)聽(tīng)該端口的所有IP連接
- server_name:基于名稱(chēng)的虛擬主機(jī)配置
- 語(yǔ)法格式如下:
# server_name name ...;
- 對(duì)于name 來(lái)說(shuō),可以只有一個(gè)名稱(chēng),也可以有多個(gè)名稱(chēng),中間用空格隔開(kāi)。而每個(gè)名字由兩段或者三段組成,每段之間用“.”隔開(kāi)。
server_name test.com www.test.com
- 可以使用通配符“*”,但通配符只能用在由三段字符組成的首段或者尾端,或者由兩端字符組成的尾端。
server_name *.test.com www.test.*
- 還可以使用正則表達(dá)式,用“~”作為正則表達(dá)式字符串的開(kāi)始標(biāo)記。
server_name ~^www\d+\.test\.com$;
- server_name:基于IP地址的虛擬主機(jī)配置
#語(yǔ)法結(jié)構(gòu)和基于域名匹配一樣,而且不需要考慮通配符和正則表達(dá)式的問(wèn)題。
server_name 192.168.1.1
- proxy_pass
該指令用于設(shè)置被代理服務(wù)器的地址??梢允侵鳈C(jī)名稱(chēng)、IP地址加端口號(hào)的形式
# proxy_pass URL; # URL 為被代理服務(wù)器的地址,可以包含傳輸協(xié)議、主機(jī)名稱(chēng)或IP地址加端口號(hào),URI等。 proxy_pass http://www.test.com/uri;
- index
該指令用于設(shè)置網(wǎng)站的默認(rèn)首頁(yè)。
#index filename ...; #后面的文件名稱(chēng)可以有多個(gè),中間用空格隔開(kāi)。 index index.html index.jsp;
六 ngxin負(fù)載均衡
- 輪詢算法負(fù)載均衡
upstream OrdinaryPolling { server 172.16.113.1:8081; server 172.16.113.1:8082; } server { listen 80; server_name test.test.com; access_log /home/lich/logs/rproxy_slb.access.log; error_log /home/lich/logs/rproxy_slb.error.log; location / { proxy_pass http://OrdinaryPolling; index index.html index.htm index.jsp; # deny ip # allow ip } }
- 基于比例加權(quán)輪詢負(fù)載均衡
upstream OrdinaryPolling { server 172.16.113.1:8081 weight=2; server 172.16.113.1:8082 weight=5; } server { listen 80; server_name test.test.com; access_log /home/lich/logs/rproxy_slb.access.log; error_log /home/lich/logs/rproxy_slb.error.log; location / { proxy_pass http://OrdinaryPolling; # index index.html index.htm index.jsp; # deny ip # allow ip } }
- 基于IP路由負(fù)載均衡
在 upstream 指令塊中增加了ip_hash 指令。該指令就是告訴 nginx 服務(wù)器,同一個(gè) IP 地址客戶端發(fā)送的請(qǐng)求都將分發(fā)到同一個(gè) Tomcat 服務(wù)器進(jìn)行處理。
upstream OrdinaryPolling { server 172.16.113.1:8081 weight=2; server 172.16.113.1:8082 weight=5; ip_hash; } server { listen 80; server_name test.test.com; access_log /home/lich/logs/rproxy_slb.access.log; error_log /home/lich/logs/rproxy_slb.error.log; location / { proxy_pass http://OrdinaryPolling; # index index.html index.htm index.jsp; # deny ip # allow ip } }
- 基于服務(wù)器響應(yīng)時(shí)間負(fù)載均衡
根據(jù)服務(wù)器處理請(qǐng)求的時(shí)間來(lái)進(jìn)行負(fù)載,處理請(qǐng)求越快,也就是響應(yīng)時(shí)間越短的優(yōu)先分配。
upstream OrdinaryPolling { server 172.16.113.1:8081 weight=2; server 172.16.113.1:8082 weight=5; fair; } server { listen 80; server_name test.test.com; access_log /home/lich/logs/rproxy_slb.access.log; error_log /home/lich/logs/rproxy_slb.error.log; location / { proxy_pass http://OrdinaryPolling; # index index.html index.htm index.jsp; # deny ip # allow ip } }
到此這篇關(guān)于CentOS 7.6安裝Nginx及配置文件詳解的文章就介紹到這了,更多相關(guān)CentOS 7.6安裝Nginx及配置文件詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
WIN2003服務(wù)器 IIS經(jīng)常停止響應(yīng)問(wèn)題解決辦法參考
前些天,朋友的服務(wù)器出現(xiàn)問(wèn)題了,現(xiàn)象就是IIS經(jīng)常停止響應(yīng),restart iis 既正常!經(jīng)過(guò)一番努力問(wèn)題解決,把方法和相關(guān)參考總結(jié)供各位參考2011-10-10Windows?Server?2019?FTP服務(wù)器搭建的實(shí)現(xiàn)步驟
FTP服務(wù)器的主要功能是通過(guò)FTP協(xié)議讓用戶能夠在服務(wù)器上進(jìn)行文件的上傳和下載操作,本文主要介紹了Windows?Server?2019?FTP服務(wù)器搭建的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04由于這臺(tái)計(jì)算機(jī)沒(méi)有終端服務(wù)器客戶端訪問(wèn)許可證,遠(yuǎn)程會(huì)話被中斷
問(wèn)題:使用[遠(yuǎn)程桌面連接]到win2003 server 消息:[由于這臺(tái)計(jì)算機(jī)沒(méi)有終端服務(wù)器客戶端訪問(wèn)許可證,遠(yuǎn)程會(huì)話被中斷。請(qǐng)跟服務(wù)器管理員聯(lián)系。] 原因:許可證服務(wù)器中可能沒(méi)有剩余的“每設(shè)備 CAL”來(lái)頒發(fā)。2009-06-06w3wp.exe內(nèi)存占用過(guò)高(網(wǎng)站打不開(kāi),應(yīng)用程序池回收就正常)
服務(wù)器cpu,內(nèi)存正常, 部分網(wǎng)站打不開(kāi),應(yīng)用程序池回收就正常,如何解決?2013-08-08win2003中的w3wp.exe進(jìn)程大量占用cpu資源的各種問(wèn)題解決方法
發(fā)生此問(wèn)題是因?yàn)?Jet 數(shù)據(jù)庫(kù)引擎中存在缺陷。 Microsoft Windows Server 2003 上只會(huì)出現(xiàn)此問(wèn)題。2010-06-06iis 7.5 選中站點(diǎn)提示 錯(cuò)誤: 無(wú)法讀取配置文件的解決方法
今天在本地學(xué)習(xí)配置windows server 2008 r2的時(shí)候,點(diǎn)擊很早以前創(chuàng)建的站點(diǎn)竟然提示錯(cuò)誤: 無(wú)法讀取配置文件,也不知道如何刪除站點(diǎn)重新創(chuàng)建一個(gè)也行,經(jīng)過(guò)摸索方法如下,特分享一下需要的朋友可以參考下2016-06-06阿里云服務(wù)IIS搭建Web網(wǎng)站外網(wǎng)無(wú)法訪問(wèn)的解決方法
本文主要介紹了阿里云服務(wù)IIS搭建Web網(wǎng)站外網(wǎng)無(wú)法訪問(wèn)的解決方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07