nginx學習從入門到精通
- 為什么使用Nignx
- Nginx基本用法
- Nginx的主組態檔
- Nginx的其他檔案
- Nginx反向代理
- Nginx的location路徑映射
- Nginx的負載均衡
- 輪詢
- 權重
- ip_hash
- Nginx動靜分離
- 動態資源代理
- 靜態資源代理
- 擴展內容(nginx集群)
為什么使用Nignx
- Nginx是一個高性能的HTTP和反向代理服務器
- 記憶體消耗小:開啟10個nginx才占150M記憶體 ,nginx處理靜態檔案好,耗費記憶體少
- 跨平臺、配置簡單,非阻塞、高并發連接:處理2-3萬并發連接數,官方監測能支持5萬并發
Nginx基本用法
開啟nginx:sudo service nginx start
重啟nginx:sudo service nginx reload
關閉nginx:nginx -s stop
Nginx的主組態檔
我們一般把nginx.conf放在/etc/nginx/nginx.conf
user nginx;
# worker_processes的數值越大,Nginx的并發能力就越強
worker_processes 1;
# error_log代表Nginx錯誤日志存放的位置
error_log /var/log/nginx/error.log warn;
# pid是Nginx運行的一個標識
pid /var/run/nginx.pid;
events {
# worker_connections的數值越大,Nginx的并發能力就越強
worker_connections 1024;
}
http {
# include代表引入一個外部檔案
# mime.types中存放著大量媒體型別
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#引入了conf.d下以.conf為結尾的組態檔
include /etc/nginx/conf.d/*.conf;
}
Nginx的其他檔案
其他組態檔放在/etc/nginx/conf.d/*.conf
基本配置資訊(這樣是訪問靜態資源的)
nginx ssl簡單配置(https認證)-- 點我查看
server {
# listen代表Nginx監聽ipv4的埠號
listen 80;
# listen代表Nginx監聽ipv6的埠號
listen [::]:80;
# server_name代表Nginx接受請求的IP
server_name localhost;
location / {
# root:將接受到的請求根據/usr/share/nginx/html去查找靜態資源
root /usr/share/nginx/html;
# index:默認去上述的路徑中找到index.html或index.htm
index index.html index.htm;
}
#錯誤跳轉頁面(沒有不耽誤)
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Nginx反向代理
學習反向代理時,應該先了解正向代理
正向代理:
1.正向代理服務是由客戶端設立的
2.客戶端了解代理服務器和目標服務器都是誰
3.幫助咱們實作突破訪問權限,提高訪問的速度,對目標服務器隱藏客戶端的ip地址

反向代理
1.反向代理服務器是配置在服務端的
2.客戶端不知道訪問的到底是哪一臺服務器
3.達到負載均衡,并且可以隱藏服務器真正的ip地址

反向代理代碼實作:
修改/etc/nginx/conf.d/*.conf
這里我們訪問我們的tomcat服務器
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
# 反向代理的服務器:127.0.0.1可以換成域名或者ip
proxy_pass http://127.0.0.1:8080/;
}
}
Nginx的location路徑映射
1、 = 匹配
location = / {
#精準匹配,主機名后面不能帶能和字串
}
2、 通用匹配
location /xxx {
#匹配所有以/xxx開頭的路徑
}
3、 匹配開頭路徑
location ^~ /xxx/xx {
#匹配所有以/xxx/xx開頭的路徑
}
4、正則匹配
location ~ /xxx {
#匹配所有以/xxx開頭的路徑
}
5、匹配結尾路徑
location ~* \.(gif/jpg/png)$ {
#匹配以.gif、.jpg或者.png結尾的路徑
}
6、 通用匹配
location / {
#通用匹配,匹配所有請求
}
Nginx的負載均衡
Nginx有三種負載均和的策略
- 輪詢:
將客戶端發起請求,平均分配給每一臺服務器 - 權重:
會將客戶端的請求,根據服務器的權重值不同,分配不同的數量 - ip_hash:
基于發起請求的客戶端的ip地址不同,他始侄訓將請求發送到指定的服務器上
就是說如果這個客戶端的請求的ip地址不變,那么處理請求的服務器將一直是同一個
輪詢
upstream daili_server{
server localhost:8080; #服務器IP或域名
server localhost:8081; #服務器IP或域名
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://daili_server/; #負載均衡
}
}
權重
upstream daili_server{
server localhost:8080 weight=10; #服務器IP或域名
server localhost:8081 weight=2; #服務器IP或域名
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://daili_server/; #負載均衡
}
}
ip_hash
upstream daili_server{
ip_hash;
server localhost:8080; #服務器IP或域名
server localhost:8081; #服務器IP或域名
}
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://daili_server/; #負載均衡
}
}
Nginx動靜分離
提高用戶訪問靜態代碼的速度,降低對后臺應用訪問
我們將靜態資源放到nginx中,動態資源轉發到tomcat服務器中
Nginx的并發能力公式:
worker_processes * worker_connections / 4|2 = Nginx最終的并發能力
動態資源需要/4,靜態資源只需要/2
動態資源代理
location / {
proxy_pass 路徑;
}
靜態資源代理
location / {
root 靜態資源路徑;
index 默認訪問路徑下的什么資源;
autoindex on;#可以不寫,寫了則代表展示靜態資源的全部內容,以串列的形式展開
}
擴展內容(nginx集群)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/202896.html
標籤:其他
