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

后端 未结 3 1527
清歌不尽
清歌不尽 2020-12-31 03:59

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 serve

相关标签:
3条回答
  • 2020-12-31 04:38

    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/;
        }
    }
    
    0 讨论(0)
  • 2020-12-31 04:45

    This worked for me:

    location /static {
         alias /usr/src/app/project/static;
     }
    
    0 讨论(0)
  • 2020-12-31 04:48

    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.

    0 讨论(0)
提交回复
热议问题