Java SpringMVC使用nginx和WildFly实现动静分离

1. 运行环境
SUSE Linux Enterprise Server 12 SP1
nginx-1.11.2
WildFly10.0
java version 1.8.0_121
开发的Web项目用到spring-mvc
2. 如何安装略,重点是部署的注意事项
   要实现动静分离,即把静态的部分交给nginx返回,动态的部分交给WildFly执行。
把eclipse打包的war项目部署在两个地方,一个地方是nginx访问的部分,另一部分是
WildFly10.0动态执行的部分,这部分按原来的方式部署,不变。由于项目使用到springmvc,
MVC无后缀,故而把这部分路径配置到nginx.conf的最后,即前面部分都没有匹配到,就跳
转到WildFly10.0后台处理。如我的Web项目打名后名为MyWeb.war,将MyWeb.war项目部署到
WildFly10.0的部署目录下,再将MyWeb.war用rar软件解压,将MyWeb目录旋转到nginx指定的
位置。


3. nginx配置文件说明

#user  nobody;
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 {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset utf-8;
    #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;
    upstream jboss_web_server{
    server localhost:8080 weight=10;#localhost:8080为WildFly10.0配置的站点
    }
    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    gzip  on;


    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;
index index.jsp index.htm index.php;
root /data/nginx_web;   #nginx访问的静态资源部分放于此处,全路径为/data/nginx_web/MyWeb
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
default_type 'text/html';
charset utf-8;
    #以下后缀类型的资源访问转向WildFly后台处理  
location ~ .*\.(jsp|jspx|do|wsdl)?$
{
  proxy_pass http://jboss_web_server;
}
#静态资源访问由nginx直接返回,无需跳转到后端  
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|apk|tar.gz)$
{
  expires 30d;
  root /data/nginx_web;
}
#静态资源访问由nginx直接返回,无需跳转到后端  
location ~ .*\.(js|css)?$
{
 expires 12d;
 root /data/nginx_web;
}
#spring-mvc调用跳转到后端处理,即前面都没有匹配上的内容跳转到后台,
#这一句不能放到最前面,放到前面会把所有访问都跳到后台了。
    location / 
{
      charset GBK;
      proxy_pass http://jboss_web_server;
    }


        #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       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;


    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值