转自搭建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.jsphttp://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;
    #    }
    #}

}

转自:https://blog.csdn.net/cupidove/article/details/29596541

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • 各位读者朋友,多多支持百里庆之的盗墓小说《盗墓诡咒》,多多支持,求推荐。 http://m.motie.com/w...
    秦北书生阅读 236评论 0 0
  • UITableView性能优化,这个问题只要做iOS研发相关工作的人都会遇到,或是工作开发需要,或者面试问题。 我...
    简鱼7819阅读 554评论 1 3
  • 2018年03月16日 今日分享:今天晚上是儿子游泳训练的日子,以往都是周四晚上,教练为了孩子无负担的训练从这...
    幸福树328阅读 107评论 0 0
  • 自从有了小宝之后,在两年多的时间里,自己就没在理发店洗过头发,一直是在家洗澡洗头一次搞定,也没有想去做个新...
    东方白1阅读 181评论 4 1