搭建Nginx+Java环境测试并且运行
一、简介:
Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱。虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很多。
二、下载安装:
下载nginx
http://nginx.org/en/download.html
下载解压后放到F:\nginx-1.7.1(官网这样要求的,不知道放其它盘有没有问题)
启动nginx.exe,然后在浏览器输入127.0.0.1即可
配置自己的项目测试
第二环节我们使用了默认的nginx.conf 。Nginx的配置文件都存于目录conf文件下,其中nginx.conf是它的主配置文件。
以下为我加上注释并配置的新的虚拟server
- #运行用户
- #user nobody;
- #开启进程数 <=CPU数
- worker_processes 1;
- #错误日志保存位置
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #进程号保存文件
- #pid logs/nginx.pid;
- #等待事件
- events {
- #Linux下打开提高性能
- #use epoll;
- #每个进程最大连接数(最大连接=连接数x进程数)
- worker_connections 1024;
- }
- 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;
- #设定请求缓冲
- client_header_buffer_size 1k;
- large_client_header_buffers 4 4k;
- #打开发送文件
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #客户端上传文件大小控制
- client_max_body_size 8m;
- #打开gzip压缩
- #gzip on;
- #设定负载均衡的服务器列表
- #upstream mysvr {
- # #weigth参数表示权值,权值越高被分配到的几率越大
- # #本机上的Squid开启3128端口
- # #server 192.168.8.1:3128 weight=5;
- # #server 192.168.8.2:80 weight=1;
- # #server 192.168.8.3:80 weight=6;
- #}
- #第一个虚拟主机
- server {
- #监听IP端口
- listen 80;
- #主机名
- server_name localhost;
- #root
- #设置字符集
- #charset koi8-r;
- #本虚拟server的访问日志 相当于局部变量
- #access_log logs/host.access.log main;
- #日志文件输出格式
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- location / {
- root html;
- index index.html index.htm;
- }
- #静态文件缓存时间设置
- #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${
- # expires 30d;
- #}
- #静态文件缓存时间设置
- #location ~ .*\.(js|css)?${
- # expires 1h;
- #}
- #对本server"/"启用负载均衡
- #location / {
- # proxy_pass http://mysvr;
- # proxy_redirect off;
- # proxy_set_header Host $host;
- # proxy_set_header X-Real-IP $remote_addr;
- # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- # client_max_body_size 10m;
- # client_body_buffer_size 128k;
- # proxy_connect_timeout 90;
- # proxy_send_timeout 90;
- # proxy_read_timeout 90;
- # proxy_buffer_size 4k;
- # proxy_buffers 4 32k;
- # proxy_busy_buffers_size 64k;
- # proxy_temp_file_write_size 64k;
- #}
- #设定查看Nginx状态的地址
- #location /NginxStatus {
- # stub_status on;
- # access_log on;
- # auth_basic “NginxStatus”;
- # auth_basic_user_file conf/htpasswd;
- #}
- #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 localhost:8666;
- #主机名
- server_name LIULJ2576;
- #WEB文件路径
- root E:/Portal;
- #默认首页
- index HomePage.html;
- #location / {
- # #这里相当于局部变量
- # root E:/Portal;
- # index HomePage.html;
- #}
- }
- # HTTPS server HTTPS SSL加密服务器
- #
- #server {
- # listen 443;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
进入cmd。然后进入F:\nginx-1.7.1\
dos环境运行命令:
start nginx
//启动nginx
nginx -s stop // 停止nginx
nginx -s reload // 重新加载配置文件
nginx -s quit // 退出nginx
nginx -t
//检查配置文件是否正确
二、Nginx可以通过以下两种方式来实现与Tomcat的耦合:
将静态页面请求交给Nginx,动态请求交给后端Tomcat处理。
将所有请求都交给后端的Tomcat服务器处理,同时利用Nginx自身的负载均衡功能进行多台Tomcat服务器的负载均衡。
下面通过两个配置实例分别讲述这两种实现
下载Tomcat6:http://mirrors.cnnic.cn/apache/tomcat/tomcat-6/v6.0.41/bin/apache-tomcat-6.0.41-windows-x86.zip
在F:\nginx-1.7.1\路径新建tomcat文件夹。把下载后的apache-tomcat-6.0.41-windows-x86.zip解压。解压后把apache-tomcat-6.0.41更名为apache-tomcat-8080。并复制几个apache-tomcat-8080分别改名为apache-tomcat-8060,apache-tomcat-8090
启动多个tomcat。修改tomcat里面的server.xml配置文件。注意以下修改的四处,各个tomcat配置里面的端口号不要有冲突。例如tomcat1里面的
Server port=18006,则另外一个就不能用此端口。其他的依次类推
《一》
- <!-- 修改port端口:俩个tomcat不能重复,端口随意,别太小-->
- <Server port="18006" shutdown="SHUTDOWN">
《二》
- <!-- port="18081" tomcat监听端口,随意设置,别太小 -->
- <Connector port="18081" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
《三》
- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
《四》
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
在同一台电脑上启动两个tomcat。进入cmd命令模式,然后进入各自的tomcat路径,执行F:\nginx-1.7.1\tomcat\apache-tomcat-8090\bin>startup.bat
F:\nginx-1.7.1\tomcat\apache-tomcat-8080\bin>startup.bat。则两个不同的tomcat已经启动完成
在IE上输入http://localhost/index.jsp和http://localhost/,如果得到不同的界面表示成功
最终的nginx.conf配置如下
- #运行用户
- #user nobody;
- #开启进程数 <=CPU数
- worker_processes 1;
- #错误日志保存位置
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #进程号保存文件
- #pid logs/nginx.pid;
- #等待事件
- events {
- #Linux下打开提高性能
- #use epoll;
- #每个进程最大连接数(最大连接=连接数x进程数)
- worker_connections 1024;
- }
- 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;
- #设定请求缓冲
- client_header_buffer_size 1k;
- large_client_header_buffers 4 4k;
- #打开发送文件
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #客户端上传文件大小控制
- client_max_body_size 8m;
- #打开gzip压缩
- #gzip on;
- #gzip_min_length 1000;
- #gzip_types text/plain text/css application/x-javascript;
- #设定负载均衡的服务器列表
- upstream mysvr {
- #weigth参数表示权值,权值越高被分配到的几率越大
- #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
- #同一机器在多网情况下,路由切换,ip可能不同
- server 127.0.0.1:8080 weight=1;
- server 127.0.0.1:8090 weight=2;
- }
- #第一个虚拟主机
- server {
- #监听IP端口
- listen 80;
- #主机名
- server_name localhost;
- #root
- #设置字符集
- #charset koi8-r;
- #本虚拟server的访问日志 相当于局部变量
- #access_log logs/host.access.log main;
- #日志文件输出格式
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #location / {
- # root html;
- # index index.html index.htm;
- #}
- #静态文件缓存时间设置
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
- expires 30d;
- }
- #静态文件缓存时间设置
- location ~ .*\.(js|css)?$ {
- expires 1h;
- }
- #对本server"/"启用负载均衡
- #如果开启了这里的location,则79行的location必须屏蔽
- #对各种静态还是动态的数据进行过滤
- #此处如果请求是.jsp、.do结尾的文件都交给Tomcat服务器
- #其他的交给nginx处理
- location ~ (\.jsp)|(\.do)$ {
- proxy_pass http://mysvr;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- client_max_body_size 10m;
- client_body_buffer_size 128k;
- proxy_connect_timeout 90;
- proxy_send_timeout 90;
- proxy_read_timeout 90;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- }
- #设定查看Nginx状态的地址
- location /NginxStatus {
- stub_status on;
- access_log on;
- auth_basic “NginxStatus”;
- auth_basic_user_file conf/htpasswd;
- }
- #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 localhost:50000;
- #主机名
- server_name LIULJ2576;
- #WEB文件路径
- root E:/Portal;
- #默认首页
- index HomePage.html;
- #location / {
- # #这里相当于局部变量
- # root E:/Portal;
- # index HomePage.html;
- #}
- }
- # HTTPS server HTTPS SSL加密服务器
- #
- #server {
- # listen 443;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
搭建Nginx+Java环境测试并且运行的更多相关文章
- 搭建Nginx+JAVA环境
搭建Nginx+JAVA环境 Apache对Java的支持很灵活,他们的结合度也很高,例如Apache+Tomcat和Apache+resin等都可以实现对Java应用的支持.Apache一般采用一个 ...
- 搭建基于java环境
.net项目架构改造之搭建基于java环境配置一览[上] 最近公司做了一个项目,需要嵌套在千牛的客户端上,项目代码必须上阿里的聚石塔,全程采用基于docker的自动化部署,我们的项目是基于.net ...
- 使用docker快速搭建nginx+php环境
在朋友的强烈推荐下,走上了docker之路.经过了繁琐的docker环境安装,看了下镜像/容器的简单使用,开始进行nginx+php环境的搭建,本文记录一下在安装过程中的笔记. 原文地址:代码汇个人博 ...
- PHP-Windows下搭建Nginx+PHP环境
项目中光用Nginx了, 由于有运维人员, 很少搭建Nginx服务器, 开发也就用用Apache, 搭过几次Nginx也忘的快, 每次都去翻别人博客, 今天重搭特此记录, 装前最好了解下FastCGI ...
- docker一键搭建Nginx+PHP环境(含自动部署命令)
文章的主要部分是一步一步的教程,文章的最后是我整理好的一键安装命令,自动下载并安装docker,构建镜像,启动容器集群(压缩包内注释覆盖范围达到80%) 大家可以看完教程亲自尝试下,也可以直接执行一键 ...
- 在Windows系统搭建.NET Core环境并创建运行ASP.NET网站
微软于6月27日在红帽DevNation峰会上 正式发布了.NET Core 1.0.ASP.NET 1.0和Entity Framework Core 1.0,其将全部支持Windows.OS X和 ...
- java环境设置与运行
在初学java编程语言时,痛苦的事莫过于跟着示例一步步做,总是得不到想要的结果,这是很多初学者都会碰到的问题.下面详细教你运行第一个java应用程序(环境windows xp + jdk 6.0): ...
- .net项目架构改造之搭建基于java环境配置一览【上】
最近公司做了一个项目,需要嵌套在千牛的客户端上,项目代码必须上阿里的聚石塔,全程采用基于docker的自动化部署,我们的项目是基于.net架构.很遗憾 的是基于windows的docker上部署在访问 ...
- OSX10.12搭建IPv6本地环境测试APP
前记 最近刚换了工作,生活终于又安定下来了,又可以更博了 正文 最近公司在上线APP(整体全是用JS去写的,就用了我原生的一个控制器),然后APP就去上线,就被苹果巴巴给拒了.通过阅读苹果回复的邮件, ...
随机推荐
- ubuntu下载工具uget和aria2
一直想在ubuntu下找到个和迅雷差不多的下载工具.在网上找到了. 这篇文章完全是抄袭整理网上的. 我的系统版本是ubuntu14.04. 1.安装uget和aria2 sudo apt-get in ...
- PHP进程通信基础——shmop 、sem系列函数使用
PHP进程通信基础--shmop .sem系列函数使用 进程通信的原理就是在系统中开辟出一个共享区域,不管是管道也好,还是共享内存,都是这个原理.如果心中有了这个概念,就会很方便去理解代码.由于官网上 ...
- SQL Server 2008 允许远程连接的配置
如果无法通过IP地址远程连接你的SQL Server 2008服务器,可以参考下面的内容进行设置.在进行下述设置之前,应该确保你的网络已经安装设置完毕,服务器已经正常连接到网络中. 1.单击Windo ...
- cent6.4使用
让centos能够上网 刚安装了时,是不能联网的,我们可以通过以下命令更改即可: cd /etc/sysconfig/network-script vi ifcfg-eth0 更改其中的ONBOOT= ...
- github教程
http://www.yangzhiping.com/tech/github.html
- HTTP 基础知识
HTTP是一种协议.HTTP使用流程,一般情况下, 第一:由HTTP客户端发出请求,创建端口. 第二:HTTP服务器在端口监听客户端的请求. 第三:一旦收到请求,HTTP服务器向客户端返回状态和内容. ...
- [Android]优化相关
尽量减少布局的层次,最多10层,可以通过LinearLayout向RelativeLayout的转变来减少层的数量 使用ListView的时候,getView方法中的对象尽量重用
- React 组件性能优化探索实践
转自:http://www.tuicool.com/articles/Ar6Zruq React本身就非常关注性能,其提供的虚拟DOM搭配上Diff算法,实现对DOM操作最小粒度的改变也是非常的高效. ...
- webpack使用的心得
1 . 我们需要使用打包工具,首先第一步就得 执行 npm install进行安装,可是很多时候 加载速度很慢,这个时候我们可以 用淘宝镜像源,参考地址: p.p1 { margin: 0.0px 0 ...
- js function集合
/*跳转并是刷新界面*/ function page_back(){ history.go(-); location.reload(); } function show_div(obj){ if(ob ...