Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make NGINX serve static content like .js, .css, .html?

Tags:

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/;         } 
like image 939
Pulkit Avatar asked May 21 '14 07:05

Pulkit


People also ask

Can nginx be used for serving static content?

Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.

How nginx serve html files?

To serve static files with nginx, you should configure the path of your application's root directory and reference the HTML entry point as the index file. In this example, the root directory for the snake deployment is /home/futurestudio/apps/snake which contains all the files.

Where do I put html for nginx?

These are the all files and directories of nginx. The default path for the html page in nginx is /var/www. The html page that is running on our localhost is set to default in sites-available directory.


2 Answers

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/;     } } 
like image 130
ist Avatar answered Oct 09 '22 03:10

ist


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.

like image 45
code-8 Avatar answered Oct 09 '22 04:10

code-8