1

I have similar project structures and directories in almost all nodejs apps I'm proxy forwarding via nginx. The structure is as follows:

/var/srv
-/app1
 -/public
  -/css
  -/js
  -/img
-/app2
 -/public
  -/css
  -/js
  -/img
-/app3
 -/public
  ......

hope you get the idea. For now I've hard coded serving static files code in nginx by providing location and aliasing for each project but I want something like this :

location /$app_var/public/(js or css or img or other static content) {

then aliasing it as

alias /var/srv/$app_var/public/(js or css or img or other static content)

so that it serves static content based on project's directory and I don't have to manually specify each and every project location. please do comment if I haven't explained it better. Thankyou :)

My current config :

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /srv/www/;
        index index.html index.htm;

        server_name example.com www.example.com;


   rewrite ^/(.*) https://nulll.me/$1 permanent;
}

server {
        listen 443;
        server_name example.com example.com;

        root /srv/www/;
        index index.html index.htm;

        ssl on;
        ssl_certificate /home/---------------.crt;
        ssl_certificate_key /home/------------.key;

        ssl_session_timeout 5m;

        ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

        location / {
                try_files $uri $uri/ =404;
        }

        location /app1 {
        alias /srv/www/watch/;
        proxy_pass http://127.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
    #simillarly more apps

     location /app1/public/img {
         alias /srv/www/app1/img/;
         access_log off;
         expires max;
     }
     # simillarly for /css , /js and for each website.

}
0

1 Answer 1

3

Use a regex ?

Something like this should make it :

location ~ ^/(?<app>[^/]+)/public/(?<dir>[^/]+)/(?<file>[^/]+)$ {
    alias /var/srv/$app/public/$dir/$file;
}
5
  • it looks good but can you expain what do those regex inside each ( ) do , not a thorough explanation just what string will they allow. Sorry I'm a bit noob when it comes to regexes. And thanks for helping :) Mar 29, 2015 at 16:14
  • @AbhinavGauniyal They allow strings containings one or more characters except slashes and capture them into named variables. Mar 29, 2015 at 16:16
  • just one real quick request. What if my css or js directory contains more directories , and here I'm assuming /var/srv/$app/public/$dir/$file; will evaluate just /public/css/file.css and not /public/css/style1/main.css. Can you recommend a solution for any file inside $dir to be served. Thanks. Mar 29, 2015 at 16:27
  • @AbhinavGauniyal Turn the regex into ^/(?<app>[^/]+)/public/(?<dir>.+)/(?<file>[^/]+)$ Mar 29, 2015 at 16:30
  • @AD7six , I plan on migrating everything to public folder and have edited conf in question. Sorry. Mar 29, 2015 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .