手记系列之三 ----- 关于使用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;#}#}}

推荐阅读