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

  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,则另外一个就不能用此端口。其他的依次类推

《一》

  1. <!--  修改port端口:俩个tomcat不能重复,端口随意,别太小-->  
  2. <Server port="18006" shutdown="SHUTDOWN">  
《二》

  1. <!-- port="18081" tomcat监听端口,随意设置,别太小 -->  
  2. <Connector port="18081" protocol="HTTP/1.1"   
  3.                connectionTimeout="20000"   
  4.                redirectPort="8443" />  
  5.   
  6.    
《三》

  1. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  

《四》

  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配置如下

  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.   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建Spring Boot和Vue前后端分离项目需要以下步骤: 1. 安装环境:首先,需要安装Java JDK和Maven来支持Spring Boot的开发。可以去官方网站下载并安装。然后,安装Node.js和Vue CLI来支持Vue的开发。 2. 创建Spring Boot项目:使用Maven创建一个Spring Boot的项目,可以使用宝塔面板自带的Maven插件进行构建,也可以通过命令行进入项目目录,运行`mvn clean install`来创建项目。 3. 编写后端代码:在Spring Boot项目中编写后端逻辑,包括接口的实现、数据库的操作等。可以选择使用Spring Data JPA来简化对数据库的操作。 4. 创建Vue项目:使用Vue CLI创建一个Vue项目,可以通过宝塔面板自带的Node.js插件进行创建,也可以通过命令行运行`vue create 项目名`来创建。 5. 编写前端代码:在Vue项目中编写前端逻辑,包括页面的布局、调用后台接口等。可以使用axios库来发送HTTP请求。 6. 配置跨域:由于前后端分离的项目可能存在不同端口的情况,需要进行跨域配置。可以在Spring Boot项目的配置文件中添加`@CrossOrigin`注解,允许指定的域访问接口。 7. 打包部署:完成开发后,可以使用Maven将后端项目打包成一个独立的Jar文件。然后,将打包好的Jar文件上传到宝塔面板上的对应目录。将Vue项目使用`npm run build`命令打包成静态文件,然后将打包好的文件上传到宝塔面板对应的目录。 8. 配置Nginx反向代理:为了将前后端项目结合在一起,可以使用Nginx配置反向代理,将前端的请求转发给后端的接口。在宝塔面板上找到对应的站点,进行Nginx配置。 通过以上步骤,就可以搭建一个基于Spring Boot和Vue的前后端分离项目,并将项目部署到宝塔面板上进行访问和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值