如何使NGINX提供静态内容,如.js,.css,.html? [英] how to make NGINX serve static content like .js, .css, .html?

查看:759
本文介绍了如何使NGINX提供静态内容,如.js,.css,.html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始使用NGINX,我发现我们可以使用它进行反向代理,从自身提供静态内容,这可以减少加载时间。我在我的本地机器上有一个Tomcat / JBoss服务器,我想把NGINX放在它的前面,以便静态内容将由NGINX提供,并由Tomcat / JBoss来完成。我的Tomcat / JBoss应用程序运行在 http:// localhost:8081 / Test 我的NGINX配置工作正常,但它不能加载 css / js / jpg 文件。这里是我的战争时期的静态内容是

Recently I have started using NGINX, I found that we can use it for reverse proxy, serving static content from itself which can reduce load time. I have a Tomcat/JBoss server on my local machine and I want to put NGINX in front of it so that static content will be served from NGINX and rest all by Tomcat/JBoss. My Tomcat/JBoss application is running on http://localhost:8081/Test my NGINX configuration worked properly but it is not able to load css/js/jpg file. Here is my war strcuture wehere static contents are

Test.war

TEST
  |
  |--->Resources
  |       |------->CSS
  |       |         |----> style.css
  |       |
  |       |-------->Images
  |                  |----> a.jpg
  |                  |----> b.jpg
  |   
  |--->WEB-INF
  |        |----->Web.xml
  |        |----->spring-servlet.xml
  |
  |--->JSP
         |---->login.jsp


$ b b

我认为问题是因为绝对路径,所以我应该复制资源文件夹,并将其放在NGINX的某个文件夹中,并配置我的NGINX从自己的目录选择文件,而不是去Tomcat / JBoss?我是新的,所以我不知道这样做,任何人都可以帮助我在这。这是我的NGINX的conf文件(windows)

I think the problem is because of absolute path, so should I copy resources folder and put it in some folder in NGINX and configure my NGINX to pick file from its own directory rather going to Tomcat/JBoss? I am new so I dont have any idea of doing this can anyone pls help me in this. This is my conf file for NGINX(windows)

server {
        listen 80;
        server_name localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
             proxy_pass http://127.0.0.1:8081/Test/;
        }


推荐答案

位置与regexp:

You can add location with regexp:

server {
    listen 80;
    server_name localhost;

    location ~* \.(js|jpg|png|css)$ {
        root path/to/tomcat/document/root/Test/;
        expires 30d;
    }

    location / {
        proxy_pass http://127.0.0.1:8081/Test/;
    }
}

这篇关于如何使NGINX提供静态内容,如.js,.css,.html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