搭建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

[java] view plain copy
  1. #運行用戶  
  2. #user  nobody;  
  3. #開啓進程數 <=CPU數  
  4. worker_processes  1;  
  5. #錯誤日誌保存位置  
  6. #error_log  logs/error.log;  
  7. #error_log  logs/error.log  notice;  
  8. #error_log  logs/error.log  info;  
  9. #進程號保存文件  
  10. #pid        logs/nginx.pid;  
  11.   
  12. #等待事件  
  13. events {  
  14.     #Linux下打開提高性能  
  15.     #use epoll;  
  16.     #每個進程最大連接數(最大連接=連接數x進程數)  
  17.     worker_connections  1024;  
  18. }  
  19.   
  20.   
  21. http {  
  22.     #文件擴展名與文件類型映射表  
  23.     include       mime.types;  
  24.     #默認文件類型  
  25.     default_type  application/octet-stream;  
  26.     #日誌文件輸出格式 這個位置相於全局設置  
  27.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  28.     #                  '$status $body_bytes_sent "$http_referer" '  
  29.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  30.       
  31.     #請求日誌保存位置  
  32.     #access_log  logs/access.log  main;  
  33.       
  34.     #設定請求緩衝  
  35.     client_header_buffer_size 1k;  
  36.     large_client_header_buffers 4 4k;  
  37.   
  38.     #打開發送文件  
  39.     sendfile        on;  
  40.     #tcp_nopush     on;  
  41.   
  42.     #keepalive_timeout  0;  
  43.     keepalive_timeout  65;  
  44.       
  45.     #客戶端上傳文件大小控制  
  46.     client_max_body_size 8m;  
  47.       
  48.     #打開gzip壓縮  
  49.     #gzip  on;  
  50.       
  51.     #設定負載均衡的服務器列表  
  52.     #upstream mysvr {  
  53.     #    #weigth參數表示權值,權值越高被分配到的機率越大  
  54.     #    #本機上的Squid開啓3128端口  
  55.     #    #server 192.168.8.1:3128 weight=5;  
  56.     #    #server 192.168.8.2:80 weight=1;  
  57.     #    #server 192.168.8.3:80 weight=6;  
  58.     #}  
  59.   
  60.     #第一個虛擬主機  
  61.     server {  
  62.         #監聽IP端口  
  63.         listen       80;  
  64.         #主機名  
  65.         server_name  localhost;  
  66.         #root    
  67.           
  68.         #設置字符集  
  69.         #charset koi8-r;  
  70.         #本虛擬server的訪問日誌 相當於局部變量  
  71.         #access_log  logs/host.access.log  main;  
  72.         #日誌文件輸出格式  
  73.         #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  74.         #                  '$status $body_bytes_sent "$http_referer" '  
  75.         #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  76.           
  77.         location / {  
  78.             root   html;  
  79.             index  index.html index.htm;  
  80.         }  
  81.           
  82.         #靜態文件緩存時間設置  
  83.         #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${           
  84.         #    expires 30d;  
  85.         #}  
  86.           
  87.         #靜態文件緩存時間設置  
  88.         #location ~ .*\.(js|css)?${           
  89.         #    expires 1h;  
  90.         #}  
  91.           
  92.         #對本server"/"啓用負載均衡  
  93.         #location / {  
  94.         #    proxy_pass http://mysvr;  
  95.         #    proxy_redirect off;  
  96.         #    proxy_set_header Host $host;  
  97.         #    proxy_set_header X-Real-IP $remote_addr;  
  98.         #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  99.         #    client_max_body_size 10m;  
  100.         #    client_body_buffer_size 128k;  
  101.         #    proxy_connect_timeout 90;  
  102.         #    proxy_send_timeout 90;  
  103.         #    proxy_read_timeout 90;  
  104.         #    proxy_buffer_size 4k;  
  105.         #    proxy_buffers 4 32k;  
  106.         #    proxy_busy_buffers_size 64k;  
  107.         #    proxy_temp_file_write_size 64k;  
  108.         #}  
  109.           
  110.         #設定查看Nginx狀態的地址  
  111.         #location /NginxStatus {  
  112.         #    stub_status on;  
  113.         #    access_log on;  
  114.         #    auth_basic “NginxStatus”;  
  115.         #    auth_basic_user_file conf/htpasswd;  
  116.         #}  
  117.   
  118.   
  119.   
  120.         #error_page  404              /404.html;  
  121.   
  122.         # redirect server error pages to the static page /50x.html  
  123.         #  
  124.         error_page   500 502 503 504  /50x.html;  
  125.         location = /50x.html {  
  126.             root   html;  
  127.         }  
  128.   
  129.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  130.         #  
  131.         #location ~ \.php$ {  
  132.         #    proxy_pass   http://127.0.0.1;  
  133.         #}  
  134.   
  135.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  136.         #  
  137.         #location ~ \.php$ {  
  138.         #    root           html;  
  139.         #    fastcgi_pass   127.0.0.1:9000;  
  140.         #    fastcgi_index  index.php;  
  141.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  142.         #    include        fastcgi_params;  
  143.         #}  
  144.   
  145.         # deny access to .htaccess files, if Apache's document root  
  146.         # concurs with nginx's one  
  147.         #  
  148.         #location ~ /\.ht {  
  149.         #    deny  all;  
  150.         #}  
  151.     }  
  152.   
  153.   
  154.     # another virtual host using mix of IP-, name-, and port-based configuration      
  155.     server {  
  156.         #多監聽         
  157.         listen       localhost:8666;  
  158.         #主機名  
  159.         server_name  LIULJ2576;  
  160.         #WEB文件路徑  
  161.         root         E:/Portal;  
  162.         #默認首頁  
  163.         index        HomePage.html;          
  164.         #location / {  
  165.         #    #這裏相當於局部變量  
  166.         #    root   E:/Portal;  
  167.         #    index  HomePage.html;  
  168.         #}  
  169.     }  
  170.   
  171.   
  172.     # HTTPS server HTTPS SSL加密服務器  
  173.     #  
  174.     #server {  
  175.     #    listen       443;  
  176.     #    server_name  localhost;  
  177.   
  178.     #    ssl                  on;  
  179.     #    ssl_certificate      cert.pem;  
  180.     #    ssl_certificate_key  cert.key;  
  181.   
  182.     #    ssl_session_timeout  5m;  
  183.   
  184.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  185.     #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
  186.     #    ssl_prefer_server_ciphers   on;  
  187.   
  188.     #    location / {  
  189.     #        root   html;  
  190.     #        index  index.html index.htm;  
  191.     #    }  
  192.     #}  
  193.   
  194. }  
