01-Nginx安裝
?
Nginx
是一款
輕量級
的
Web
服務(wù)器/
反向代理
服務(wù)器及
電子郵件
(IMAP/POP3)代理服務(wù)器,并在一個BSD-like 協(xié)議下發(fā)行。由俄羅斯的程序設(shè)計師Igor Sysoev所開發(fā),供俄國大型的入口網(wǎng)站及搜索引擎Rambler(俄文:Рамблер)使用。其特點是占有內(nèi)存少,
并發(fā)
能力強,事實上nginx的并發(fā)能力確實在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好,中國大陸使用nginx網(wǎng)站用戶有:
京東
、
新浪
、
網(wǎng)易
、
騰訊
、
淘寶
等。
目錄
01-Nginx安裝
nginx安裝
02-Nginx負(fù)載均衡策略
03-Nginx實現(xiàn)Tomcat負(fù)載均衡集群
04-nginx.conf配置文件
?? ?4.1、nginx.conf介紹文章來源:http://www.zghlxwxcb.cn/news/detail-819917.html
? ? 4.2、常用指令文章來源地址http://www.zghlxwxcb.cn/news/detail-819917.html
nginx安裝
# 0.首先需要有依賴
yum install -y gcc pcre-devel zlib-devel
# 1.下載Nginx
http://nginx.org/en/download.html
# 2.將Nginx上傳到linux中,并解壓縮
tar -zxvf nginx-1.11.1.tar.gz
# 3.在解壓好的nginx目錄中執(zhí)行如下命令:(指定安裝位置)
./configure --prefix=/usr/nginx
# 5.執(zhí)行上述命令后,執(zhí)行如下命令:
make && make install
# 6.編譯完成后進(jìn)入編譯安裝目錄/usr/nginx目錄中查看:
[root@localhost nginx]# ls -l
總用量 4
drwxr-xr-x. 2 root root 4096 10月 14 21:17 conf
drwxr-xr-x. 2 root root 40 10月 14 21:17 html
drwxr-xr-x. 2 root root 6 10月 14 21:17 logs
drwxr-xr-x. 2 root root 19 10月 14 21:17 sbin
# 7.啟動nginx,進(jìn)入nginx安裝目錄的sbin目錄中執(zhí)行:
./nginx
# 8.在windows中瀏覽器訪問,可以看到nginx歡迎頁面:
http://10.15.0.8:80/????????--輸入自己的服務(wù)器地址:nginx端口號
注意:關(guān)閉網(wǎng)絡(luò)防火墻
# 9.關(guān)閉nginx,進(jìn)入nginx安裝目錄的sbin目錄中執(zhí)行:
./nginx -s stop
# 10.nginx配置文件在nginx安裝目錄的conf目錄中:
[root@localhost conf]# ls -l
總用量 60
-rw-r--r--. 1 root root 2656 10月 14 21:17 nginx.conf
.......
注意:nginx.conf為nginx的配置文件,可以在nginx.conf修改nginx默認(rèn)配置
02-Nginx負(fù)載均衡策略
# 1.輪詢
說明: 默認(rèn)策略,每個請求會按時間順序逐一分配到不同的后端服務(wù)器
# 2.weight 權(quán)重
說明: weight參數(shù)用于指定輪詢幾率,weight的默認(rèn)值為1,;weight的數(shù)值與訪問比率成正比
upstream tomcat-servers {
?? ?server localhost:8080 weight=2;
?? ?server localhost:8081;
?? ?server localhost:8082 backup;
}
注意:1.權(quán)重越高分配到需要處理的請求越多。2.此策略可以與least_conn和ip_hash結(jié)合使用主要用于后端服務(wù)器性能不均
# 3.ip_hash 4%3=1
說明:指定負(fù)載均衡器按照基于客戶端IP的分配方式,這個方法確保了相同的客戶端的請求一直發(fā)送到相同的服務(wù)器,以保證session會話。這樣每個訪客都固定訪問一個后端服務(wù)器,可以解決session不能跨服務(wù)器的問題。
upstream tomcat-servers {
?? ??? ?ip_hash; #保證每個訪客固定訪問一個后端服務(wù)器
?? ??? ?server localhost:8080;
?? ??? ?......
?? ?}
# 4.least_conn
說明: 把請求轉(zhuǎn)發(fā)給連接數(shù)較少的后端服務(wù)器。輪詢算法是把請求平均的轉(zhuǎn)發(fā)給各個后端,使它們的負(fù)載大致相同;但是,有些請求占用的時間很長,會導(dǎo)致其所在的后端負(fù)載較高。這種情況下,least_conn這種方式就可以達(dá)到更好的負(fù)載均衡效果。
upstream tomcat-servers{
?? ?least_conn; #把請求轉(zhuǎn)發(fā)給連接數(shù)較少的后端服務(wù)器
?? ?server localhost:8080;
}
03-Nginx實現(xiàn)Tomcat負(fù)載均衡集群
# 0.準(zhǔn)備多個tomcat
tar -zxvf apache-tomcat-8.5.46.tar.gz #解壓縮一個新的tomcat安裝包
mv apache-tomcat-8.5.46 tomcat1 #將名稱改為tomcat1
cp -r tomcat1/ tomcat2 #復(fù)制一份
cp -r tomcat1/ tomcat3 #復(fù)制一份
# 1.此時當(dāng)前目錄中有三個服務(wù)器,如下:
[root@localhost ~]# ls -l
總用量 12248
-rwxrwxrwx. 1 root root 11623939 10月 13 12:25 apache-tomcat-8.5.46.tar.gz
drwxr-xr-x. 9 root root 220 10月 14 21:28 tomcat1
drwxr-xr-x. 9 root root 220 10月 14 21:38 tomcat2
drwxr-xr-x. 9 root root 220 10月 14 21:38 tomcat3
# 2.修改tomcat1端口號:(偽分布式)
vim tomcat1/conf/server.xml,命令修改如下內(nèi)容:
a.<Server port="8001" shutdown="SHUTDOWN"> ---關(guān)閉端口
b.<Connector port="8888" protocol="HTTP/1.1" ---http協(xié)議端口
connectionTimeout="20000"
redirectPort="8443" />
c.<Connector port="10010" protocol="AJP/1.3" redirectPort="8443" /> ---AJP協(xié)議端口
# 3.修改tomcat2端口號:(偽分布式)
vim tomcat2/conf/server.xml,命令修改如下內(nèi)容:
a.<Server port="8002" shutdown="SHUTDOWN">
b.<Connector port="8889" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
c.<Connector port="10011" protocol="AJP/1.3" redirectPort="8443" />
# 4.修改tomcat3端口號:(偽分布式)
vim tomcat2/conf/server.xml,命令修改如下內(nèi)容:
a.<Server port="8003" shutdown="SHUTDOWN">
b.<Connector port="8890" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
c.<Connector port="10012" protocol="AJP/1.3" redirectPort="8443" />
# 5.將多個tomcat啟動:
tomcat1/bin/startup.sh
tomcat2/bin/startup.sh
tomcat3/bin/startup.sh
# 6.查看tomcat是否啟動成功
ps -aux|grep tomcat
# 7.在windows中分別訪問tomcat,都看到主頁代表啟動成功:
http://10.15.0.8:8888/
http://10.15.0.8:8889/
http://10.15.0.8:8890/
注意:這步一定要關(guān)閉網(wǎng)路防火墻
# 8.將多個tomcat配置到nginx的配置文件中:
1).進(jìn)入nginx的sbin目錄關(guān)掉nginx服務(wù)
./nginx -s stop
2).進(jìn)入conf目錄,然后編輯nginx.conf文件
vi nginx.conf
3).在server標(biāo)簽上加入如下配置:
upstream tomcat-servers {
server 192.168.80.130:8090;
server 192.168.80.130:8091;
server 192.168.80.130:8092;
}
4).把配置文件里的如下配置注釋掉(server配置里)
????????location / {
????????????root???html;
????????????index??index.html index.htm;
????????}
5).將配置文件中 location /替換為如下配置:
location / {
proxy_pass http://tomcat-servers;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
# 9.進(jìn)入nginx安裝目錄sbin目錄啟動nginx
./nginx -c /usr/nginx/conf/nginx.conf
# 10.訪問nginx,看到其中一個tomcat畫面:
http://10.15.0.8/
04-nginx.conf配置文件
?? ?4.1、nginx.conf介紹
Nginx的配置文件通常是一個文本文件,通常命名為
nginx.conf
,其內(nèi)容包括以下幾個方面:
-
全局配置:這部分包含了一些全局配置指令,例如
user指令指定Nginx進(jìn)程的運行用戶,
worker_processes指令指定Nginx要啟動的工作進(jìn)程數(shù)等。
-
Events配置:這部分包含了一些事件處理相關(guān)的配置指令,例如
worker_connections指令指定每個工作進(jìn)程可以同時處理的連接數(shù),
use指令可以指定事件模型,例如
use epoll或
use kqueue等。
-
HTTP配置:這部分包含了所有HTTP相關(guān)的配置指令,例如
server指令定義了一個HTTP服務(wù)器,
location指令定義了一個請求的處理位置,
proxy_pass指令可以將請求代理到其他服務(wù)器等。
下面是一個簡單的Nginx配置文件示例:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
????worker_connections 1024;
}
http {
????include mime.types;
????default_type application/octet-stream;
????sendfile on;
????keepalive_timeout 65;
????server {
????????listen 80;
????????server_name example.com;
????????location / {
????????????root /var/www/example.com;
????????????index index.html;
????????}
????????location /api/ {
????????????proxy_pass http://api.example.com/;
????????}
????}
}
? ? 4.2、常用指令
4.2.1、alias: alias指令可以將請求的URI映射到本地文件系統(tǒng)中的另一個路徑,從而實現(xiàn)路徑更換的效果。
例如,可以將
http://example.com/resource
映射到
/var/www/cdn/resource
,可以使用以下配置:
location /resource {
????alias /var/www/cdn/resource;
????index index.html;
}
上面的配置將會把
http://example.com/resource
請求映射到本地文件系統(tǒng)的
/var/www/cdn/resource
路徑下,Nginx會自動查找該目錄下的
index.html
文件,并返回給客戶端。如果沒有找到
index.html
文件,則會返回目錄列表。
需要注意的是,使用
alias
指令時,需要指定完整的本地路徑,且路徑中不能包含正則表達(dá)式。另外,使用
alias
指令可能會影響性能,因為Nginx需要對每個請求進(jìn)行一次路徑替換。如果只是簡單的路徑更換,建議使用
rewrite
指令或在應(yīng)用程序代碼中進(jìn)行處理。
另外還有一種類似的指令
root
,它也可以將請求的URI映射到本地文件系統(tǒng)中的路徑,但是它會將請求的URI和
root
指定的路徑拼接在一起。例如,如果使用以下配置:
location /resource {
????root /var/www/cdn;
????index index.html;
}
則
http://example.com/resource
請求會映射到本地文件系統(tǒng)的
/var/www/cdn/resource
路徑下。
root
指令適用于簡單的靜態(tài)文件服務(wù),但是不能實現(xiàn)路徑更換的效果。
4.2.2、autoindex:展示目錄
首先,需要確保目錄的訪問權(quán)限設(shè)置正確,使得Nginx用戶(一般是
nginx
用戶)有讀取目錄的權(quán)限。然后,在Nginx的配置文件中,找到對應(yīng)的
location
塊,添加
autoindex on
指令即可,例如:
server {
????listen 80;
????server_name example.com;
????location / {
????????root /var/www/example.com;
????????index index.html;
????}
????location /files/ {
????????root /var/www/example.com;
????????autoindex on;
????}
}
上面的配置指定了
/files/
路徑要展示目錄,當(dāng)請求
http://example.com/files/
時,Nginx會自動列出目錄下的所有文件和子目錄,并顯示在網(wǎng)頁上。
可以通過配置
autoindex_format
指令來自定義目錄列表的顯示格式,例如:
location /files/ {
????root /var/www/example.com;
????autoindex on;
????autoindex_format html;
????autoindex_exact_size off;
????autoindex_localtime on;
}
上面的配置指定了使用HTML格式顯示目錄列表,不顯示文件大小的精確值,顯示本地時間而不是GMT時間等。
需要注意的是,展示目錄時,如果目錄下有
index.html
文件,Nginx會優(yōu)先顯示該文件的內(nèi)容,而不是目錄列表。可以通過修改
index
指令來更改默認(rèn)的索引文件,例如:
location /files/ {
????root /var/www/example.com;
????autoindex on;
????index index.html index.php;
}
上面的配置將會在目錄中查找
index.html
和
index.php
文件,如果找到了其中一個文件,則優(yōu)先顯示該文件的內(nèi)容,否則才顯示目錄列表。
4.2.3、proxy_pass: 表示反向代理的地址,可以將客戶端請求轉(zhuǎn)發(fā)至該地址指定的后端服務(wù)進(jìn)行處理。
在?
location?
塊中使用?
proxy_pass?
配置項最常見,示例如下:
location /app/ {
??proxy_pass http://localhost:8000/;
??proxy_set_header Host $host;
??proxy_set_header X-Real-IP $remote_addr;
??proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
以上配置將會將包含?
/app/?
路徑的請求轉(zhuǎn)發(fā)至?
http://localhost:8000/
,并將請求頭?
Host
、
X-Real-IP
、
X-Forwarded-For?
的參數(shù)傳遞給后端服務(wù)。
在?
proxy_pass?
配置項中,可以指定 HTTP、HTTPS、FTP 等協(xié)議,也可以通過域名指定反向代理的目標(biāo)。例如:
?? ?1.使用 HTTPS:
location /app/ {
??proxy_pass https://localhost:8000/;
??...
}
?? ?2.使用域名:
location /app/ {
??proxy_pass http://backend.example.com;
??...
}
需要注意的是,對于?
proxy_pass?
中指定的地址,如果是域名或者其他需要解析的地址,需要確認(rèn) DNS 解析是否正確,同時需要考慮相關(guān)安全方案,例如 SSL/TLS 等。此外,在使用?
proxy_pass?
時還需要考慮反向代理的性能調(diào)優(yōu),例如通過?
keepalive
、
proxy_cache?
等配置項進(jìn)行優(yōu)化,避免反向代理成為系統(tǒng)性能瓶頸。
到了這里,關(guān)于linux(Centos)環(huán)境安裝使用 Nginx的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!