简体   繁体   中英

Express/Nginx can't load static files

My Express app can't load static files on browser but it works in curl. I use nginx as webserver. App works well but without any static files. What am I doing wrong here?

App.js

...

App.use(cors())
App.use("/data", express.static(__dirname + '/data'));

App.use('/api', require('./routes/api'))

App.listen(1337)

nginx

server
{
        listen x.x.x.x:80;
        server_name example.com www.example.com ;

        location /api {

                proxy_pass http://x.x.x.x:1337;

                ...
        }

    location / {
                return 301 https://$server_name$request_uri;
        }

}

server
{
        listen x.x.x.x:443 ssl;
        server_name example.com www.example.com ;
    root /path/to/public_html;
        index index.php index.html index.htm;
        ssl on;
        location /api {
                add_header X-Cache "HIT from Backend";

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;

                proxy_pass http://x.x.x.x:1337;

                ...
        }
}

Curl is ok:

http://127.0.0.1:1337/data/pic.png

But browser is not:

http://example.com/api/data/pic.png

router:

App.use('/api', require('./routes/api'))

Try this Nginx config, it will redirect http://example.com/api/data/pic.png to http://xxxx:1337/data/pic.png

location /api {
  rewrite /api(.*) /$1 break;
  proxy_pass http://x.x.x.x:1337;

  ...
}

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