Tomcat 默認配置的最大請求數是 150,併發量小,用戶使用的少,在低併發的狀況下,一個jar包啓動應用就夠了,而後內部tomcat返回內容給用戶。
用戶愈來愈多了,併發量慢慢增大了,這時候一臺服務器知足不了咱們的需求了。
因而咱們橫向擴展,又增長了服務器。這個時候幾個項目啓動在不一樣的服務器上,用戶要訪問,就須要增長一個代理服務器了,經過代理服務器來幫咱們轉發和處理請求。
咱們但願這個代理服務器能夠幫助咱們接收用戶的請求,而後將用戶的請求按照規則幫咱們轉發到不一樣的服務器節點之上。這個過程用戶是無感知的,用戶並不知道是哪一個服務器返回的結果,咱們還但願他能夠按照服務器的性能提供不一樣的權重選擇。保證最佳體驗!因此咱們使用了Nginx。
php
Nginx (engine x) 是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務。Nginx是由伊戈爾·賽索耶夫爲俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0發佈於2004年10月4日。2011年6月1日,nginx 1.0.4發佈。
其特色是佔有內存少,併發能力強,事實上nginx的併發能力在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、京東、新浪、網易、騰訊、淘寶等。在全球活躍的網站中有12.18%的使用比率,大約爲2220萬個網站。
Nginx 是一個安裝很是的簡單、配置文件很是簡潔(還可以支持perl語法)、Bug很是少的服務。Nginx 啓動特別容易,而且幾乎能夠作到7*24不間斷運行,即便運行數個月也不須要從新啓動。你還可以不間斷服務的狀況下進行軟件版本的升級。
Nginx代碼徹底用C語言從頭寫成。官方數據測試代表可以支持高達 50,000 個併發鏈接數的響應。
css
經過使用Nginx,大大提升網站的響應速度,優化用戶體驗,讓網站的健壯性更上一層樓!
html
Http代理,反向代理:做爲web服務器最經常使用的功能之一,尤爲是反向代理。
linux
Nginx提供的負載均衡策略有2種:內置策略和擴展策略。內置策略爲輪詢,加權輪詢,Ip hash。擴展策略,就天馬行空,只有你想不到的沒有他作不到的。
nginx
iphash對客戶端請求的ip進行hash操做,而後根據hash結果將同一個客戶端ip的請求分發給同一臺服務器進行處理,能夠解決session不共享的問題。
c++
動靜分離,在咱們的軟件開發中,有些請求是須要後臺處理的,有些請求是不須要通過後臺處理的(如:css、html、jpg、js等等文件),這些不須要通過後臺處理的文件稱爲靜態文件。讓動態網站裏的動態網頁根據必定規則把不變的資源和常常變的資源區分開來,動靜資源作好了拆分之後,咱們就能夠根據靜態資源的特色將其作緩存操做。提升資源響應的速度。
web
http://nginx.org/en/download.html 下載穩定版本。
以nginx/Windows-1.18.0爲例,直接下載 nginx-1.18.0.zip。
下載後解壓,解壓後以下:
正則表達式
有不少種方法啓動nginx算法
nginx.exe
,回車即直接在瀏覽器地址欄輸入網址 http://localhost:80 回車,出現如下頁面說明啓動成功!
shell
nginx的配置文件是conf目錄下的nginx.conf,默認配置的nginx監聽的端口爲80,若是80端口被佔用能夠修改成未被佔用的端口便可。
當咱們修改了nginx的配置文件nginx.conf 時,不須要關閉nginx後從新啓動nginx,只須要執行命令 nginx -s reload
便可讓改動生效
server { listen 80; server_name localhost; }
若是使用cmd命令窗口啓動nginx, 關閉cmd窗口是不能結束nginx進程的,可以使用兩種方法關閉nginx
nginx -s stop
(快速中止nginx) 或 nginx -s quit
(完整有序的中止nginx)taskkill /f /t /im nginx.exe
taskkill是用來終止進程的 /f是強制終止 /t終止指定的進程和任何由此啓動的子進程。 /im示指定的進程名稱
安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:
yum install gcc-c++
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也須要此庫。命令:
yum install -y pcre pcre-devel
zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。
yum install -y zlib zlib-devel
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel
手動下載.tar.gz安裝包,地址:https://nginx.org/en/download.html
下載完畢上傳到服務器上
tar -zxvf nginx-1.18.0.tar.gz cd nginx-1.18.0
./configure make make install
cd /usr/local/nginx/sbin/ ./nginx 啓動
啓動成功,訪問 服務器ip:80,直接在瀏覽器地址欄輸入網址 http://192.168.10.216:80 回車,出現如下頁面說明啓動成功!
注意:如何鏈接不上,檢查服務器是否開放端口,或者服務器防火牆是否開放端口!
# 開啓防火牆 service firewalld start # 重啓 service firewalld restart # 關閉 service firewalld stop # 查看防火牆規則 firewall-cmd --list-all # 查詢端口是否開放 firewall-cmd --query-port=8080/tcp # 開放80端口 firewall-cmd --permanent --add-port=80/tcp # 移除端口 firewall-cmd --permanent --remove-port=8080/tcp #重啓防火牆(修改配置後要重啓防火牆) firewall-cmd --reload # 參數解釋 一、firwall-cmd:是Linux提供的操做firewall的一個工具; 二、--permanent:表示設置爲持久; 三、--add-port:標識添加的端口;
cd /usr/local/nginx/sbin/ ./nginx 啓動 ./nginx -s stop 中止 ./nginx -s quit 安全退出 ./nginx -s reload 從新加載配置文件 ps aux|grep nginx 查看nginx進程
location / { root html; index index.html index.htm; # 反向代理配置 proxy_pass http://qing; }
# 負載均衡配置,名字qing可任意定義,在server->location->proxy_pass反向代理配置中使用 # weight是權重配置 upstream qing { server 192.168.10.216:8080 weight=1; server 192.168.10.217:8080 weight=2; }
# 全局配置 #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配置 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; # 負載均衡配置,名字qing可任意定義,在server->location->proxy_pass反向代理配置中使用 # weight是權重配置 upstream qing { server 192.168.10.216:8080 weight=1; server 192.168.10.217:8080 weight=2; } # http服務配置 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; # 反向代理配置 proxy_pass http://qing; } #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 # https服務配置 #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; # } #} }