一、Nginx簡介
1、什么是nginx
Nginx 是高性能的 HTTP 和反向代理的服務器,處理高并發能力是十分強大的,能經受高負載的考驗,有報告表
明能支持高達 50,000 個并發連接數,
2、反向代理
正向代理:需要在客戶端配置代理服務器進行指定網站訪問,(客戶端配置)反向代理:暴露的是代理服務器地址,隱藏了真實服務器 IP 地址,(服務端配置)
3、負載均衡
負載均衡:增加服務器的數量,然后將請求分發到各個服務器上,將原先請求集中到單個服務器上的情況改為將請求分發到多個服務器上,將負載分發到不同的服務器
4、動靜分離
動靜分離:后臺應用的靜態資源與動態資源分開部署,
二、nginx安裝
1、安裝
# ls
nginx-1.12.2.tar.gz
pcre-8.37.tar.gz
- 安裝 pcre 依賴
1)tar –xvf pcre-8.37.tar.gz
2)./configure
3) make && make install
`檢查版本`pcre-config -version
- 安裝 openssl 、zlib 、 gcc 依賴
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
- 安裝 nginx
1)tar –xvf nginx-1.12.2.tar.gz
2)./configure
3) make && make install
`檢查版本`cd /usr/local/nginx/sbin
./nginx -v
./nginx
- 訪問 ip+80
2、常用命令
- 版本號
./nginx -v
- 啟動
./nginx
- 關閉
./nginx -s stop
- 重啟
./nginx -s reload
3、組態檔
位置:/usr/local/nginx/conf/nginx.conf
全域塊:配置服務器整體運行的配置指令,比如 worker_processes 1; 處理并發數的配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events塊:影響 Nginx服務器與用戶的網路連接,比如 worker_connections 1024; 支持的最大連接數為 1024
events {
worker_connections 1024;
}
http塊:http 全域塊+sever塊
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
三、配置實體
1、反向代理
- 埠轉發
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.22.203:8080/;
}
}
- 路徑轉發
路徑優先級:(location =/index)>(location /xxx/xxx/xxx)>(location ^~/xxx)>(location ~/xxx)
=(location ~*.[jpg|png|gif])>( location /)
location =/index {
#精準匹配,只匹配當前路徑
}
location /xxx/xxx/xxx {
#通用匹配,匹配/xxx開頭的所有路徑
}
location ^~/xxx {
#正則匹配,匹配/xxx開頭的所有路徑
}
location ~/xxx {
#匹配開頭路徑,匹配/xxx開頭的所有路徑
}
location ~*\.[jpg|png|gif] {
#匹配結尾,匹配/xxx開頭的所有路徑
}
2、負載均衡
- 負載均衡
upstream my-server{
server 192.168.22.203:8080;
server 192.168.22.203:8081;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://my-server/;
}
}
-
均衡策略
- 均衡:平均轉發到兩天服務器
upstream my-server{ server 192.168.22.203:8080; server 192.168.22.203:8081; }- 權重:根據權重,處理速度快的權重大,分發的多
upstream my-server{ server 192.168.22.203:8080 weight=10; server 192.168.22.203:8081 weight=2; }- IP Hash:一個ip的請求始終交給一臺服務器
upstream my-server{ ip_hash; server 192.168.22.203:8080; server 192.168.22.203:8081; }
3、動靜分離
- 動態資源代理:占用4個連接數
location / {
proxy_pass 路徑;
}
- 靜態資源代理:占用兩個連接數
location / {
root 靜態資源路徑;
index 默認訪問路徑下的資源;
autoindex on;#展示靜態資源的全部內容,以串列顯示展開
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/115286.html
標籤:其他
下一篇:7-4計算運算式求解答
