Chirag Jain
Chirag Jain

Reputation: 2418

Nginx multiple static route

Directory Structure:

project
|__profile_pictures
   |__user1.png
|__static
   |__js
      |__main.js

Requests:

1) /js/main.js

2) /profile_pictures/user1.png

Nginx configuration:

location ~/profile_picture(^.+\.(jpg|jpeg|gif|png)$) {
    alias /home/chirag/Desktop/project/profile_pictures/$1;
    expires -1;
}

location ~*(^.+\.(jpg|jpeg|gif|css|png|js|ico|eot|otf|svg|ttf|woff|hbs)$) {
    alias /home/chirag/Desktop/project/static/$1;
    expires -1;
}

First request should goto static folder.(Working) Second request should goto profile_pictures folder.

Second request is failing. What am i missing here?

Upvotes: 0

Views: 240

Answers (1)

Shiva
Shiva

Reputation: 56

The problem is with the first profile picture location block. So the right way to handle this would be

location ~ ^/profile_picture/(.+\.(jpg|jpeg|gif|png)$) {
     alias /home/chirag/Desktop/project/profile_pictures/$1;
     expires -1;
}

location ~*(^.+\.(jpg|jpeg|gif|css|png|js|ico|eot|otf|svg|ttf|woff|hbs)$) {
    alias /home/chirag/Desktop/project/static/$1;
    expires -1;
}

Do not use the ^ in the middle of a regex. It is meant to suggest the beginning. Also you missed the following / after the profile picture. The new location block should work for you.

Good Luck Mate. Cheers

Upvotes: 1

Related Questions