一、基本概念
1.什么是Nginx?
Nginx是一個高性能的web服務器和反向代理服務器,特點是占用記憶體少,并發能力強;事實上Nginx的并發能力確實在同型別的網頁服務器中表現良好;
Nginx專為性能優化而開發,性能是其主要的考量;實作上非常注重效率,經得住高并發的考驗,有報告表名能支持高達50000個并發連接數
2.反向代理
(1)正向代理
在客戶端(瀏覽器)中需要配置代理服務器,通過代理服務器進行互聯網訪問
(2)反向代理
客戶端只需要將請求發送到反向代理服務器,由反向代理服務器去選擇目標服務器獲取資料,再回傳給客戶端,此時反向代理服務器和目標服務器對外就是一個服務器,暴露的是代理服務器地址,隱藏了真實服務器地址
3.負載均衡
單個服務器解決不了,我們增加服務器數量,然后將請求分發到各個服務器上,將原先請求集中到單個服務器的情況改為將請求分發到多個服務器上,將負載分發到不同的服務器上,這就是負載均衡
4.動靜分離
為了加快網站的訪問速度,可以吧動態頁面和靜態頁面由不同服務器來決議,加快決議速度,降低原來單個服務器的壓力,
二、Nginx的安裝
1.安裝Nginx
①安裝pcre 依賴
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
解壓檔案
執行 ./configure 完成后,回到pcre目錄下執行 make,
在執行 make install
② 安裝openssl
③ 安裝zlib
yum -y install make zlib zlib -devel gcc-c++ libtool openssl openssl-devel
④安裝Nginx
(1)把Nginx安裝檔案放入linux系統中去
(2) 解壓壓縮檔案
(3) make && make install
安裝成功以后,在usr多出一個檔案夾 local/nginx,在nginx有sbin有啟動腳本
(4)啟動nginx(進入local/nginx/sbin 目錄, 執行 ./nginx
(5)防火墻設定
查看開放的防火墻: firewall -cmd --list-all
設定開放的埠:firewall -cmd --add-service=http -permanent
sudu firewall -cmd --add-port=8002/tcp --permanent
重啟防火墻:firewall -cmd --reload
三、nginx的常用命令
1.使用nginx操作命令前提條件:進入到nginx的目錄下(usr/local/nginx/sbin)
| 命令說明 | 命令 |
|---|---|
| 查看nginx;版本號 | ./nginx -v |
| 啟動 nginx | ./nginx |
| 關閉nginx | /nginx -s stop |
| 重新加載 nginx | ./nginx -s reload |
四、nginx組態檔
nginx組態檔有三部分組成
(1) 全域塊
從組態檔開始到events塊之間的內容,主要會設定一些影響nginx服務器整體運行的配置命令,主要包括配置運行Nginx服務器的用戶組,允許生成的 work_process數,行程PID存放路徑,日志存放路徑和型別一級組態檔的 引入等,
(2) events塊
events塊涉及的指令主要影響Nginx服務器與用戶的網路連接
比如 worker_connections 1024; 支持的最大連接數
1 events { 2 worker_connections 1024; 3 }
(2) http塊
1 http { 2 include mime.types; 3 default_type application/octet-stream; 4 ? 5 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 6 # '$status $body_bytes_sent "$http_referer" ' 7 # '"$http_user_agent" "$http_x_forwarded_for"'; 8 ? 9 #access_log logs/access.log main; 10 ? 11 sendfile on; 12 #tcp_nopush on; 13 proxy_connect_timeout 5; 14 proxy_read_timeout 60; 15 proxy_send_timeout 60; 16 proxy_buffer_size 32k; 17 proxy_buffers 4 128k; 18 proxy_busy_buffers_size 128k; 19 proxy_temp_file_write_size 128k; 20 client_max_body_size 128m; 21 ignore_invalid_headers on; 22 ? 23 ? 24 #keepalive_timeout 0; 25 ? 26 27 ? 28 ? 29 keepalive_timeout 65; 30 ? 31 #gzip on; 32 ? 33 upstream XXXXX { 34 server IP地址:埠號; 35 ? 36 } 37 ? 38 ? 39 server { 40 listen 90; 41 server_name localhost; 42 ? 43 #charset koi8-r; 44 ? 45 #access_log logs/host.access.log main; 46 ? 47 ? 48 location / { 49 # these two lines here 50 proxy_http_version 1.1; 51 proxy_set_header Connection ""; 52 proxy_pass https://域名/; 53 } 54 ? 55 location /ZZZZZ/ { 56 proxy_pass http://XXXXX; 57 } 58 ? 59 #error_page 404 /404.html; 60 ? 61 # redirect server error pages to the static page /50x.html 62 # 63 error_page 500 502 503 504 /50x.html; 64 location = /50x.html { 65 root html; 66 } 67 ? 68 ? 69 } 70 ? 71 server { 72 listen 88; 73 server_name localhost; 74 ? 75 #charset koi8-r; 76 ? 77 #access_log logs/host.access.log main; 78 ? 79 ? 80 location / { 81 # these two lines here 82 proxy_http_version 1.1; 83 proxy_set_header Connection ""; 84 proxy_pass https://域名/; 85 } 86 ? 87 location /ZZZZZ/ { 88 proxy_pass http://XXXXX; 89 } 90 ? 91 #error_page 404 /404.html; 92 ? 93 # redirect server error pages to the static page /50x.html 94 # 95 error_page 500 502 503 504 /50x.html; 96 location = /50x.html { 97 root html; 98 } 99 ? 100 ? 101 } 102 ? 103 server { 104 listen 95; 105 server_name localhost; 106 ? 107 #charset koi8-r; 108 ? 109 #access_log logs/host.access.log main; 110 ? 111 ? 112 location / { 113 # these two lines here 114 proxy_http_version 1.1; 115 proxy_set_header Connection ""; 116 proxy_pass https://域名/; 117 } 118 #error_page 404 /404.html; 119 ? 120 # redirect server error pages to the static page /50x.html 121 # 122 error_page 500 502 503 504 /50x.html; 123 location = /50x.html { 124 root html; 125 } 126 ? 127 ? 128 } 129 ? 130 131 ? 132 }
Nginx最頻繁配置的地方,代理、快取和日志定義等 絕大多數功能和第三方模塊的配置都在這里
①http全域塊
http全域塊配置的指令包括檔案引入、MIME-TYPE 定義、日志自定義、連接超時時間、單鏈請求數上限等,
② server塊
這塊和虛擬主機 相關,虛擬主機從用戶側看,和一臺獨立額硬體主機是完全一樣的,該技術的產生是為了節省互聯網服務器硬體成本,
每個http塊可以包括多個server塊,而每個server塊相當于一個主機
每個server塊也分為全域server塊,以及可以同時包括多個location塊
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/3840.html
標籤:其他
上一篇:【程式員面試系列】手把手教你如何面試:面試程序(一)
下一篇:《劍指Offer》各面試題總結
