Static files served with Nginx empty or don't load?

Viewed 20

I was used to serving static files through express static but want to move to Nginx. I keep my static files in a public folder: /home/user/Documents/app.com/CURRENT PROJECT/public/.

On my websites they are called like this: app.com/css/styles.js, app.com/fonts/font.woff2, app.com/js/main.js.

I wasn't able to figure it out with nginxs examples. When I tried my config they just returned 302 codes. I have tried these versions of the config + I have the entire version bellow if anyone needs it for reference.

location ~ \.(css|js|woff|woff2|png|jpg|jpeg|webp|svg|mp3) {
          root '/home/user/Documents/app.com/CURRENT PROJECT/public';
          gzip_static on;
          expires max;
}
#for each path
location /css/ {
          root '/home/user/Documents/app.com/CURRENT PROJECT/public';
          gzip_static on;
          expires max;
          autoindex on;
}

Full:

/etc/nginx/nginx.conf                                                                                                                                                                                                                                                                                                                
user www-data;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 8192;
        multi_accept on;
}

http {

    upstream loadbalance {
        least_conn;
        server app:8003;
    }

    limit_req_zone $binary_remote_addr zone=ip:10m rate=4r/s;
    http2_push_preload on;

    server {
        listen 80;
        listen 443 ssl http2;
        server_name www.app.com;

        ssl_certificate         /etc/ssl/certs/cert.pem;
        ssl_certificate_key     /etc/ssl/private/key.pem;
        ssl_client_certificate /etc/ssl/certs/cloudflare.crt;

        return 301 https://app.com$request_uri;
    }

    server {

        limit_req zone=ip burst=20 delay=14;
        server_name app.com;

        ##
        # SSL Settings
        ##
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        ssl_certificate         /etc/ssl/certs/cert.pem;
        ssl_certificate_key     /etc/ssl/private/key.pem;
        ssl_client_certificate /etc/ssl/certs/cloudflare.crt;

        # added
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_session_cache shared:SSL:50m;
        ssl_session_timeout 1d;
        ssl_session_tickets on;

        # OCSP Stapling
        ssl_stapling on;
        ssl_stapling_verify on;
        resolver 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;
        resolver_timeout 2s;

        location ~ \.(css|js|woff|woff2|png|jpg|jpeg|webp|svg|mp3) {
                root '/home/user/Documents/app.com/CURRENT PROJECT/public';
                gzip_static on;
                expires max;
                autoindex on;
                #add_header Cache-Control private; 
        }

        location / {
            proxy_http_version 1.1;
            proxy_request_buffering off;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;

            proxy_pass http://loadbalance;
        }

    }

    # Gzip Settings
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 32 16k;
    gzip_http_version 1.1;
    gzip_min_length 1024;
    gzip_types image/jpeg image/bmp image/svg+xml text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon;


    client_body_timeout 16;
    client_body_buffer_size 12K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;
    client_header_timeout 12;
    keepalive_timeout 15;

    send_timeout 10;
    access_log off;
    error_log /dev/null;
    include servers/*;
}
0 Answers
Related