柚子快報(bào)激活碼778899分享:運(yùn)維 nginx教程
柚子快報(bào)激活碼778899分享:運(yùn)維 nginx教程
nginx教程
一 、 Nginx介紹
1.1引言
為什么要學(xué)習(xí)Nginx
問(wèn)題1:客戶(hù)端到底要將請(qǐng)求發(fā)送給哪臺(tái)服務(wù)器。
問(wèn)題2:如果所有客戶(hù)端的請(qǐng)求都發(fā)送給了服務(wù)器1。
問(wèn)題3:客戶(hù)端發(fā)送的請(qǐng)求可能是申請(qǐng)動(dòng)態(tài)資源的,也有申請(qǐng)靜態(tài)資源。
服務(wù)器搭建集群后。
服務(wù)器搭建集群后,使用Nginx做反向代理。
1.2 Nginx介紹
Nginx是由俄羅斯人研發(fā)的,應(yīng)對(duì)Rambler的網(wǎng)站,并且2004年發(fā)布的第一個(gè)版本。
Nginx的特點(diǎn):
1.穩(wěn)定性極強(qiáng)。7*24小時(shí)不間斷運(yùn)行。
2.Nginx提供了非常豐富的配置實(shí)例。
3.占用內(nèi)存小,并發(fā)能力強(qiáng)。
二、Nginx安裝
2.1 安裝Nginx
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx :latest
container_name: nginx
ports:
- 80:80
volumes:
- /opt/docker_nginx/conf.d/ :/etc/nginx/conf.d_
# 直接安裝
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
./configure --prefix=/usr/local/nginx/ --with-http_ssl_module --with-http_stub_status_module
make && make install
cd /usr/local/nginx
./nginx
參考文章:nginx安裝
2.2 Nginx的配置文件
關(guān)于Nginx的核心配置文件nginx.conf
#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;
#以上統(tǒng)稱(chēng)為全局塊,
#worker_processes他的數(shù)值越大,Nginx的并發(fā)能力就越強(qiáng)
#error_log代表Nginx的錯(cuò)誤日志存放的位置
events {
worker_connections 1024;
}
#events塊
# worker_connections他的數(shù)值越大,Nignx并發(fā)能力越強(qiáng)
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
# location塊
# root:將接收到的請(qǐng)求根據(jù)/usr/share/nginx/html去查找靜態(tài)資源
# index:默認(rèn)去上述的路徑中找到index.html或者index.htm
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#server塊
#listen:代表Nginx監(jiān)聽(tīng)的端口號(hào)localhost:代表Nginx接收請(qǐng)求的ip
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
# http塊
# include代表引入一個(gè)外部的文件 ->/mime.types中放著大量的媒體類(lèi)型
# include /etc/nginx/conf.d/*.conf; ->引入了conf.d目錄下的以.conf為結(jié)尾的配置文件
三、Nginx的反向代理
3.1 正向代理和反向代理
正向代理:
1.正向代理服務(wù)時(shí)由客戶(hù)端設(shè)立的。
2客戶(hù)端了解代理服務(wù)器和目標(biāo)服務(wù)器都是誰(shuí)。
3.幫助咱們實(shí)現(xiàn)突破訪(fǎng)問(wèn)權(quán)限,提高訪(fǎng)問(wèn)的速度,對(duì)目標(biāo)服務(wù)器隱藏客戶(hù)端的ip地址
反向代理:
1.反向代理服務(wù)器是配置在服務(wù)端的。
2.客戶(hù)端是不知道訪(fǎng)問(wèn)的到底是哪一臺(tái)服務(wù)器。
3.達(dá)到負(fù)載均衡,并且可以隱藏服務(wù)器真正的ip地址。
3.2 基于nginx反向代理
準(zhǔn)備一個(gè)目標(biāo)服務(wù)器。
啟動(dòng)了之前的tomcat服務(wù)器。
編寫(xiě)nginx的配置文件,通過(guò)Nginx訪(fǎng)問(wèn)到tomcat服務(wù)器。
server{
listen 80;
server_name localhost;
#基于反向代理訪(fǎng)問(wèn)到Tomcat服務(wù)器
location / {
proxy_pass http://192.168.199.109:8080/;
}
}
3.3 關(guān)于Nginx的location路徑映射
優(yōu)先級(jí)關(guān)系:
(location = ) > (location/xxx/w/zzz) > (location ^~) > (location ,*) >(location /起始路徑)>(location /)
#1.=匹配
location = / {
#精準(zhǔn)匹配,主機(jī)名后面不能帶任何的字符串
}
# 2.通用配
location /xxx{
#匹配所有以/xxx開(kāi)頭的路徑
}
# 3.正則匹配
location ~/xxx {
#匹配所有以/xxx開(kāi)頭的路徑
}
#4.匹配開(kāi)頭路徑
location ^~ /images/{
#匹配所有以/images開(kāi)頭的路徑
}
#5. ~* \. (gif|jpg|png)${
#匹配以gif或者jpg或者png為結(jié)尾的路徑
}
案例配置
server{
listen 80;
server_name localhost;
location^~ / ssm/ {
proxy_pass http:i/192.168.199.109:8081/ssm/ ; #SSM項(xiàng)目首頁(yè)■
}
location = /index {
proxy_pass http://192.168.199.109:8081/;# Tomcat首頁(yè)
}
#基于反向代理訪(fǎng)問(wèn)到Tomcat服務(wù)器
location / {
proxy_pass http://192.168.199.109:8080/;# Hello Nginx! !
}
#location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
#}
}
四、Nginx負(fù)載均衡
Nginx為我們默認(rèn)提供了三種負(fù)載均衡的策略:
1.輪詢(xún):將客戶(hù)端發(fā)起的請(qǐng)求,平均的分配給每一臺(tái)服務(wù)器。
2.權(quán)重:會(huì)將客戶(hù)端的請(qǐng)求,根據(jù)服務(wù)器的權(quán)重值不同,分配不同的數(shù)量。
3.ip_hash :基于發(fā)起請(qǐng)求的客戶(hù)端的ip地址不同,他始終會(huì)將請(qǐng)求發(fā)送到指定的服務(wù)器上。
4.1輪詢(xún)
想實(shí)現(xiàn)Nginx輪詢(xún)負(fù)載均衡機(jī)制只需要在配置文件中添加以下內(nèi)容
upstream my-server{
server 192.168.199.109:8080;
server 192.168.199.109:8081;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://my-server/;
}
}
4.2 權(quán)重
實(shí)現(xiàn)權(quán)重方式:
upstream my-server{
server 192.168.199.109:8080 weight=10;
server 192.168.199.109:8081 weight=2;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://my-server/;
}
}
4.3 ip_hash
實(shí)現(xiàn)ip_hash方式:
upstream my-server{
ip_hash;
server 192.168.199.109:8080 weight=10;
server 192.168.199.109:8081 weight=2;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://my-server/;
}
}
五、Nginx動(dòng)靜分離
Nginx的并發(fā)能力公式:
worker_processes * worker_connections / 4|2= Nginx最終的并發(fā)能力
動(dòng)態(tài)資源除以4,靜態(tài)資源除以2
Nginx通過(guò)動(dòng)靜分離,來(lái)提升Nginx的并發(fā)能力,更快的給用戶(hù)響應(yīng)。
5.1動(dòng)態(tài)資源代理
#配置如下
location /{
proxy_pass 路徑;
}
5.2靜態(tài)資源代理
#配置如下
location / {
root 靜態(tài)資源路徑;
index 默認(rèn)訪(fǎng)問(wèn)路徑下的什么資源;
autoindex on;#代表展示靜態(tài)資源全的全部?jī)?nèi)容,以列表的形式展開(kāi)。
}
#先修改docker,添加一個(gè)數(shù)據(jù)卷,映射到Nginx服務(wù)器的一個(gè)目錄
#添加了index.html和11.jpg靜態(tài)資源
#修改配置文件
六、Nginx集群
6.1 引言
單點(diǎn)故障,避免nginx的宕機(jī),導(dǎo)致整個(gè)程序的崩潰
準(zhǔn)備多臺(tái)Nginx。
準(zhǔn)備keepalived,監(jiān)聽(tīng)nginx的健康情況。
準(zhǔn)備haproxy,提供一個(gè)虛擬的路徑,統(tǒng)一的去接收用戶(hù)得請(qǐng)求。
Dockerfile
FROM nginx:1.13.5-alpine
RUN apk update && apk upgrade
RUN apk add --no-cache bash curl ipvsadm iproute2 openrc keepalived
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]
entrypoint.sh
# !/bin/sh
#/usr/sbin/keepalived -n -l -D -f /etc/keepalived/keepalived.conf --dont-fork --log-console &
/usr/sbin/keepalived -D -f /etc/keepalived/keepalived.conf
nginx -g "daemon off;"
docker-compose.yml
version: "3.1"
services:
nginx_master:
build:
context: ./
dockerfile: ./Dockerfile
ports:
- 8981:80
volumes:
- ./index-master.html :/usr/share/nginx/html/index.html
- ./favicon.ico:/usr/share/nginx/html/favicon.ico
- ./keepalived-master.conf:/etc/keepalived/keepalived.conf
networks:
static-network :
ipv4_address: 172.20.128.2
cap_add:
- NET_ADMIN
nginx_slave:
build:
context: ./
dockerfile: ./Dockerfile
ports:
- 8082:80
volumes:
- ./index-slave.html :/usr/share/nginx/htm1/index.html
- ./favicon.ico:/usr/share/nginx/htm1/favicon.ico
- ./keepalived-slave.conf:/etc/keepalived/keepalived.conf
6.2 nginx搭建
查看資料中的內(nèi)容
柚子快報(bào)激活碼778899分享:運(yùn)維 nginx教程
文章鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀(guān)點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。