1、前言
最近空閒時間稍微少了點,晚上下班通常會看會書,因此更新也就沒有那麼快了!這不最近在看以前買的書籍(今年真的要多看點書籍):php
還有音視頻接口和解封裝框架,我差很少快捋順了,其實以前不少人問的接口,徹底能夠去ffmpeg官網查文檔說明,並且上面還有不少的demo測試案例說明:
html
差很少下週開始繼續更新音視頻的學習筆記;nginx
今天這篇文章呢,主要是總結一下過年期間複習的nginx負載均衡一些配置簡單的實戰演示!web
2、nginx的常見使用
這裏我主要演示nginx的源碼安裝以及相應的模塊安裝,而後講解一下負載均衡的原理並經過實戰來簡單演示,還有靜態資源的訪問(好比說圖片和視頻的訪問),關於什麼是nginx,它是幹什麼用的,網上有不少介紹,這裏我就不囉嗦了,我挑重點實戰來分享!正則表達式
一、nginx以及相關模塊源碼安裝:ubuntu
咱們先進行源碼所須要的模塊
vim
-- nginx-1.13.7.tar.gz數組
-- openssl-1.1.0g.tar.gz服務器
-- pcre-8.41.tar.gz微信
-- zlib-1.2.11.tar.gz
對應下載地址:
wget http://nginx.org/download/nginx-1.13.7.tar.gz
wget https://www.openssl.org/source/openssl-1.1.0g.tar.gz
wget http://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
wget http://www.zlib.net/zlib-1.2.11.tar.gz
(1)解壓nginx:
(2)解壓openssl:
(3)解壓pcre:
(4)解壓zlib:
(5)進行配置,先進入到nginx裏面去,而後執行下面語句:
./configure --prefix=/usr/local/nginx
--with-http_realip_module
--with-http_addition_module
--with-http_gzip_static_module
--with-http_secure_link_module
--with-http_stub_status_module
--with-stream
--with-pcre=/home/txp/share/nginx/pcre-8.41
--with-zlib=/home/txp/share/nginx/zlib-1.2.11
--with-openssl=/home/txp/share/nginx/openssl-1.1.0g
而後直接make:
而後接着再sudo make install:
最終咱們能夠看到在/usr/local/nginx/目錄下看到安裝的nginx:
如今咱們能夠試着來運行nginx,並進行訪問(下面的訪問成功):
這裏小結一下:
不少開源軟件的安裝步驟大概都差很少是下面這樣的套路(好比等下咱們下面要安裝的模塊,也是這樣安裝的思路,因此這裏就不造輪子了)
-- ./cofigure
-- make
--sudo make install
二、本身寫conf文件
在平時的開發過程當中,主要咱們要去配置它的conf文件夾下的nginx.conf文件
root@ubuntu:/usr/local/nginx# ls
client_body_temp conf fastcgi_temp
html logs proxy_temp sbin scgi_temp
uwsgi_temp
這個文件本來內容是:
#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 {
worker_connections 1024;
}
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;
}
#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;
#}
}
# 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;
# }
#}
}
這裏內容比較多,咱們如今本身來寫一個配置文件來實現nginx的訪問:
txp@ubuntu:/usr/local/nginx$ sudo mkdir demo_conf
txp@ubuntu:/usr/local/nginx$ cd demo_conf/
txp@ubuntu:/usr/local/nginx$ vim demo_conf/demo.conf
worker_processes 4;#進程數量
events{
worker_connections 1024;#併發訪問數量
}
http{
server {
listen 8888;#監聽端口
server_name localhost;#服務器名稱
client_max_body_size 100m;#訪問的數量大小
location / {
root /usr/local/nginx/html/ #訪問這個本地服務器裏面的這個html頁面
}
}
}
如今咱們來看一下咱們本身配置的是否成功,先把以前的nginx關閉掉:
./sbin/nginx -s stop
而後再執行這個配置文件:
./sbin/nginx -c demo_conf/demo.conf

這裏擴展一下基礎知識點:
Nginx 由配置文件中指定的指令控制的模塊組成。指令分爲簡單指令和塊指令。一個簡單的指令由空格分隔的名稱和參數組成,並以分號(;)結尾。塊指令具備與簡單指令相同的結構,但不是以分號結尾,而是以大括號({和})包圍的一組附加指令結束。若是塊指令能夠在大括號內部有其餘指令,則稱爲上下文(例如:events,http,server 和 location)。配置文件中放置在任何上下文以外的僞指令都被認爲是主上下文。events 和 http 指令駐留在主上下文中,server 在 http 中的,而 location 在 http 塊中。
三、負載均衡、反向代理和靜態資源的訪問演示:
--反向代理原理(ReverseProxy):它是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給internet上請求鏈接的客戶端,簡單來講就是真實的服務器不能直接被外部網絡訪問,想要訪問必須經過代理,以下圖所示:

