天天看點

nginx牛逼tomcat弱雞探索之道

nginx牛逼解密
  • 序言
  • 安裝
  • 反向代理
  • 靜态資源伺服器
  • nginx vs tomcat壓測對比
    • 并發結果對比
    • 對伺服器影響對比
  • 說在後面的話

序言

nginx牛逼tomcat弱雞探索之道

對于常用的伺服器,大家可能更多的知道apache,tomcat,lls等伺服器。我們跟多的了解到nginx常常用于反向代理。而實質是nginx也是一個高性能伺服器。常用于前端頁面資源靜态化和負載均衡的反向代理。

下面就由部落客帶你認識nginx。以及nginx的反向代理、資源靜态化,和壓測對比。

安裝

筆者環境是ubuntu18.04.是以下載下傳東西是非常簡單。centos 的yum源也有nginx。對于window。直接下載下傳使用即可。

  • nginx 下載下傳:

    sudo apt get nginx

  • jmeter 下載下傳 官網自行安裝 解壓完(

    sudo chomd -R 777 apache-jmeter-5.1.1

    )

這樣,一些基本的工具就下載下傳完成啦。直接nginx或者sudo nginx通路

localhost

就有hello nginx 就是安裝成功,對于jmeter 如果安裝可以到官網進行安裝。

對于nginx安裝後的一些路徑,都在

  • 所有的配置檔案都在/etc/nginx下
  • 程式檔案在/usr/sbin/nginx
  • 日志放在了/var/log/nginx中
  • 并已經在/etc/init.d/下建立了啟動腳本nginx

還有nginx的一些常用關閉指令

  • nginx -s reload :修改配置後重新加載生效
  • nginx -s reopen :重新打開日志檔案
  • 關閉nginx:

    nginx -s stop :快速停止nginx

    quit :完整有序的停止nginx

  • 其他的停止nginx 方式:

    ps -ef | grep nginx

    kill -9 xxx

如果權限不足記得加上

sudo

反向代理

nginx牛逼tomcat弱雞探索之道

對于nginx,很多人用nginx+tomcat做負載均衡。提高系統并發量。

對于nginx的配置檔案,在

/etc/nginx/conf.d/

下建立

proxy.conf

(也可以在nginx/nginx.conf中配置)

就比如我想用我本地8081端口代理到本地的nginx上

upstream tomcat-server{

   server 47.100.58.250:8080 ;
  }
  server{
         listen 8081; 
         server_name localhost; 
         location / {
           proxy_pass http://tomcat-server;
           root html;
           index index.html;
         }
} 
           

複制

對于nginx/nginx.conf我進行了修改去掉一些暫時用不到的。

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;

    client_max_body_size 20m;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}
           

複制

這樣,你在本地就可以代理通路伺服器位址http://localhost:8081/

當然,如果有多個server可以在upstream中進行配置,還可配置各個節點的權重等等。具體就不介紹了。

nginx牛逼tomcat弱雞探索之道

靜态資源伺服器

在前後端分離流行的當今。nginx被許多前端愛好者接受。通過nginx能夠簡單的将靜态資源部署,進而達到動靜分離的效果。

nginx牛逼tomcat弱雞探索之道

nginx配置靜态資源伺服器很簡單

在上述同級檔案conf.d/下建立static.conf

server {
    listen    8082;
    server_name localhost;

    location / {
      root  /home/tomcat9/webapps/ROOT/;
      autoindex on;
    }
  }
           

複制

其中root為tomcat的絕對路徑(可以随便配置),我事先對tomcat的首頁的index.jsp跑起來存成index.html檔案。

那麼現在我們已經完成

兩件事

8081端口負載均衡(反向代理到我伺服器)

8082端口制成本地tomcat的靜态檔案伺服器

那麼開啟tomcat。左面是nginx做伺服器傳回靜态

nginx牛逼tomcat弱雞探索之道

nginx vs tomcat壓測對比

并發結果對比

  1. 我們先前下載下傳的jmeter可以上場了。針對同一個靜态頁面。nginx路徑為http://localhost:8082/,tomcat為http://localhost:8080/.
  2. 添加線程組。先測試15000個線程1s内同時請求首頁。線程組中添加http請求。ip:localhost。port:8080

    在添加->監聽器添加自己喜歡資料格式(需要qps的可以找插件)

  3. 對比

    其中tomcat,最大qps2400左右

nginx牛逼tomcat弱雞探索之道

而nginx的結果明顯要強,最大qps3000左右

nginx牛逼tomcat弱雞探索之道

對伺服器影響對比

  • 當20000個并發10s内請求時候。附上完成時的cpu狀況 tomcat cpu達到21.6%。記憶體站到6.4%,也就是随着并發tomcat對伺服器的cpu和記憶體造成不小壓力
nginx牛逼tomcat弱雞探索之道

而nginx在壓測過程中記憶體幾乎沒變化,幾乎不吃記憶體。而cpu占用也比tomcat少。

nginx牛逼tomcat弱雞探索之道

測壓總結:

名額 nginx tomcat
響應中位數 0ms 1ms
前95%響應時間 4 5
前99%響應時間 7 16
qps(觀察) 3000 2400
對記憶體影響 0% 6.4%
對cpu影響 14% 21.6%
其他 輕便強大 java吃點記憶體、速度慢點

(測量環境相對統一)

說在後面的話

通過系列對比,發現nginx的強大之處。負載均衡和靜态資源伺服器能夠為并發減輕壓力。而nginx的學問依然很多。還需要細細研究才行。并且nginx的牛B之處遠不止這些,還需要深入學習才行!