如何让 NGINX 提供像 .js、.css、.html 这样的静态内容? [英] how to make NGINX serve static content like .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
测试.war
TEST
|
|--->Resources
| |------->CSS
| | |----> style.css
| |
| |-------->Images
| |----> a.jpg
| |----> b.jpg
|
|--->WEB-INF
| |----->Web.xml
| |----->spring-servlet.xml
|
|--->JSP
|---->login.jsp
我认为问题是因为绝对路径,所以我应该复制资源文件夹并将其放在NGINX中的某个文件夹中并配置我的NGINX从其自己的目录中选择文件而不是去Tomcat/JBoss吗?我是新手,所以我不知道这样做任何人都可以帮助我.这是我的 NGINX(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/;
}
推荐答案
您可以使用正则表达式添加位置:
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屋!