26

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

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/;
        }
Pulkit
  • 3,953
  • 6
  • 31
  • 55
  • Adding a more specific location `/Resources/` that you point at the absolute path should work. (you need to point it at the exploded war folder) – Thilo May 21 '14 at 07:30
  • so I should put a Resources folder in my NGINX server directory? – Pulkit May 21 '14 at 07:31
  • actually what I am thinking is to put all my static content in NGINX server directory and they should be served from here instead of loading them from JBoss/Tomcat but I dont know how to configure NGINX to load all these static content from its own server directory – Pulkit May 21 '14 at 07:33
  • That should also work. But you need to stop the proxy_pass from being effective. Define a more specific location. – Thilo May 21 '14 at 07:39
  • @Thilo: Do you have any sample conf file where you did this to look for static content in NGINX directory and rest all to JBoss directory??? – Pulkit May 21 '14 at 07:51
  • do you really need to do that? You cannot limit the paths (for example to /Resources)? – Thilo May 21 '14 at 09:27
  • sorry I didn't get you, anyways I will try this and will let you know – Pulkit May 21 '14 at 09:30

3 Answers3

41

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/;
    }
}
ist
  • 435
  • 5
  • 5
6

Try

server {
    listen 80;
    server_name localhost;

    location ~* \.(css|js|gif|jpe?g|png)$ {
        expires 168h;
    }

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

How to test

In your CLI run ab -c 20 -n 1000 https://your-site/any-file

You will see Time taken for tests decrease drastically.

code-8
  • 54,650
  • 106
  • 352
  • 604
2

This worked for me:

location /static {
     alias /usr/src/app/project/static;
 }
abhishek
  • 21
  • 1