windows下Nginx+Tomcat實現叢集以及Nginx實現動靜分離
阿新 • • 發佈:2018-12-16
最近學習了點Nginx的基礎配置,是真的強大。
簡單來說Nginx的作用:
- 負載均衡,實現專案的叢集分佈。意思就是同一個專案可以分別放在不同的伺服器裡,通過Nginx配置管理這多個伺服器,這樣子可以分擔伺服器壓力,將流量按負載均衡演算法分配給這些不同的伺服器,但是訪問的都是同一個專案。
- 動靜分離,實現專案的靜態資源和動態資源分離訪問。Nginx訪問靜態資源的速度比Tomcat快,所以一般都是靜態資源如html,css等放在Nginx伺服器,而動態資源,如:jsp,json放在tomcat中訪問。
最後實現的效果圖:
解釋一下:
動靜分離:
靜態資源和動態資源的分離訪問:靜態資源通過Nginx直接返回,動態資源通過Nginx從Tomcat中獲取。
Tomcat叢集:
首先:windows本地得建立多個Tomcat伺服器然後啟動,本次實驗中我就建立兩個。 然後:修改 Tomcat 的埠設定。分別進入兩個 Tomcat 伺服器的 conf 目錄,開啟 server.xml 配置檔案並做修改。 其次:將 Tomcat1 的三個埠分別修改為 8081、8082、8083,瀏覽器訪問 Tomcat1 使用的埠號為 8082,之後修改 Tomcat2 的三個埠號,分別為 8084、8085、8086,瀏覽器訪問 Tomcat2 使用的埠號為 8085。 伺服器配置完成,然後再來配置Nginx伺服器,進入D:\nginx-1.12.2\conf開啟nginx.conf修改配置檔案:
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; upstream HA-tomcat{ server localhost:8082; server localhost:8085; } server { listen 8888; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location ^~ /static/ { ## 匹配任何以 /static/ 開始的請求,並停止匹配 其它location root D:\\nginx-1.12.2; } #以~或~*開頭表示正則匹配,~*表示正則不區分大小寫 #~ 波浪線表示執行一個正則匹配,區分大小寫 #~* 表示執行一個正則匹配,不區分大小寫 #^~ ^~表示普通字元匹配,如果該選項匹配,只匹配該選項,不匹配別的選項,一般用來匹配目錄 location ~* \.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { # 匹配以 gif|jpg|jpeg|bmp|png|ico|txt|js|css結尾的請求. # 但是所有 /static/ 目錄的請求將由 /static/處理. root D:\\nginx-1.12.2; } location / { # 匹配任何請求,因為所有請求都是以"/"開始 # 但是更長字元匹配或者正則表示式匹配會優先匹配 root html; index index.html index.htm; proxy_pass http://HA-tomcat; } #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; # } #} }
然後開啟兩個tomcat伺服器和Nginx即可訪問叢集tomcat伺服器 動態資源訪問:
http://localhost:8888
靜態資源訪問:
http://localhost:8888/12.jpg
http://localhost:8888/static/1.jpg
我這裡將預設埠80修改為8888 因為開啟Nginx的時候它報錯說我80埠被pid為4的程式佔用,這個4是system管理記憶體的一個東西,無法強制刪除,我只能修改nginx監聽埠為8888.
最後看一下nginx的三個常用命令: 開啟:
D:\nginx-1.12.2>start nginx
重啟:
D:\nginx-1.12.2>nginx -s reload
關閉:
D:\nginx-1.12.2>nginx -s stop