對於經常使用的服務器,你們可能更多的知道apache,tomcat(java應用服務器),lls等服務器。咱們跟多的瞭解到nginx經常用於反向代理。而實質是nginx也是一個高性能web服務器。經常使用於前端頁面資源靜態化和負載均衡的反向代理。html
下面就由博主帶你認識nginx。以及nginx的反向代理、資源靜態化,和壓測對比。前端
筆者環境是ubuntu18.04.因此下載東西是很是簡單。centos 的yum源也有nginx。對於window。直接下載使用便可。java
sudo apt get nginx
sudo chomd -R 777 apache-jmeter-5.1.1
)這樣,一些基本的工具就下載完成啦。直接nginx或者sudo nginx訪問localhost
就有hello nginx 就是安裝成功,對於jmeter 若是安裝能夠到官網進行安裝。node
對於nginx安裝後的一些路徑,都在
python
還有nginx的一些經常使用關閉命令
nginx
若是權限不足記得加上sudo
web
/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我進行了修改去掉一些暫時用不到的。apache
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中進行配置,還可配置各個節點的權重等等。具體就不介紹了。 ubuntu
在先後端分離流行的當今。nginx被許多前端愛好者接受。經過nginx可以簡單的將靜態資源部署,從而達到動靜分離的效果。 後端
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 |
---|---|---|
響應中位數 | 0ms | 1ms |
前95%響應時間 | 4 | 5 |
前99%響應時間 | 7 | 16 |
qps(觀察) | 3000 | 2400 |
對內存影響 | 0% | 6.4% |
對cpu影響 | 21.6% | 14% |
其餘 | 輕便強大 | java吃點內存、速度慢點 |
(測量環境相對統一)
經過系列對比,發現nginx的強大之處。負載均衡和靜態資源服務器可以爲併發減輕壓力。而nginx的學問依然不少。還須要細細研究才行。而且nginx的牛B之處遠不止這些,還須要深刻學習才行!
同時,替本身公衆號打啵廣告