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/; }
Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.
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.
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.
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/; } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With