上圖中有兩個網關,一個是nginx應用層網關,一個路由器硬件網關,nginx和各服務器都是在同一個局域網裏面;路由器它作了一個端口映射(nat)直接訪問到nginx,給人的感受nginx就在公網上面;
注意這裏的服務器對外不提供服務的,經過nginx代理來向外提供服務;外面訪問的是公網ip,而後經過端口映射找到nginx
如今咱們用nginx作代理配置(好比說我這裏用143的這臺機器代理141這臺機器):
worker_processes 4;
events{
worker_connections 1024;
}
http{
server {
listen 8888;
server_name localhost;
client_max_body_size 100m;
location / {
root /usr/local/nginx/html/;
proxy_pass http://192.168.29.141;
}
}
}
注意:141的那臺機器也安裝nginx了,而後當我訪問143這臺機器的時候,其實訪問的是141這臺機器的內容,這就是代理的使用了:

-- 負載均衡:從負載均衡四個字來看,確定是用來減輕服務器的訪問壓力的;好比說當一臺服務器的單位時間內的訪問量越大時,服務器壓力就越大,大到超過自身承受能力時,服務器就會崩潰(好比每一年雙十一活動,淘寶就使用了nginx的負載均衡的功能,否則當天那麼多的用戶活躍在淘寶上,服務器確定吃不消啊!)。所以爲了不服務器崩潰,讓用戶有更好的體驗,咱們經過負載均衡的方式來分擔服務器壓力。咱們能夠創建不少不少服務器,組成一個服務器集羣,當用戶訪問網站時,先訪問一箇中間服務器(也就是咱們的nginx),在讓這個中間服務器在服務器集羣中選擇一個壓力較小的服務器,而後將該訪問請求引入該服務器。如此以來,用戶的每次訪問,都會保證服務器集羣中的每一個服務器壓力趨於平衡,分擔了服務器壓力,避免了服務器崩潰的狀況。
下面我演示負載均衡案例:
worker_processes 4;
events{
worker_connections 1024;
}
http{
upstream backend{
server 192.168.29.142 weight=2;//weight表示權重
server 192.168.29.141 weight=1;
}
server {
listen 8888;
server_name localhost;
client_max_body_size 100m;
location / {
# root /usr/local/nginx/html/;
# proxy_pass http://192.168.29.141;
proxy_pass http://backend;
}
}
}
注意:權重表示被訪問的更多,這裏因爲我三臺機器都安裝了nginx,因此內容顯示看不出什麼不一樣之處來,其實142的機器被訪問了2次,141的機器被訪問了1次,我這裏有三臺機器:14一、14二、143:
-- 訪問靜態資源(圖片和視頻)
這裏我在143的機器上放了幾張圖片,而後在/usr/local/nginx目錄下建立了一個images文件夾,而後把143機器上的圖片copy到images下面來:
root@ubuntu:/usr/local/nginx# ls
client_body_temp fastcgi_temp images proxy_temp scgi_temp vip_conf
conf html logs sbin uwsgi_temp
root@ubuntu:/usr/local/nginx# mv /home/txp/share/nginx/*.png images/
root@ubuntu:/usr/local/nginx# ls
client_body_temp fastcgi_temp images proxy_temp scgi_temp vip_conf
conf html logs sbin uwsgi_temp
root@ubuntu:/usr/local/nginx# cd images/
root@ubuntu:/usr/local/nginx/images# ls
1.png 2.png 3.png
而後進行conf文件配置:
worker_processes 4;
events{
worker_connections 1024;
}
http{
upstream backend{
server 192.168.29.142 weight=2;
server 192.168.29.141 weight=1;
}
server {
listen 8888;
server_name localhost;
client_max_body_size 100m;
location / {
# root /usr/local/nginx/html/;
# proxy_pass http://192.168.29.141;
proxy_pass http://backend;
}
location /images/ {
root /usr/local/nginx/;
}
}
}
實現結果以下:
下面我在演示一下視頻訪問,一樣,我建立一個media目錄,而後把143機器上的test.mp4copy到media目錄來:
root@ubuntu:/usr/local/nginx# mv /home/txp/share/nginx/test.mp4 media/
root@ubuntu:/usr/local/nginx# cd media/
root@ubuntu:/usr/local/nginx/media# ls
test.mp4
conf文件配置:
worker_processes 4;
events{
worker_connections 1024;
}
http{
upstream backend{
server 192.168.29.142 weight=2;
server 192.168.29.141 weight=1;
}
server {
listen 8888;
server_name localhost;
client_max_body_size 100m;
location / {
# root /usr/local/nginx/html/;
# proxy_pass http://192.168.29.141;
proxy_pass http://backend;
}
location /images/ {
root /usr/local/nginx/;
}
location ~\.(mp3|mp4) #這裏利用了正則表達式
root /usr/local/nginx/media/;
}
}
}
結果以下:

3、總結
今天就暫時總結這麼多吧,還有cgi和fastcgi的使用和區別,就暫時不講了,若是哪天有用到,再來實戰演示;最後若是對技術熱愛的朋友,能夠加我微信,進技術交流羣交流學習,一塊兒進步;通常有問題我也會及時互相交流學習的:
tu18879499804
站在巨人的肩膀上:
https://blog.csdn.net/X1021333506/article/details/80975462?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-10.baidujs&dist_request_id=6c30ed21-fb2d-462c-a4d8-7c5581c1b504&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-10.baidujs
https://juejin.cn/post/6844903944267759624
https://ke.qq.com/webcourse/index.html#cid=420945&term_id=102189254&taid=8498121076534353&vid=5285890803239999870
本文分享自微信公衆號 - TXP嵌入式(txp1121518wo-)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。