nginx-tomcat集群配置反向代理(web项目)
2017-09-05 17:31
525 查看
手头某个项目计划要用nginx-tomcat做集群反向代理,于是就做了技术预研。
网上贴的资料感觉好少,不知是我没找对方向还是怎么着,都找不到想要的,折腾了好几天,总算弄好了,不过也不知道到底对不对,有没有更好的办法。
先贴上来,做个记录,大家要是会的也给指点指点。
背景:项目使用springmvc4-hibernate4
首先要先去nginx官方网站去下nginx压缩包。是tar.gz格式的,windows我是直接解压后安装用,linux的话在我的前两篇blog中有稍微记录。
文章地址:http://blog.csdn.net/muluo7fen/article/details/77773846
下载文件:http://download.csdn.net/download/muluo7fen/9960637
安装过程就略过了,自己百度一下,还是比较多的。
4000
这里提一下有个比较好用的办法,见下图,我根据网上的办法弄了.bat文件真的很方便,双击运行,不用再用命令了。
我是使用Notepad++软件编辑保存的。三个文件的内容分别如下,按图序:
nginx -s reload
start nginx
nginx -s stop
我是使用myeclipse开发项目的,所以就直接在myeclipse里面用tomcat开关了,这样比较方便,初步一个先这么着。
所以应用服务器就不用管了,用现成的,项目也编译好在tomcat上了,剩下就是nginx配置了。
贴上我的配置文件:在解压的nginx文件内的conf文件中:nginx.conf可以直接用文本编辑
#运行用户
#user nobody;
worker_processes 1; #工作进程的个数,一般与计算机的cpu核数一致
#全局错误日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工作模式及连接数上限
events {
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,
#仅用于linux2.6以上内核,可以大大提高nginx的性能
#use epoll;
worker_connections 1024; #单个后台进程最大连接数(最大连接数=连接数*进程数)
# 并发总数是 worker_processes 和 worker_connections 的乘积
# 即 max_clients = worker_processes * worker_connections
# 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么
# 为什么上面反向代理要除以4,应该说是一个经验值
# 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
# worker_connections 值的设置跟物理内存大小有关
# 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
# 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
# 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
# $ cat /proc/sys/fs/file-max
# 输出 34336
# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
# 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
# 使得并发总数小于操作系统可以打开的最大文件数目
# 其实质也就是根据主机的物理CPU和内存进行配置
# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
# ulimit -SHn 65535
}
http {
include mime.types; #文件扩展名与文件类型映射表;设定mime类型,类型由mime.type文件定义
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;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数(zero copy 方式)来输出文件,对于普通应用设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#tcp_nopush on;
#keepalive_timeout 0; #长连接超时时间,单位是秒
keepalive_timeout 65;
#gzip on; #启用Gizp压缩
#服务器的集群
upstream test { #服务器集群名字
ip_hash;
server 127.0.0.1:8080 ;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
#server 127.0.0.1:8380 weight=2; #我这里就先用一个做实验了,多配的话跟一个是一样的
}
#当前的Nginx的配置
server {
listen 80;
#server_name localhost; # 当前服务的域名 #要是想直接通过ip访问就像我这样把这个注掉,这样可以让局域网的其他机子也访问到
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;
#这里太坑了,各个请求一开始都发部出去,可以搜索以下“nginx location 配置/正则”之类的,能找到一些提示
#产品介绍页面
#这个是主页,所以没有写html文件
location
^~ /product {
#这个正则大概是匹配:以“/product”开头区分大小写的请求
proxy_pass http://test/pd/; proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#提示页面
location ^~ /notice {
#这个就是有页面的了
proxy_pass http://test/pd/xxx.html; proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#获取服务器时间请求
location /getServerTime {
#这是个servlet
proxy_pass http://test/pd/getServerTime;
}
#指定静态文件访问根目录
#这个很重要,没有的话所有文件都访问不来
location ~ \.(html|js|css|png|gif|woff|ttf|woff2|pdf|properties|worker.js)$ {
#alias D:\apache-tomcat-7.0.54\webapps\pd;
expires 30d;
root D:\apache-tomcat-7.0.54\webapps\pd;
}
#产品介绍信息请求
location /productLoad.do {
#这就是一个ajax请求了
proxy_pass http://test/pd/product/productLoad.do; proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#投保须知内容显示请求
#这里是pdfjs的页面
location /viewer {
proxy_pass http://test/pd/res/pdfJs/generic/web/viewer.html;
}
#图标加载指定目录
#这个因为在页面上会报错,就加上了
location ~ /favicon.ico$ {
root D:\apache-tomcat-7.0.54\webapps\ROOT;
}
# 定义错误提示页面
#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
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
#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
#禁止访问 .htxxx 文件
#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;
# }
#}
}
好了,到这里就配完了,当然里面很多请求配置我都省略了,然后我们启动nginx
第一个是reload,然后start,如果有修改过配置文件就先运行reload,没有的话就直接start运行一下,运行后就是一闪而过
打开windows的任务管理器,看有没有nginx.exe的进程就知道开没开了,要是没有开,极大可能是因为修改的nginx.conf有错误
然后就直接访问项目:(因为我不想通过域名访问,本地只能是localhost:80,80端口是可以不写的)
http://127.0.0.1/product?pid=1
经过nginx反向代理后,也不需要在后台转发请求重定向什么的了,通通取消
我的示例就做到这里了,有没有大神指点一下我这菜鸟?
网上贴的资料感觉好少,不知是我没找对方向还是怎么着,都找不到想要的,折腾了好几天,总算弄好了,不过也不知道到底对不对,有没有更好的办法。
先贴上来,做个记录,大家要是会的也给指点指点。
背景:项目使用springmvc4-hibernate4
首先要先去nginx官方网站去下nginx压缩包。是tar.gz格式的,windows我是直接解压后安装用,linux的话在我的前两篇blog中有稍微记录。
文章地址:http://blog.csdn.net/muluo7fen/article/details/77773846
下载文件:http://download.csdn.net/download/muluo7fen/9960637
安装过程就略过了,自己百度一下,还是比较多的。
4000
这里提一下有个比较好用的办法,见下图,我根据网上的办法弄了.bat文件真的很方便,双击运行,不用再用命令了。
我是使用Notepad++软件编辑保存的。三个文件的内容分别如下,按图序:
nginx -s reload
start nginx
nginx -s stop
我是使用myeclipse开发项目的,所以就直接在myeclipse里面用tomcat开关了,这样比较方便,初步一个先这么着。
所以应用服务器就不用管了,用现成的,项目也编译好在tomcat上了,剩下就是nginx配置了。
贴上我的配置文件:在解压的nginx文件内的conf文件中:nginx.conf可以直接用文本编辑
#运行用户
#user nobody;
worker_processes 1; #工作进程的个数,一般与计算机的cpu核数一致
#全局错误日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工作模式及连接数上限
events {
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,
#仅用于linux2.6以上内核,可以大大提高nginx的性能
#use epoll;
worker_connections 1024; #单个后台进程最大连接数(最大连接数=连接数*进程数)
# 并发总数是 worker_processes 和 worker_connections 的乘积
# 即 max_clients = worker_processes * worker_connections
# 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么
# 为什么上面反向代理要除以4,应该说是一个经验值
# 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
# worker_connections 值的设置跟物理内存大小有关
# 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
# 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
# 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
# $ cat /proc/sys/fs/file-max
# 输出 34336
# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
# 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
# 使得并发总数小于操作系统可以打开的最大文件数目
# 其实质也就是根据主机的物理CPU和内存进行配置
# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
# ulimit -SHn 65535
}
http {
include mime.types; #文件扩展名与文件类型映射表;设定mime类型,类型由mime.type文件定义
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;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数(zero copy 方式)来输出文件,对于普通应用设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#tcp_nopush on;
#keepalive_timeout 0; #长连接超时时间,单位是秒
keepalive_timeout 65;
#gzip on; #启用Gizp压缩
#服务器的集群
upstream test { #服务器集群名字
ip_hash;
server 127.0.0.1:8080 ;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
#server 127.0.0.1:8380 weight=2; #我这里就先用一个做实验了,多配的话跟一个是一样的
}
#当前的Nginx的配置
server {
listen 80;
#server_name localhost; # 当前服务的域名 #要是想直接通过ip访问就像我这样把这个注掉,这样可以让局域网的其他机子也访问到
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;
#这里太坑了,各个请求一开始都发部出去,可以搜索以下“nginx location 配置/正则”之类的,能找到一些提示
#产品介绍页面
#这个是主页,所以没有写html文件
location
^~ /product {
#这个正则大概是匹配:以“/product”开头区分大小写的请求
proxy_pass http://test/pd/; proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#提示页面
location ^~ /notice {
#这个就是有页面的了
proxy_pass http://test/pd/xxx.html; proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#获取服务器时间请求
location /getServerTime {
#这是个servlet
proxy_pass http://test/pd/getServerTime;
}
#指定静态文件访问根目录
#这个很重要,没有的话所有文件都访问不来
location ~ \.(html|js|css|png|gif|woff|ttf|woff2|pdf|properties|worker.js)$ {
#alias D:\apache-tomcat-7.0.54\webapps\pd;
expires 30d;
root D:\apache-tomcat-7.0.54\webapps\pd;
}
#产品介绍信息请求
location /productLoad.do {
#这就是一个ajax请求了
proxy_pass http://test/pd/product/productLoad.do; proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#投保须知内容显示请求
#这里是pdfjs的页面
location /viewer {
proxy_pass http://test/pd/res/pdfJs/generic/web/viewer.html;
}
#图标加载指定目录
#这个因为在页面上会报错,就加上了
location ~ /favicon.ico$ {
root D:\apache-tomcat-7.0.54\webapps\ROOT;
}
# 定义错误提示页面
#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
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
#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
#禁止访问 .htxxx 文件
#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;
# }
#}
}
好了,到这里就配完了,当然里面很多请求配置我都省略了,然后我们启动nginx
第一个是reload,然后start,如果有修改过配置文件就先运行reload,没有的话就直接start运行一下,运行后就是一闪而过
打开windows的任务管理器,看有没有nginx.exe的进程就知道开没开了,要是没有开,极大可能是因为修改的nginx.conf有错误
然后就直接访问项目:(因为我不想通过域名访问,本地只能是localhost:80,80端口是可以不写的)
http://127.0.0.1/product?pid=1
经过nginx反向代理后,也不需要在后台转发请求重定向什么的了,通通取消
我的示例就做到这里了,有没有大神指点一下我这菜鸟?
相关文章推荐
- Windows环境下,Nginx+tomcat+redis实现web项目集群
- nginx+keepalived+tomcat配置高可用web集群
- Windows环境下,Nginx+tomcat+redis实现web项目集群
- tomcat集群配置session,并用nginx做反向代理和负载均衡
- 成功配置了apache tomcat + 花生壳 外网访问自己电脑上的web项目
- Nginx相关指令和Nginx+tomcat配置集群负载均衡配置文件nginx.conf
- IDEA 开发Web项目,配置tomcat时,设置项目路径,启动后404问题
- Nginx+tomcat配置集群负载均衡
- eclipse配置Tomcat服务器Server Locations,可以配置web项目自动部署到tomcat
- eclipse进行Web开发环境配置-设置自动部署WEB项目到我们配置的tomcat服务器
- IntelliJ IDEA 2017 配置Tomcat 运行Web项目
- Nginx+tomcat配置集群负载均衡
- Nginx+Tomcat 配置负载均衡集群
- Nginx+Keepalived+Tomcat高可用负载均衡,Zookeeper集群配置,Mysql(MariaDB)搭建,Redis安装,FTP配置
- EclipseEE版本中配置Tomcat发布Web项目及相关
- 详解使用Nginx和uWSGI配置Python的web项目的方法
- Nginx+tomcat配置集群
- MyEclipse 配置Tomcat和创建Web项目
- LINUX部署WEB项目(TOMCAT安装和配置)
- Nginx+tomcat配置集群负载均衡