Nginx+Tomcat集群配置[一]
2018-01-02 08:55
363 查看
Nginx+Tomcat集群配置[一]
前言
本人亲测有效,希望各位作为参考,希望可以帮助你们,大佬请绕过!
什么是Nginx- nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;同时也是一个IMAP、POP3、SMTP代理服务器;nginx可以作为一个HTTP服务器进行网站的发布处理,另外nginx可以作为反向代理进行负载均衡的实现。
-
1.安装
安装很简单,直接点击这里,下载nginx-1.8.1,(中间的那个是windows版本的),下载后解压就可以了,启动直接点击解压后文件中nginx.exe,访问nginx,在浏览器中输入http://localhost,访问页面,出现下列页面标示访问成功!
由于我的nginx是在windows下安装的,关闭比较麻烦,可以写一个脚本来管理,桌面新建nginx.bat文件,然后用编辑器打开,添加一下内容
==================================================== @echo off rem 当前bat的作用 echo ==================begin======================== cls SET NGINX_PATH=D: SET NGINX_DIR=D:\nginx-1.13.8\ color 0a TITLE Nginx Perssion CLS :MENU ECHO. * nginx list * tasklist|findstr /i "nginx.exe" ECHO. ECHO. [1] Start Nginx ECHO. [2] Close Nginx ECHO. [3] Restart Nginx ECHO. [4] quit ECHO. ECHO. please input you num: set /p ID= IF "%id%"=="1" GOTO start IF "%id%"=="2" GOTO stop IF "%id%"=="3" GOTO restart IF "%id%"=="4" EXIT PAUSE :start call :startNginx GOTO MENU :stop call :shutdownNginx GOTO MENU :restart call :shutdownNginx call :startNginx GOTO MENU :shutdownNginx ECHO. ECHO.close Nginx...... taskkill /F /IM nginx.exe > nul ECHO.OK,close all nginx proess goto :eof :startNginx ECHO. ECHO.start Nginx...... IF NOT EXIST "%NGINX_DIR%nginx.exe" ECHO "%NGINX_DIR%nginx.exe" not exit %NGINX_PATH% cd "%NGINX_DIR%" IF EXIST "%NGINX_DIR%nginx.exe" ( echo "start '' nginx.exe" start "" nginx.exe ) ECHO.OK goto :eof
2.nginx的配置文件:nginx.conf
- 配置文件的组成结构:main(全局配置),events(工作模式配置),http(http设置),server(服务器主机设置),结构大致类似下面这样
main# 全局配置 events {# nginx工作模式配置 } http {# http设置 .... server {# 服务器主机配置 .... location {# 路由配置 .... } location path { .... } } server { .... location { .... } } upstream name {# 负载均衡配置 .... } }
3.功能
- 3.1静态HTTP代理
- 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML、图片)通过HTTP协议展现给客户端。配置:
#使用的用户和组 #user nobody; worker_processes 1;#工作进程的个数,一半与计算机的cpu核数一致 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #指定错误日志存放的路径,错误日志记录级别可选项为:[debug|info|notice|warn|error|crit],默认是crit,记录的日志数量从crit到debug,由少到多。 #指定pid存放的路径 #pid logs/nginx.pid; events { worker_connections 1024;#单个进程的最大连接数(最大连接数=连接数*进程数) } ############################################################################## #遵循http协议的服务器全局设置 http { include mime.types;#文件扩展名与文件类型映射表 default_type application/octet-stream;#默认文件类型 sendfile on; keepalive_timeout 65;#长连接超时时间,单位是秒 #gzip on;#启用Gizp压缩 #upstream设置,设置代理服务器(负载均衡池),默认的负载均衡方式是轮询,另外一种是ip_hash upstream netitcast.com { #服务器集群名字 #ip_hash server 47.100.52.177:8888 weight=2;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。 server 127.0.0.1:8080 weight=1; } #当前的Nginx的配置 server { listen 80;#监听80端口,可以改成其他端口 server_name localhost;########当前主机的域名 location / { # 配置你的访问路径,可以是相对路径也可以是绝对路径 #root hyh; # 配置首页,以下顺序匹配 #index index.html,index.htm; #映射地址,如果请求为localhost:80则交给netitcast.com的Nginx集群来处理 proxy_pass http://netitcast.com; #与前面的服务器集群名字相同 #映射地址跳转方式 proxy_redirect default; } # 用于设置如果出现指定的HTTP错误状态码,则返回指定的url页面 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; } } }
3.2 反向代理服务器
-什么是反向代理?
客户端本来可以直接通过HTTP协议访问某网站应用服务器,网站管理员可以在中间加上一个Nginx,客户端请求Nginx,Nginx请求应用服务器,然后将结果返回给客户端,此时Nginx就是反向代理服务器。
配置如下:
#核心配置和上面一样,只是改变了location中的内容 server { listen 80;#监听80端口,可以改成其他端口 server_name localhost;########当前主机的域名 location / { proxy_pass http://127.0.0.1:8080;#我本地的tomcat } # 用于设置如果出现指定的HTTP错误状态码,则返回指定的url页面 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; } } }
-3.3负载均衡
当网站访问量非常大,网站站长开心赚钱的同时,也摊上事儿了。因为网站越来越慢,一台服务器已经不够用了。于是将同一个应用部署在多台服务器上,将大量用户的请求分配给多台机器处理。同时带来的好处是,其中一台服务器万一挂了,只要还有其他服务器正常运行,就不会影响用户使用。Nginx可以通过反向代理来实现负载均衡。
配置:
#使用的用户和组 #user nobody; worker_processes 1;#工作进程的个数,一半与计算机的cpu核数一致 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #指定错误日志存放的路径,错误日志记录级别可选项为:[debug|info|notice|warn|error|crit],默认是crit,记录的日志数量从crit到debug,由少到多。 #指定pid存放的路径 #pid logs/nginx.pid; events { worker_connections 1024;#单个进程的最大连接数(最大连接数=连接数*进程数) } ############################################################################## #遵循http协议的服务器全局设置 http { include mime.types;#文件扩展名与文件类型映射表 default_type application/octet-stream;#默认文件类型 sendfile on; keepalive_timeout 65;#长连接超时时间,单位是秒 #gzip on;#启用Gizp压缩 #upstream设置,设置代理服务器(负载均衡池),默认的负载均衡方式是轮询,另外一种是ip_hash upstream netitcast.com { #服务器集群名字 #ip_hash server 47.100.52.177:8888 weight=2;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。 server 127.0.0.1:8080 weight=1; } #当前的Nginx的配置 server { listen 80;#监听80端口,可以改成其他端口 server_name localhost;########当前主机的域名 location / { #映射地址,如果请求为localhost:80则交给netitcast.com的Nginx集群来处理 proxy_pass http://netitcast.com; #与前面的服务器集群名字相同 #映射地址跳转方式 proxy_redirect default; } # 用于设置如果出现指定的HTTP错误状态码,则返回指定的url页面 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; } } }
最后使用地址栏问localhost:80,出现远程端的Tomcat和本地端的Tomcat首页!
相关文章推荐
- nginx+tomcat集群配置(3)---获取真实客户端IP
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[二]
- windows下nginx+tomcat集群配置
- Nginx+Tomcat集群与负载均衡示例,配置webservice,配置Tomcat,配置Nginx
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[一]
- ubuntu16.04+nginx+tomcat集群配置
- Nginx+Tomcat集群配置[二]
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[二]
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[二]
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[一]
- Nginx+Tomcat集群配置[二]
- Nginx+Tomcat集群配置[一]