進入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,則另外一個就不能用此端口。其他的依次類推

《一》

[java] view plain copy
  1. <!--  修改port端口:倆個tomcat不能重複,端口隨意,別太小-->  
  2. <Server port="18006" shutdown="SHUTDOWN">  
《二》

[java] view plain copy
  1. <!-- port="18081" tomcat監聽端口,隨意設置,別太小 -->  
  2. <Connector port="18081" protocol="HTTP/1.1"   
  3.                connectionTimeout="20000"   
  4.                redirectPort="8443" />  
  5.   
  6.    
《三》

[java] view plain copy
  1. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  

《四》

[java] view plain copy
  1. <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配置如下

[java] view plain copy
  1. #運行用戶  
  2. #user  nobody;  
  3. #開啓進程數 <=CPU數  
  4. worker_processes  1;  
  5. #錯誤日誌保存位置  
  6. #error_log  logs/error.log;  
  7. #error_log  logs/error.log  notice;  
  8. #error_log  logs/error.log  info;  
  9. #進程號保存文件  
  10. #pid        logs/nginx.pid;  
  11.   
  12. #等待事件  
  13. events {  
  14.     #Linux下打開提高性能  
  15.     #use epoll;  
  16.     #每個進程最大連接數(最大連接=連接數x進程數)  
  17.     worker_connections  1024;  
  18. }  
  19.   
  20.   
  21. http {  
  22.     #文件擴展名與文件類型映射表  
  23.     include       mime.types;  
  24.     #默認文件類型  
  25.     default_type  application/octet-stream;  
  26.     #日誌文件輸出格式 這個位置相於全局設置  
  27.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  28.     #                  '$status $body_bytes_sent "$http_referer" '  
  29.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  30.       
  31.     #請求日誌保存位置  
  32.     #access_log  logs/access.log  main;  
  33.       
  34.     #設定請求緩衝  
  35.     client_header_buffer_size 1k;  
  36.     large_client_header_buffers 4 4k;  
  37.   
  38.     #打開發送文件  
  39.     sendfile        on;  
  40.     #tcp_nopush     on;  
  41.   
  42.     #keepalive_timeout  0;  
  43.     keepalive_timeout  65;  
  44.       
  45.     #客戶端上傳文件大小控制  
  46.     client_max_body_size 8m;  
  47.       
  48.     #打開gzip壓縮  
  49.     #gzip  on;  
  50.     #gzip_min_length      1000;    
  51.     #gzip_types         text/plain text/css application/x-javascript;  
  52.       
  53.     #設定負載均衡的服務器列表  
  54.     upstream mysvr {  
  55.         #weigth參數表示權值,權值越高被分配到的機率越大  
  56.         #根據ip計算將請求分配各那個後端tomcat,許多人誤認爲可以解決session問題,其實並不能。    
  57.         #同一機器在多網情況下,路由切換,ip可能不同    
  58.         server 127.0.0.1:8080 weight=1;  
  59.         server 127.0.0.1:8090 weight=2;  
  60.     }  
  61.   
  62.     #第一個虛擬主機  
  63.     server {  
  64.         #監聽IP端口  
  65.         listen       80;  
  66.         #主機名  
  67.         server_name  localhost;  
  68.         #root    
  69.           
  70.         #設置字符集  
  71.         #charset koi8-r;  
  72.         #本虛擬server的訪問日誌 相當於局部變量  
  73.         #access_log  logs/host.access.log  main;  
  74.         #日誌文件輸出格式  
  75.         #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  76.         #                  '$status $body_bytes_sent "$http_referer" '  
  77.         #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  78.           
  79.         #location / {  
  80.         #    root   html;  
  81.         #    index  index.html index.htm;  
  82.         #}  
  83.           
  84.         #靜態文件緩存時間設置  
  85.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {           
  86.             expires 30d;  
  87.         }  
  88.           
  89.         #靜態文件緩存時間設置  
  90.         location ~ .*\.(js|css)?$ {           
  91.             expires 1h;  
  92.         }  
  93.           
  94.         #對本server"/"啓用負載均衡  
  95.         #如果開啓了這裏的location,則79行的location必須屏蔽  
  96.         #對各種靜態還是動態的數據進行過濾  
  97.         #此處如果請求是.jsp、.do結尾的文件都交給Tomcat服務器  
  98.         #其他的交給nginx處理  
  99.         location ~ (\.jsp)|(\.do)$ {    
  100.           proxy_pass http://mysvr;    
  101.           proxy_redirect off;    
  102.           proxy_set_header Host $host;    
  103.           proxy_set_header X-Real-IP $remote_addr;    
  104.           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
  105.           client_max_body_size 10m;    
  106.           client_body_buffer_size 128k;    
  107.           proxy_connect_timeout 90;    
  108.           proxy_send_timeout 90;    
  109.           proxy_read_timeout 90;    
  110.           proxy_buffer_size 4k;    
  111.           proxy_buffers 4 32k;    
  112.           proxy_busy_buffers_size 64k;    
  113.           proxy_temp_file_write_size 64k;    
  114.         }    
  115.         #設定查看Nginx狀態的地址  
  116.         location /NginxStatus {  
  117.             stub_status on;  
  118.             access_log on;  
  119.             auth_basic “NginxStatus”;  
  120.             auth_basic_user_file conf/htpasswd;  
  121.         }  
  122.   
  123.   
  124.   
  125.         #error_page  404              /404.html;  
  126.   
  127.         # redirect server error pages to the static page /50x.html  
  128.         #  
  129.         #error_page   500 502 503 504  /50x.html;  
  130.         #location = /50x.html {  
  131.         #    root   html;  
  132.         #}  
  133.   
  134.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  135.         #  
  136.         #location ~ \.php$ {  
  137.         #    proxy_pass   http://127.0.0.1;  
  138.         #}  
  139.   
  140.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  141.         #  
  142.         #location ~ \.php$ {  
  143.         #    root           html;  
  144.         #    fastcgi_pass   127.0.0.1:9000;  
  145.         #    fastcgi_index  index.php;  
  146.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  147.         #    include        fastcgi_params;  
  148.         #}  
  149.   
  150.         # deny access to .htaccess files, if Apache's document root  
  151.         # concurs with nginx's one  
  152.         #  
  153.         #location ~ /\.ht {  
  154.         #    deny  all;  
  155.         #}  
  156.     }  
  157.   
  158.   
  159.     # another virtual host using mix of IP-, name-, and port-based configuration      
  160.     server {  
  161.         #多監聽         
  162.         listen       localhost:50000;  
  163.         #主機名  
  164.         server_name  LIULJ2576;  
  165.         #WEB文件路徑  
  166.         root         E:/Portal;  
  167.         #默認首頁  
  168.         index        HomePage.html;          
  169.         #location / {  
  170.         #    #這裏相當於局部變量  
  171.         #    root   E:/Portal;  
  172.         #    index  HomePage.html;  
  173.         #}  
  174.     }  
  175.   
  176.   
  177.     # HTTPS server HTTPS SSL加密服務器  
  178.     #  
  179.     #server {  
  180.     #    listen       443;  
  181.     #    server_name  localhost;  
  182.   
  183.     #    ssl                  on;  
  184.     #    ssl_certificate      cert.pem;  
  185.     #    ssl_certificate_key  cert.key;  
  186.   
  187.     #    ssl_session_timeout  5m;  
  188.   
  189.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  190.     #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
  191.     #    ssl_prefer_server_ciphers   on;  
  192.   
  193.     #    location / {  
  194.     #        root   html;  
  195.     #        index  index.html index.htm;  
  196.     #    }  
  197.     #}  
  198.   
  199. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章