轉(zhuǎn)自搭建Nginx+java

一、簡介:

Tomcat在高并發(fā)環(huán)境下處理動態(tài)請求時性能很低,而在處理靜態(tài)頁面更加脆弱。雖然Tomcat的最新版本支持epoll,但是通過Nginx來處理靜態(tài)頁面要比通過Tomcat處理在性能方面好很多。

二、下載安裝:

下載nginx

http://nginx.org/en/download.html

下載解壓后放到F:\nginx-1.7.1(官網(wǎng)這樣要求的,不知道放其它盤有沒有問題)

啟動nginx.exe,然后在瀏覽器輸入127.0.0.1即可

配置自己的項目測試

第二環(huán)節(jié)我們使用了默認的nginx.conf 。Nginx的配置文件都存于目錄conf文件下,其中nginx.conf是它的主配置文件。 以下為我加上注釋并配置的新的虛擬server

#運行用戶
#user  nobody;
#開啟進程數(shù) <=CPU數(shù)
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;
    #每個進程最大連接數(shù)(最大連接=連接數(shù)x進程數(shù))
    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;

    #打開發(fā)送文件
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #客戶端上傳文件大小控制
    client_max_body_size 8m;
    
    #打開gzip壓縮
    #gzip  on;
    
    #設定負載均衡的服務器列表
    #upstream mysvr {
    #    #weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大
    #    #本機上的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 {
        #監(jiān)聽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;
        }
        
        #靜態(tài)文件緩存時間設置
        #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${         
        #    expires 30d;
        #}
        
        #靜態(tài)文件緩存時間設置
        #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狀態(tài)的地址
        #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 {
        #多監(jiān)聽       
        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環(huán)境運行命令:
start nginx
//啟動nginx
nginx -s stop // 停止nginx
nginx -s reload // 重新加載配置文件
nginx -s quit // 退出nginx
nginx -t
//檢查配置文件是否正確
二、Nginx可以通過以下兩種方式來實現(xiàn)與Tomcat的耦合:
將靜態(tài)頁面請求交給Nginx,動態(tài)請求交給后端Tomcat處理。
將所有請求都交給后端的Tomcat服務器處理,同時利用Nginx自身的負載均衡功能進行多臺Tomcat服務器的負載均衡。
下面通過兩個配置實例分別講述這兩種實現(xiàn)
下載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監(jiān)聽端口,隨意設置,別太小 -->
<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路徑,執(zhí)行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已經(jīng)啟動完成

在IE上輸入http://localhost/index.jsphttp://localhost/,如果得到不同的界面表示成功
最終的nginx.conf配置如下

#運行用戶
#user  nobody;
#開啟進程數(shù) <=CPU數(shù)
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;
    #每個進程最大連接數(shù)(最大連接=連接數(shù)x進程數(shù))
    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;

    #打開發(fā)送文件
    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參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大
        #根據(jù)ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實并不能。  
        #同一機器在多網(wǎng)情況下,路由切換,ip可能不同  
        server 127.0.0.1:8080 weight=1;
        server 127.0.0.1:8090 weight=2;
    }

    #第一個虛擬主機
    server {
        #監(jiān)聽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;
        #}
        
        #靜態(tài)文件緩存時間設置
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {         
            expires 30d;
        }
        
        #靜態(tài)文件緩存時間設置
        location ~ .*\.(js|css)?$ {         
            expires 1h;
        }
        
        #對本server"/"啟用負載均衡
        #如果開啟了這里的location,則79行的location必須屏蔽
        #對各種靜態(tài)還是動態(tài)的數(shù)據(jù)進行過濾
        #此處如果請求是.jsp、.do結(jié)尾的文件都交給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狀態(tài)的地址
        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 {
        #多監(jiān)聽       
        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;
    #    }
    #}

}

轉(zhuǎn)自:https://blog.csdn.net/cupidove/article/details/29596541

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 各位讀者朋友,多多支持百里慶之的盜墓小說《盜墓詭咒》,多多支持,求推薦。 http://m.motie.com/w...
    秦北書生閱讀 289評論 0 0
  • UITableView性能優(yōu)化,這個問題只要做iOS研發(fā)相關(guān)工作的人都會遇到,或是工作開發(fā)需要,或者面試問題。 我...
    簡魚7819閱讀 611評論 1 3
  • 2018年03月16日 今日分享:今天晚上是兒子游泳訓練的日子,以往都是周四晚上,教練為了孩子無負擔的訓練從這...
    幸福樹328閱讀 131評論 0 0
  • 自從有了小寶之后,在兩年多的時間里,自己就沒在理發(fā)店洗過頭發(fā),一直是在家洗澡洗頭一次搞定,也沒有想去做個新...
    東方白1閱讀 245評論 4 1

友情鏈接更多精彩內(nèi)容