linux下Nginx+Tomcat实现动静分离架构实现

nginx和tomcat的安装配置这里不再赘述,可以查看之前的tomcat配置笔记:https://sulao.cn/post/210.html

直接上干货代码

nginx配置如下

server{
    listen 80;
    server_name www.test.com;
    index index.html index.htm;
    root /usr/local/nginx/html;
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${
        root /usr/local/nginx/html;
        expires 30d;
    }
    location ~ (\.jsp)|(\.do)${
        proxy_pass http://192.168.1.2:8080;
        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;
    }
    location ~ .*\.(js|css)?${
        expires 1h;
    }
tomcat需要修改的配置
首先,修改$tomcat/conf/server.xml文件。
在server.xml文件中,有一段如下:
……
<enginename="Catalina"defaultHost="localhost">
<hostname="localhost"appBase="webapps"
unpackWARs="true"autoDeploy="true"
xmlValidation="false"xmlNamespaceAware="false">
……
<host>
</engine>
……

在<host></host>标签之间添加上:

<Contextpath=""docBase="html"debug="0"reloadable="true"/>

path是说明虚拟目录的名字,如果你要只输入ip地址就显示主页,则该键值留为空;
docBase是虚拟目录的路径,它默认的是$tomcat/webapps/ROOT目录,现在我在webapps目录下建了一个html目录,让该目录作为我的默认目录。
debug和reloadable一般都分别设置成0和true。

然后,修改$tomcat/conf/web.xml文件。
在web.xml文件中,有一段如下:

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

在<welcome-file-list>与<welcome-file>index.html</welcome-file>之间添加上:

<welcome-file>html</welcome-file>

在/usr/local/tomcat/webapps/html目录下新建一个index.jsp,一个index.do

然后在浏览器上访问静态页面index.html

在浏览器上访问动态页面index.jsp、index.do

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/341.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。