搭建Nginx+Java环境(详细)

来源:互联网 发布:电子相册软件app排行榜 编辑:程序博客网 时间:2024/06/13 00:16
Nginx的配置文件都存于目录conf文件下,其中nginx.conf是它的主配置文件。

以下为我加上注释并配置的新的虚拟server

[plain] view plaincopyprint?
  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. http { 
  21.     #文件扩展名与文件类型映射表 
  22.     include       mime.types; 
  23.     #默认文件类型 
  24. default_type application/octet-stream;
  25.     #日志文件输出格式 这个位置相于全局设置 
  26.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  27.     #                  '$status $body_bytes_sent "$http_referer" ' 
  28.     #                  '"$http_user_agent" "$http_x_forwarded_for"'; 
  29.      
  30.     #请求日志保存位置 
  31.     #access_log  logs/access.log  main; 
  32.      
  33.     #设定请求缓冲 
  34.     client_header_buffer_size 1k; 
  35.     large_client_header_buffers 4 4k; 
  36.  
  37.     #打开发送文件 
  38.     sendfile        on; 
  39.     #tcp_nopush     on; 
  40.  
  41.     #keepalive_timeout  0; 
  42.     keepalive_timeout  65; 
  43.      
  44.     #客户端上传文件大小控制 
  45.     client_max_body_size 8m; 
  46.      
  47.     #打开gzip压缩 
  48.     #gzip  on; 
  49.      
  50.     #设定负载均衡的服务器列表 
  51.     #upstream mysvr { 
  52.     #    #weigth参数表示权值,权值越高被分配到的几率越大 
  53.     #    #本机上的Squid开启3128端口 
  54.     #    #server 192.168.8.1:3128 weight=5; 
  55.     #    #server 192.168.8.2:80 weight=1; 
  56.     #    #server 192.168.8.3:80 weight=6; 
  57.     #} 
  58.  
  59.     #第一个虚拟主机 
  60.     server { 
  61.         #监听IP端口 
  62.         listen       80; 
  63.         #主机名 
  64.         server_name  localhost; 
  65.         #root   
  66.          
  67.         #设置字符集 
  68.         #charset koi8-r; 
  69.         #本虚拟server的访问日志 相当于局部变量 
  70.         #access_log  logs/host.access.log  main; 
  71.         #日志文件输出格式 
  72.         #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  73.         #                  '$status $body_bytes_sent "$http_referer" ' 
  74.         #                  '"$http_user_agent" "$http_x_forwarded_for"'; 
  75.          
  76.         location / { 
  77.             root   html; 
  78.             index  index.html index.htm; 
  79.         } 
  80.          
  81.         #静态文件缓存时间设置 
  82.         #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${          
  83.         #    expires 30d; 
  84.         #} 
  85.          
  86.         #静态文件缓存时间设置 
  87.         #location ~ .*\.(js|css)?${          
  88.         #    expires 1h; 
  89.         #} 
  90.          
  91.         #对本server"/"启用负载均衡 
  92.         #location / { 
  93.         #    proxy_pass http://mysvr; 
  94.         #    proxy_redirect off; 
  95.         #    proxy_set_header Host $host; 
  96.         #    proxy_set_header X-Real-IP $remote_addr; 
  97.         #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  98.         #    client_max_body_size 10m; 
  99.         #    client_body_buffer_size 128k; 
  100.         #    proxy_connect_timeout 90; 
  101.         #    proxy_send_timeout 90; 
  102.         #    proxy_read_timeout 90; 
  103.         #    proxy_buffer_size 4k; 
  104.         #    proxy_buffers 4 32k; 
  105.         #    proxy_busy_buffers_size 64k; 
  106.         #    proxy_temp_file_write_size 64k; 
  107.         #} 
  108.          
  109.         #设定查看Nginx状态的地址 
  110.         #location /NginxStatus { 
  111.         #    stub_status on; 
  112.         #    access_log on; 
  113.         #    auth_basic “NginxStatus”; 
  114.         #    auth_basic_user_file conf/htpasswd; 
  115.         #} 
  116.  
  117.  
  118.  
  119.         #error_page  404              /404.html; 
  120.  
  121.         # redirect server error pages to the static page /50x.html 
  122.         # 
  123.         error_page   500 502 503 504  /50x.html; 
  124.         location = /50x.html { 
  125.             root   html; 
  126.         } 
  127.  
  128.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
  129.         # 
  130.         #location ~ \.php$ { 
  131.         #    proxy_pass   http://127.0.0.1; 
  132.         #} 
  133.  
  134.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
  135.         # 
  136.         #location ~ \.php$ { 
  137.         #    root           html; 
  138.         #    fastcgi_pass   127.0.0.1:9000; 
  139.         #    fastcgi_index  index.php; 
  140.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 
  141.         #    include        fastcgi_params; 
  142.         #} 
  143.  
  144.         # deny access to .htaccess files, if Apache's document root 
  145.         # concurs with nginx's one 
  146.         # 
  147.         #location ~ /\.ht { 
  148.         #    deny  all; 
  149.         #} 
  150.     } 
  151.  
  152.  
  153.     # another virtual host using mix of IP-, name-, and port-based configuration     
  154.     server { 
  155.         #多监听        
  156.         listen       localhost:8666; 
  157.         #主机名 
  158.         server_name  LIULJ2576; 
  159.         #WEB文件路径 
  160.         root         E:/Portal; 
  161.         #默认首页 
  162.         index        HomePage.html;         
  163.         #location / { 
  164.         #    #这里相当于局部变量 
  165.         #    root   E:/Portal; 
  166.         #    index  HomePage.html; 
  167.         #} 
  168.     } 
  169.  
  170.  
  171.     # HTTPS server HTTPS SSL加密服务器 
  172.     # 
  173.     #server { 
  174.     #    listen       443; 
  175.     #    server_name  localhost; 
  176.  
  177.     #    ssl                  on; 
  178.     #    ssl_certificate      cert.pem; 
  179.     #    ssl_certificate_key  cert.key; 
  180.  
  181.     #    ssl_session_timeout  5m; 
  182.  
  183.     #    ssl_protocols  SSLv2 SSLv3 TLSv1; 
  184.     #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; 
  185.     #    ssl_prefer_server_ciphers   on; 
  186.  
  187.     #    location / { 
  188.     #        root   html; 
  189.     #        index  index.html index.htm; 
  190.     #    } 
  191.     #} 
  192.  

#号为注释内容,我们在cmd下运行nginx

启动成功,出错的话,可以查询日志(日志路径是配置文件指定的,你可以修改存到其它位置)

访问一下第二个server 配置的localhost:8666地址,结果出现

三、Nginx可以通过以下两种方式来实现与Tomcat的耦合:

将静态页面请求交给Nginx,动态请求交给后端Tomcat处理。

将所有请求都交给后端的Tomcat服务器处理,同时利用Nginx自身的负载均衡功能进行多台Tomcat服务器的负载均衡。

下面通过两个配置实例分别讲述这两种实现

一、动态页面和静态页面分离的实例

这里假定Tomcat服务器的IP地址为192.168.12.130,同时Tomcat服务器开放的服务器端口为8080。Nginx相关配置代码如下:

[plain] view plaincopyprint?
  1. server {   
  2.       listen 80;   
  3.       server_name www.ixdba.net;   
  4.       root /web/www/html;   
  5.   
  6. location /img/ {   
  7.       alias /web/www/html/img/;   
  8. }   
  9.   
  10. location ~ (\.jsp)|(\.do)$ {   
  11.      proxy_pass http://192.168.12.130:8080;   
  12.      proxy_redirect off;   
  13.      proxy_set_header Host $host;   
  14.      proxy_set_header X-Real-IP $remote_addr;   
  15.      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
  16.      client_max_body_size 10m;   
  17.      client_body_buffer_size 128k;   
  18.      proxy_connect_timeout 90;   
  19.      proxy_send_timeout 90;   
  20.      proxy_read_timeout 90;   
  21.      proxy_buffer_size 4k;   
  22.      proxy_buffers 4 32k;   
  23.      proxy_busy_buffers_size 64k;   
  24.      proxy_temp_file_write_size 64k;   
  25. }   
  26.   
  27. }  


在这个实例中,首先定义了一个虚拟主机www.ixdba.net,然后通过location指令将/web/www/html/img/目录下的静态文件交给Nginx来完成。最后一个location指令将所有以.jsp、.do结尾的文件都交给Tomcat服务器的8080端口来处理,即http://192.168.12.130:8080。

需要特别注意的是,在location指令中使用正则表达式后,proxy_pass后面的代理路径不能含有地址链接,也就是不能写成http://192.168.12.130:8080/,或者类似http://192.168.12.130:8080/jsp的形式。在location指令不使用正则表达式时,没有此限制。

2、多个tomcat负载均衡的实例

这里假定有3台Tomcat服务器,分别开放不同的端口,地址如下:

[plain] view plaincopyprint?
  1. 192.168.12.131:8000   
  2. 192.168.12.132:8080   
  3. 192.168.12.133:8090  

Nginx的相关配置代码如下:

[plain] view plaincopyprint?
  1. upstream mytomcats {   
  2.       server 192.168.12.131:8000;   
  3.       server 192.168.12.132:8080;   
  4.       server 192.168.12.133:8090;   
  5. }   
  6.   
  7. server {   
  8.       listen 80;   
  9.       server_name www.ixdba.net;   
  10.   
  11. location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {   
  12.        root /web/www/html/;   
  13. }   
  14.   
  15. location / {   
  16.           proxy_pass http://mytomcats;   
  17.           proxy_redirect off;   
  18.           proxy_set_header Host $host;   
  19.           proxy_set_header X-Real-IP $remote_addr;   
  20.           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
  21.           client_max_body_size 10m;   
  22.           client_body_buffer_size 128k;   
  23.           proxy_connect_timeout 90;   
  24.           proxy_send_timeout 90;   
  25.           proxy_read_timeout 90;   
  26.           proxy_buffer_size 4k;   
  27.           proxy_buffers 4 32k;   
  28.           proxy_busy_buffers_size 64k;   
  29.           proxy_temp_file_write_size 64k;   
  30. }   
  31.   
  32. }  
0 0
原创粉丝点击