0

I have 2 linux servers on my local network. I use the PC with the IP 192.168.1.111 as application server to run my node application on port 8080. As web server I use NGINX on a PC with the IP 192.168.1.100 and configured it as reverse proxy.

In a Network Tab of the Browser I see that all files are served properly (Status 200 OK). But all static files are not displayed.

Static files (css, js, images, fonts) are located in subfolders inside /var/www/domain.com/public

Any idea why this issue occurs?

Here my nginx.conf file:

user www-data;
worker_processes 2;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/domain.com/access.log;
        error_log /var/log/nginx/domain.com/error.log;

        gzip on;
        gzip_disable "msie6";

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

Here the /etc/nginx/sites-enabled/domain.com file

upstream appserver {
        server 192.168.1.111:8080;
}

server {

        listen  80;

        server_name domain.com www.domain.com;
        root /var/www/domain.com/public;

        location ~ ^/(images/|js/|css/|media/|favicon.ico) {
                #access_log off;
                expires off;
        }

        location / {
                proxy_pass http://appserver;
                include /etc/nginx/proxy_params;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

And here the access.log file

192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/shop.png HTTP/1.1" 200 13643 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/code.png HTTP/1.1" 200 13443 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
192.168.1.105 - - [26/Jan/2017:21:33:54 +0100] "GET /images/line.png HTTP/1.1" 200 13643 "http://192.168.1.100/home" "Mozilla/5.0 (X11; Linux x86_64) ..."
2
  • have you tried to add try_files $uri =404 in your location? Commented Jan 28, 2017 at 16:20
  • No. It doesn't help Commented Jan 28, 2017 at 16:38

2 Answers 2

1

I'm not great with regular expressions and nginx but this is how I do it and it works for me . basically getting the directory and file path being sent to the get url and then passing it to an alias public path ;You seem to be getting the location and not doing anything with it except for turning off the "expires" .

Code:

 location ~ ^/(?<dir>[^/]+)/(?<file>[^/]+)$ {

            gzip on;               
            alias /nodeproj/public/$dir/$file;

    }
Sign up to request clarification or add additional context in comments.

1 Comment

My node project (app server) is not on the same machine than the NGINX proxy. According to the documentation I read setting a root directive at the server level will apply in a location block.
0

The configuration I posted is correct. The fix for this issues was actually changing the ownership of the root folder for static files on a pro.

Permission before:

drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 css/
drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 fonts/
drwxr-xr-x 2 www-data root 4096 Jan 26 15:01 images/
drwxr-xr-x 2 www-data root 4096 Jan 26 16:17 js/
webmaster@proxy:~$

Permission after (this works):

drwxrwxr-x 2 webmaster webmaster 4096 Jan 26 00:00 css/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 13 00:15 fonts/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 13 00:15 images/
drwxrwxr-x 2 webmaster webmaster 4096 Jan 26 16:50 js/
webmaster@proxy:~$ 

To change the ownership and set the correct permission run the following commands:

sudo chmod 775 /var/www/domain.com/public/ -R
sudo chown -R webmaster:webmaster /var/www/domain.com/public/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.