手記系列之三 ----- 關于使用Nginx的一些使用方法和經驗

前言

本篇文章主要介紹的關于本人在使用Nginx的一些使用方法和經驗~
Nginx介紹介紹
Nginx("engine x")是一款是由俄羅斯的程序設計師Igor Sysoev所開發高性能的 Web和 反向代理 服務器,也是一個IMAP/POP3/SMTP 代理服務器 。在高連接并發的情況下,Nginx是Apache服務器不錯的替代品 。
正向代理和反向代理更詳細的理論知識可以看這篇文章: https://www.nginx.org.cn/article/detail/177
網上這塊的資料很多,個人理解核心,就是用戶去訪問互聯網的服務就是正向代理,互聯網服務訪問我們部署的服務就是反向代理 。
負載均衡介紹相關的使用教程可以看這篇文章:https://www.cnblogs.com/xuwujing/p/11953697.html
在介紹Nginx的負載均衡實現之前,先簡單的說下負載均衡的分類,主要分為硬件負載均衡和軟件負載均衡,硬件負載均衡是使用專門的軟件和硬件相結合的設備 , 設備商會提供完整成熟的解決方案,比如F5,在數據的穩定性以及安全性來說非常可靠 , 但是相比軟件而言造價會更加昂貴;軟件的負載均衡以Nginx這類軟件為主,實現的一種消息隊列分發機制 。
簡單來說所謂的負載均衡就是把很多請求進行分流,將他們分配到不同的服務器去處理 。比如我有3個服務器,分別為A、B、C , 然后使用Nginx進行負載均衡,使用輪詢策略 , 此時如果收到了9個請求,那么會均勻的將這9個請求分發給A、B、Cf服務器 , 每一個服務器處理3個請求,這樣的話我們可以利用多臺機器集群的特性減少單個服務器的壓力 。
Nginx實現負載均衡的示例圖:
手記系列之三 ----- 關于使用Nginx的一些使用方法和經驗

文章插圖
Nginx相關使用可以看這篇文章: https://www.cnblogs.com/xuwujing/p/11899890.html
使用Nginx+tomcat+redis做集群多個tomcat加上Nginx實現負載均衡,通過redis實現session共享 。可以使用github上面的第三方的jar包來實現 , 少量的配置即可 。下載地址: https://github.com/ran-jit/tomcat-cluster-redis-session-manager
手記系列之三 ----- 關于使用Nginx的一些使用方法和經驗

文章插圖
手記系列之三 ----- 關于使用Nginx的一些使用方法和經驗

文章插圖

手記系列之三 ----- 關于使用Nginx的一些使用方法和經驗

文章插圖
將下載lib包放到tomcat/lib 目錄下,配置文件修改的redis地址然后上傳到tomcat/conf目錄下即可 。
Nginx配置:
#usernobody;worker_processes10;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events {worker_connections1024;}error_log /var/log/nginx-error.log info;http {includemime.types;default_typeapplication/octet-stream;#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '#'$status $body_bytes_sent "$http_referer" '#'"$http_user_agent" "$http_x_forwarded_for"';#access_loglogs/access.logmain;sendfileon;#tcp_nopushon;#keepalive_timeout0;keepalive_timeout65;#gzipon;upstream pancm{server 192.169.0.24:8085;server 192.169.0.24:8084;}server {listen8083;server_name192.169.0.24;#charset koi8-r;#access_loglogs/host.access.logmain;location / {roothtml;proxy_pass http://pancm;proxy_set_header Host$host:8083;proxy_set_header X-Forwarded-Host $host:8083;proxy_set_header X-Forwarded-Server $host:8083;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 3s;proxy_read_timeout 5s;proxy_send_timeout 3s;indexindex.html index.htm;}#============對不同請求的處理=============location ~ \.(jsp|html|jspx|do|action)?${#=============tomcat的資源位置============roothtml;index index.jsp index.jspx index.do;#==========Nginx提供的代理============proxy_set_header Host$host:8083;proxy_set_header X-Forwarded-Host $host:8083;proxy_set_header X-Forwarded-Server $host:8083;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#=== 如果遇到.jsp .jspx .do .action 的請求就進入該服務器(tomcat)===proxy_pass http://pancm;}#error_page404/404.html;# redirect server error pages to the static page /50x.html#error_page500 502 503 504/50x.html;location = /50x.html {roothtml;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#proxy_passhttp://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#roothtml;#fastcgi_pass127.0.0.1:9000;#fastcgi_indexindex.php;#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;#includefastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#denyall;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#listen8000;#listensomename:8080;#server_namesomenamealiasanother.alias;#location / {#roothtml;#indexindex.html index.htm;#}#}# HTTPS server##server {#listen443 ssl;#server_namelocalhost;#ssl_certificatecert.pem;#ssl_certificate_keycert.key;#ssl_session_cacheshared:SSL:1m;#ssl_session_timeout5m;#ssl_ciphersHIGH:!aNULL:!MD5;#ssl_prefer_server_cipherson;#location / {#roothtml;#indexindex.html index.htm;#}#}}

推薦閱讀