Node & express + nginx with multiple locations

I am planning to run 2 node applications with expression in one subdomain, with nginx serving static files. In both node apps I am using:

app.use(express.static(path.join(__dirname)));

      

I have the following nginx configuration:

server {
    listen 80;
    server_name sub.domain.com;
    index index.html;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

    location / {
        proxy_pass http://localhost:232;
    }

    location ~ ^/(dist/|img/|app/|css/) {
        root /var/www/app/main/;
    }


    location /admin {
        proxy_pass http://localhost:233;
        rewrite ^/admin /$1 break;
    }

    location ~ ^/admin/(dist/|img/|app/|css/) {
        root /var/www/app/admin/;
        access_log off;
        expires max;
    }
}

      

With this setting, everything works fine for the "main" application (accessed at sub.domain.com), but the "admin" application (sub.domain.com/admin) uses the same static files as for the "main " Appendix. How do I change my setting to achieve the correct behavior?

+3


source to share


2 answers


Check with your dev-tools to make sure static assets are trying to download from the correct url; that is, http://sub.domain.com/admin/css/

or /admin/css/

, etc. It is possible that the paths are generated relative to their project roots and appear as /css/

, so when the browser tries to restore them, it goes to http://sub.domain.com/css/

.



0


source


I think the order of the blocks in your configuration might be an issue. Check out this link for more details . Try using this refactored config -



    server {
        listen 80;
        server_name sub.domain.com;
        index index.html;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        location ~ ^/admin/(dist/|img/|app/|css/) {
            root /var/www/app/admin/;
            access_log off;
            expires max;
        }

        location /admin {
            proxy_pass http://localhost:233;
            rewrite ^/admin /$1 break;
        }

        location ~ ^/(dist/|img/|app/|css/) {
            root /var/www/app/main/;
        }

        location / {
            proxy_pass http://localhost:232;
        }
    }

      

0


source







All Articles