简体   繁体   中英

NGINX as reverse proxy cannot display static public files

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) ..."

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;

    }

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM